The Circuit Breaker Design Pattern of Microservices Architecture.
In a distributed system, service failures or unresponsive components can occur. If a calling service continues to make requests to a failing or slow service, it can lead to degraded performance or complete system failures.
The Circuit Breaker pattern provides a mechanism to detect and handle faults by tripping the circuit, preventing further requests to the failing service. This promotes fault tolerance and prevents the system from overloading or becoming unresponsive due to repeated failures.
- Java Development Kit (JDK 8 or above)
- Maven
- IDE like STS(Spring Tool Suite) or Eclipse
We will create 3 microservices :
- Account Service -> Manages Bank Account creation and fetching
- Fund Transfer Service -> Manages trasnfer of funds for accounts
- Bank Rates Service -> Will give cross rates to Fund Transfer Service before it processes transactions.
-
Create Microservices : Create three separate Spring Boot projects (either via Spring Initializr or your preferred method).
-
Add Dependencies : For Bank Rate Service, add just web dependency :
-
Business Logic :
- Account Service - Create a REST API to get an account by account number.
- Fund Transfer Service - Create a REST API to post fund transfer transaction by calling Bank Rate service for rate calculcation.
- Bank Rate Service - Create a REST API to call post cross rates requried for fund transfer.
-
Bank Rate Service purpose and logic : Fund Transfer Service, before processing any transaction for particular account, will call Bank Rates Service in order to get the cross rates for fund processing based on currencies. If, from Currency and to Currency is equal, the cross rate would be 1. Otherwise, the cross rate would be 10.345. The amount that is being used processed for transfer will be updated with cross rate amount.
Here's the logic :
-
Run the services : Start all 3 services i.e. Account Service, Fund Transfer Service and Bank Rate Service.
-
For testing :
-
Hit the Actuator URL with Fund Transfer Service port 5052 to check if Fund Transfer service is successfully able to call Bank Rate Service. The state of Bank Rates service should be CLOSED with status as UP. http://localhost:5052/actuator/health
-
Make a few calls to Fund Transfer Service to ensure if interservice communication is working fine.
-
Now, stop the Bank Rates Service.
-
Make 3 POST calls on Fund Transfer Service and check with actuator URL if Circuit is Half Open. Since the number of failure calls are exceeding the threshold set for given number of minimum calls, the circuit will go to Half Open state.
-
Make 3 calls again in Half Open State to check if Bank Rates Service is up or not.
-
Since the service is not up and maximum calls allowed in Half Open State are exhausted, the actuator health URL will show that circuit is OPEN and Bank Rates Service is not responding.
-
Customize the behavior of services by editing the respective application.properties file. Adjust settings such as port, logging, and error handling.
Account Service :
Circuit Breaker configuration for Fund Transfer Service application.properties
Bank Rates Service :
FundTransfer Controller :
FundTransfer model :
Bank Rates Controller :
Bank Rates Model :
Contributions are welcome!
For questions or feedback, please email at tathoded@gmail.com OR dnyaneshsunilrao.tathode@genpact.com.