π οΈ A minimal distributed circuit breaker implementation using Redis
π¦ Built for service-to-service communication with simple gateway + microservices architecture
graph TD
Client -->|Request| Gateway
Gateway -->|Redirect| ProfileService
ProfileService -->|Fetch Auth Info| AuthService
subgraph Redis
CBState[(Circuit Breaker State)]
end
ProfileService -->|State Check| CBState
- β Simple Gateway to route requests
- β
Two microservices:
AuthService
andProfileService
- β
Circuit Breaker logic at
ProfileService
to manageAuthService
calls - β
Redis used for:
- π Shared state management (failures, open/close logic)
- π Distributed control across instances
.---------------------------.
| ProfileService |
|--------------------------|
Request --> | Is Circuit Open? |
| | |
| No |
| β |
| Call Auth Service |
| | |
| Success? |
| | |
| Yes ----------------> Return Response
| β
| Increment Failure Count
| |
| Threshold Reached?
| β
| Open Circuit (Redis)
'---------------------------'
π¦project-root
β£ πgateway
β β πindex.js
β£ πauth-service
β β πauth.js
β£ πprofile-service
β β πprofile.js <- Contains Circuit Breaker logic
β£ πredis
β£ πREADME.md
- The circuit breaker stores:
- β Failure count
- β Last failure timestamp
- β Cooldown duration
- These are used to determine:
- π When to retry
- π When to stop calling
AuthService
Redis Keys:
cb:auth:state // OPEN / CLOSED
cb:auth:failures // Number of failed requests
cb:auth:lastFailure // Timestamp of last failure
- π§ Auto-recovery based on success ratio
- π Pluggable strategy for breaker logic
- βοΈ adding correct fallback result
Request ---> π Gateway ---> π€ ProfileService ---β---> π AuthService
| β²
βΌ |
π§ Circuit Breaker (Redis)
|
[ OPEN π΄ / CLOSED π’ ]
# 1. Clone the repository
git clone https://github.com/your-username/redis-circuit-breaker.git
cd circuit-breaker-redis
# 2. Copy the environment file
cp .env.template .env
# 3. Install dependencies for each service
cd gateway && pnpm install
cd ../auth && pnpm install
cd ../profile && pnpm install
# 4. Start everything with Docker Compose
cd .. # back to project root
docker-compose up
β Make sure Redis is running via Docker Compose.
βοΈ You can modify Redis or service ports in.env
as needed.
π§ Tip: This repo shows how you can build your own Resilience Layer without 3rd-party libraries using pure Node.js and Redis.
MIT Β© 2025