GateKeeper is a lightweight Python-based gateway built using FastAPI. It acts as a proxy to route requests to multiple backend services, with built-in support for round-robin load balancing 🔁 and a circuit breaker mechanism 🔌 to prevent cascading failures in a microservices environment.
- 🔁 Round-robin request distribution across service replicas
- 🔌 In-memory circuit breaker (per backend URL)
- ⚡ FastAPI-based async HTTP routing with
httpx - 🔧 Configurable services via
config.yaml - 🛡️ Designed for high availability in microservice-based systems
- 🐍 Python 3.10+
- ⚡ FastAPI
- 🌐 httpx
- 🧾 PyYAML
git clone <your-repo-url>
cd GateKeeperpython -m venv gkvenv
source gkvenv/bin/activate # On Windows: gkvenv\Scripts\activatepip install -r requirements.txtuvicorn main:app --reload --port 8000You can start the mock backend services (e.g., mock_user_service.py, mock_order_service.py) or create your own services running on ports 8001, 8002, 8003, etc.
GET /v1/proxy/{service_name}/{path}GET http://localhost:8000/v1/proxy/user-service/profileForwards the incoming request to one of the configured replicas of the given service using round-robin logic.
Handles circuit breaker states internally to block failing backends.
services:
user-service:
- http://localhost:8001
- http://localhost:8002
order-service:
- http://localhost:8003Define each microservice and its replicas in config.yaml.
The gateway will load this mapping on startup.
-
⚡ FastAPI chosen for performance and async support
-
🌐 httpx.AsyncClient used for efficient non-blocking proxy calls
-
🧠 Circuit breaker state maintained in-memory for simplicity
-
🔁 RoundRobinManager class ensures even distribution across service replicas
-
🧼 Clean modular structure (router, circuit_breaker, utils)
Each backend URL maintains its own circuit state:
-
✅ CLOSED: Normal. Requests are sent.
-
🚫 OPEN: After 5 failures, requests are blocked for 60 seconds.
-
🔄 HALF-OPEN: After cooldown, a single test request is allowed:
-
If it succeeds → the circuit resets to CLOSED
-
If it fails → the circuit returns to OPEN
-
Logs are printed to the console when state transitions happen:
[FAILURE], [CIRCUIT OPENED], [CIRCUIT HALF-OPEN], [CIRCUIT CLOSED]GateKeeper/
├── main.py
├── config.yaml
├── requirements.txt
├── mock_user_service.py
├── mock_user_service2.py
├── mock_order_service3.py
├── README.md
├── gateway/
│ ├── router.py
│ ├── utils.py
│ └── circuit_breaker.py
└── .gitignore-
💾 Add persistent circuit breaker state (Redis, database, etc.)
-
📊 Add a
/statusendpoint to monitor circuit health -
✅ Write unit and integration tests
-
🐳 Dockerize gateway and backend services for production-ready deployment