Circuit breaker example

Here's an example of a circuit breaker in action:

Scenario: A web application is built using a microservices architecture, where multiple services communicate with each other to provide a seamless user experience. One of the services, the "Payment Gateway" service, is responsible for processing payment transactions.

Problem: The Payment Gateway service is experiencing high latency and errors due to a sudden surge in traffic. This is causing the entire application to become unresponsive, leading to frustrated users and lost revenue.

Solution: To mitigate this issue, a circuit breaker is implemented to detect when the Payment Gateway service is experiencing issues and automatically redirect traffic to a fallback service or a retry mechanism.

Circuit Breaker Components:

  1. Circuit Breaker: A component that monitors the health of the Payment Gateway service and decides when to open or close the circuit.
  2. Fault Detector: A component that detects when the Payment Gateway service is experiencing issues, such as high latency or errors.
  3. Fallback Service: A service that can handle payment transactions in case the Payment Gateway service is unavailable.

How it Works:

  1. The Circuit Breaker component is configured to monitor the Payment Gateway service's health.
  2. When the Fault Detector component detects that the Payment Gateway service is experiencing issues, it sends a signal to the Circuit Breaker component.
  3. The Circuit Breaker component checks the signal and determines whether to open or close the circuit.
  4. If the circuit is open, the application redirects traffic to the Fallback Service.
  5. If the circuit is closed, the application continues to send requests to the Payment Gateway service.

Benefits:

  1. Improved User Experience: By redirecting traffic to a fallback service, the application can continue to function even when the Payment Gateway service is experiencing issues.
  2. Reduced Errors: The circuit breaker can detect and prevent errors from propagating throughout the application.
  3. Increased Availability: The circuit breaker can automatically switch to a fallback service or retry mechanism, reducing downtime and increasing overall availability.

Example Code:

// Circuit Breaker implementation in Java
public class CircuitBreaker {
    private boolean isOpen = false;
    private int failureCount = 0;

    public void monitorServiceHealth(Service service) {
        if (service.isHealthy()) {
            isOpen = false;
            failureCount = 0;
        } else {
            failureCount++;
            if (failureCount >= 3) {
                isOpen = true;
            }
        }
    }

    public boolean isCircuitOpen() {
        return isOpen;
    }

    public void closeCircuit() {
        isOpen = false;
        failureCount = 0;
    }
}

In this example, the CircuitBreaker class monitors the health of the Service object and determines whether to open or close the circuit based on the number of failures. If the circuit is open, the application redirects traffic to a fallback service.