Skip to content

dnyanesh-genpact/Circuit-Breaker-Design-Pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Circuit-Breaker-Design-Pattern

The Circuit Breaker Design Pattern of Microservices Architecture.

Problem that Circuit Breaker Design Pattern solves

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.

Solution for this problem

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.

Prerequisites

  • Java Development Kit (JDK 8 or above)
  • Maven
  • IDE like STS(Spring Tool Suite) or Eclipse

Architecture Overview

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.

Steps :

  1. Create Microservices : Create three separate Spring Boot projects (either via Spring Initializr or your preferred method).

  2. Add Dependencies : For Bank Rate Service, add just web dependency : image

    For Fund Transfer service, add following dependencies : image

  3. Business Logic :

    1. Account Service - Create a REST API to get an account by account number.
    2. Fund Transfer Service - Create a REST API to post fund transfer transaction by calling Bank Rate service for rate calculcation.
    3. Bank Rate Service - Create a REST API to call post cross rates requried for fund transfer.
  4. 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 :

    image

  5. Run the services : Start all 3 services i.e. Account Service, Fund Transfer Service and Bank Rate Service.

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

Microservices Configuration

Customize the behavior of services by editing the respective application.properties file. Adjust settings such as port, logging, and error handling.

Account Service :

image

Circuit Breaker configuration for Fund Transfer Service application.properties image

Bank Rates Service :

image

Sample Code for Controllers and Models

FundTransfer Controller :

image

FundTransfer model :

image

image

Bank Rates Controller :

image

Bank Rates Model :

image

Contributing

Contributions are welcome!

Contact

For questions or feedback, please email at tathoded@gmail.com OR dnyaneshsunilrao.tathode@genpact.com.

About

The Circuit Breaker Design Pattern for Microservices Architecture

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages