This repository demonstrates a circuit breaker pattern using Node.js and Redis. It contains multiple services that communicate with each other and keep track of their respective health statuses in real time.
- Overview
- What is a Circuit Breaker?
- Project Structure
- Prerequisites
- Installation
- Running the Project
- How It Works
- Mermaid Diagram
This project consists of:
- Health Service: Periodically checks the health of other services and publishes “up” or “down” statuses to Redis.
- Notification Service: A placeholder for another service (subscriber or data receiver).
- Authentication Service: Another microservice that interacts with or relies on the circuit breaker.
Each microservice communicates its own health status. If a service is marked “down,” other services will stop sending requests until it recovers, thus avoiding repetitive failures.
A circuit breaker in microservice architecture is a pattern that prevents a client from making requests to an unhealthy or non-responsive service. It does this by “opening” the circuit when failures are detected, thereby avoiding wasteful calls and additional load, until the service is marked healthy again.
.
├── healthService.js # Checks and publishes health statuses to Redis
├── notificationService.js # Placeholder for a second service (Subscriber)
├── authenticationService.js # Placeholder for a third service
├── package.json # NPM scripts and dependencies
├── install_redis.sh # (Optional) Installs Redis on Linux or macOS
└── README.md # Project documentation
- Node.js (v14+ recommended)
- Redis (v6.0+ recommended)
- Make sure Redis is running on the default port
6379or adjust the environment variables accordingly.
- Make sure Redis is running on the default port
- npm or yarn (to install dependencies)
To install Redis automatically, you can run the install_redis.sh script. Otherwise, install Redis manually as per your operating system’s instructions.
-
Clone this repository:
git clone https://github.com/sangleshubham/circuit-breaker-for-micro-services.git cd circuit-breaker-for-micro-services -
Install dependencies:
npm install
or
yarn install
-
(Optional) Configure environment variables: Create a
.envfile at the root if you want to override defaults:REDIS_SERVER=localhost REDIS_PORT=6379 REDIS_USERNAME=default REDIS_PASSWORD=Adjust values as needed.
-
Ensure Redis is running
- Use
npm run prestartto install redis-tools.
- Use
-
Start all services:
npm run healthService npm run authenticationService npm run notificationService
This will start (adjust code if you need a custom port):
healthService.json port 3002notificationService.json port 3000authenticationService.json port 3001
-
Verify services are running
- Check the console logs for messages like “Health Service is up.”
- Test endpoints via a browser or curl. For example,
curl http://localhost:3000/sendDataToNotificationServicetriggers a api call to notification service. - If response is correct then the notification service is up. If the service is down the API call will be skipped.
- Health Service periodically sends requests to each microservice’s
GET /healthyendpoint. - Based on the response, it publishes messages to Redis (
serviceuporservicedown). - Other services subscribe to those channels. When they see a
servicedownmessage, they update an in-memory map to mark that service as “down.” - Before making any external request, a service checks this map (the circuit breaker). If the target is “down,” it short-circuits and avoids that call.
- Once the Health Service detects a service is back up, it publishes a
serviceupmessage, allowing requests again.
Below is a simplified overview of how the services interact, with the circuit breaker mechanism:
- Step 1: The Health Service (HS) pings each microservice’s
/healthyendpoint. - Step 2: It publishes the health status (up/down) to Redis (R).
- Step 3: Other services (like Service 1 and Service 2) subscribe to Redis channels for these updates.
- Step 4: Each service uses a circuit-breaker check (up or down). If down, it immediately short-circuits further calls; if up, it proceeds.
Feel free to submit issues or pull requests if you find any bugs or want to propose improvements!