Skip to content

🧠 A lightweight, extensible circuit breaker using Redis for distributed state in microservices. πŸŒ‰ Includes a gateway, auth service, and profile service communicating over HTTP. 🚦 Circuit breaker in profile service handles auth failures with Redis-based state persistence.

Notifications You must be signed in to change notification settings

Minhajxdd/circuit-breaker-redis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

38 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

⚑ Redis Circuit Breaker Example

πŸ› οΈ A minimal distributed circuit breaker implementation using Redis
🚦 Built for service-to-service communication with simple gateway + microservices architecture


πŸŒ‰ Architecture Overview

graph TD
    Client -->|Request| Gateway
    Gateway -->|Redirect| ProfileService
    ProfileService -->|Fetch Auth Info| AuthService
    subgraph Redis
        CBState[(Circuit Breaker State)]
    end
    ProfileService -->|State Check| CBState
Loading

✨ Features

  • βœ… Simple Gateway to route requests
  • βœ… Two microservices: AuthService and ProfileService
  • βœ… Circuit Breaker logic at ProfileService to manage AuthService calls
  • βœ… Redis used for:
    • πŸ” Shared state management (failures, open/close logic)
    • 🌐 Distributed control across instances

🚦 Circuit Breaker Flow

            .---------------------------.
            |  ProfileService          |
            |--------------------------|
Request --> | Is Circuit Open?         |
            |     |                    |
            |    No                    |
            |     ↓                    |
            | Call Auth Service        |
            |     |                    |
            |  Success?                |
            |     |                    |
            |    Yes ----------------> Return Response
            |     ↓
            |  Increment Failure Count
            |     |
            |  Threshold Reached?
            |     ↓
            |   Open Circuit (Redis)
            '---------------------------'

πŸ“¦ Project Structure

πŸ“¦project-root
 ┣ πŸ“gateway
 ┃ β”— πŸ“„index.js
 ┣ πŸ“auth-service
 ┃ β”— πŸ“„auth.js
 ┣ πŸ“profile-service
 ┃ β”— πŸ“„profile.js  <- Contains Circuit Breaker logic
 ┣ πŸ“redis
 ┣ πŸ“„README.md

πŸ”§ Custom Logic

  • 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

🧩 Future Enhancements

  • 🧠 Auto-recovery based on success ratio
  • πŸ”Œ Pluggable strategy for breaker logic
  • βš™οΈ adding correct fallback result

🎬 Request Flow

Request ---> πŸŒ‰ Gateway ---> πŸ‘€ ProfileService ---❓---> πŸ” AuthService
                                  |     β–²
                                  β–Ό     |
                             🧠 Circuit Breaker (Redis)
                                  |
                         [ OPEN πŸ”΄ / CLOSED 🟒 ]

πŸš€ Quick Start

# 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.


πŸ“œ License

MIT Β© 2025

About

🧠 A lightweight, extensible circuit breaker using Redis for distributed state in microservices. πŸŒ‰ Includes a gateway, auth service, and profile service communicating over HTTP. 🚦 Circuit breaker in profile service handles auth failures with Redis-based state persistence.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published