A distributed API Gateway built with Node.js and Express, designed to handle API traffic efficiently and reliably across multiple backend services.
The gateway sits between clients and backend servers, routing requests intelligently while protecting the system from overload and failure — the same core problems that real-world gateways like Kong, NGINX, and AWS API Gateway solve.
- Overview
- Features
- Architecture
- Tech Stack
- Getting Started (Local Setup)
- Running with Docker
- Testing the API
- Project Structure
- Learning Goals
- System Architecture
- Benchmark Results
The main goal of this project is to understand how an API Gateway improves scalability, performance, fault tolerance, and resilience — under normal traffic, high traffic, and partial service failures.
It's built as a multi-instance, distributed system (not a single gateway process), so it also explores how shared state, coordination, and failure detection work across multiple gateway nodes.
| Feature | Purpose |
|---|---|
| Load Balancing | Distributes incoming traffic across gateway/backend instances |
| Health-Aware Routing | Skips unhealthy instances based on live health checks |
| Rate Limiting | Protects backend services from being overwhelmed |
| Redis Caching / Shared State | Keeps state consistent across distributed gateway instances |
| Circuit Breaker | Stops sending traffic to a failing service until it recovers |
| Backpressure Handling | Prevents system overload during traffic spikes |
| Request Timeouts | Avoids hanging requests and cascading failures |
| Multiple Backend Servers | Simulates a realistic multi-service backend |
| Distributed Gateway Instances | Multiple gateway nodes working behind a load balancer |
Client
│
▼
Load Balancer (http://localhost:5000)
│
▼
Gateway Instances (multiple, health-aware)
│
▼
Backend Servers (multiple, load-balanced)
│
▼
Redis (shared state: rate limits, health, circuit breaker status)
- The Load Balancer is the single entry point for clients.
- It forwards requests to one of several Gateway instances, chosen based on health.
- Each Gateway applies rate limiting, circuit breaking, and backpressure logic before forwarding to a Backend server.
- Redis stores shared state (health status, rate-limit counters, circuit breaker state) so all gateway instances stay in sync.
- Node.js + Express.js — Gateway, Load Balancer, and Backend servers
- Redis — Shared state across distributed instances
- Docker & Docker Compose — Containerized multi-service setup
- k6 — Load testing and traffic simulation
- JavaScript — Core language throughout
git clone https://github.com/mohdaffan01/distributed-api-gateway.git
cd distributed-api-gatewayInstall dependencies for each component separately:
cd backend
npm install
cd ../gateway
npm install
cd ../load-balancer
npm installCreate .env files for the Gateway and Load Balancer, containing the backend and gateway URLs (ports, service addresses, etc.).
Make sure Redis is running locally on:
localhost:6379
Run each backend server on its configured port.
Run each Gateway instance.
Run the Load Balancer on:
http://localhost:5000
curl http://localhost:5000/api/usersInstall:
- Docker Desktop
- Git
Verify installation:
docker --version
docker compose versiongit clone https://github.com/mohdaffan01/distributed-api-gateway.git
cd distributed-api-gatewaydocker build -t api-backend:latest ./backend
docker build -t api-gateway:latest ./gateway
docker build -t api-load-balancer:latest ./load-balancerdocker compose up -dThis starts:
- 4 Backend Servers
- 3 Gateway Instances
- 1 Load Balancer
- 1 Redis Server
docker compose psAll containers should show Up.
docker compose downLoad Balancer base URL:
http://localhost:5000
Health check:
curl http://localhost:5000/healthExpected response:
{
"loadBalancer": "OK"
}Sample API request:
curl http://localhost:5000/api/usersExpected response:
{
"success": true,
"count": 5,
"users": [ ... ]
}docker compose downdistributed-api-gateway/
├── backend/ # Backend service(s)
├── gateway/ # Gateway instances (rate limiting, circuit breaker, backpressure)
├── load-balancer/ # Entry point, health-aware routing
├── docker-compose.yml # Multi-service orchestration
└── README.md
This project was built to understand, hands-on, how production API gateways handle:
- Traffic distribution across multiple service instances
- Failure isolation using circuit breakers
- Overload protection using backpressure and rate limiting
- Coordination across distributed nodes using Redis as shared state
- Resilience testing under load using k6
This project is open source and available for learning purposes.