Circuit Breaker Pattern
The circuit breaker pattern is a design pattern inspired by electrical circuit breakers. It is primarily used to monitor remote service calls and manage failures in a distributed system. The pattern acts as a safeguard, preventing a system from making repeated calls to a failing service, which can lead to resource exhaustion and degraded performance.
Overview
The circuit breaker pattern is implemented to detect failures and encapsulate the logic of preventing a failure from constantly recurring. This is particularly useful during maintenance, temporary external system failures, or unexpected system difficulties. By breaking the connection to a failing service, the circuit breaker allows the application to fail fast and conserve resources. Tools like Netflix Hystrix are commonly used to implement this pattern, managing remote service calls and handling timeouts effectively.
Client-Side Resiliency
The circuit breaker pattern is a client-side resiliency pattern. It acts as an intermediary between the application and the remote service, monitoring the remote call. If the call takes too long or fails frequently, the circuit breaker 'pops', preventing further calls to the failing service. This allows the client to fail fast and avoid consuming unnecessary resources.
Implementation with Netflix Hystrix
In Spring Cloud, the circuit breaker pattern is implemented using the @HystrixCommand annotation. This annotation wraps method calls with a Hystrix circuit breaker, managing calls through a thread pool and interrupting calls that exceed a specified timeout. Customizing the timeout can be achieved by passing additional parameters into the @HystrixCommand annotation, such as the execution.isolation.thread.timeoutInMilliseconds property.
Example Implementation
The following listing demonstrates how to wrap a remote resource call with a circuit breaker using the @HystrixCommand annotation:
Listing 5.2. Wrappering a remote resource call with a circuit breaker
//Imports removed for conciseness
@HystrixCommand #1
public List<License> getLicensesByOrg(String organizationId){
return licenseRepository.findByOrganizationId(organizationId);
}
#1 - @HystrixCommand annotation is used to wrapper the getLicenseByOrg() method with a Hystrix circuit breaker.
Example Scenario
Consider a scenario where Service B calls Service C through a circuit breaker. If Service C is slow, the circuit breaker will terminate the connection to prevent resource exhaustion. If enough failures occur, the circuit breaker trips, preventing further calls to Service C and allowing Service B to handle the situation gracefully.
Visual Representation
Figure 5.3. The circuit breaker trips and allows a misbehaving service call to fail quickly and gracefully.
In Figure 5.3, Service B (the client) does not directly invoke Service C. Instead, the call is delegated to the circuit breaker, which wraps it in a thread managed by a thread pool. This setup allows the circuit breaker to monitor the call and terminate it if it runs too long. The figure illustrates three scenarios: the happy path, where the call completes successfully; partial degradation, where the call is slow and the circuit breaker terminates it; and failure, where the circuit breaker prevents further calls to the failing service.
FAQ (Frequently asked questions)
What is the circuit breaker pattern?
How does the circuit breaker pattern help in service calls?
What type of pattern is the circuit breaker?
What happens when a circuit breaker 'pops'?
What role does a circuit breaker play between an application and a remote service?
How does a circuit breaker prevent resource exhaustion?
What happens when a circuit breaker trips?
What happens if Service C is slow in a circuit breaker scenario?
What does the circuit breaker do if enough failures occur?
What is the purpose of the circuit breaker pattern?
How is the circuit breaker pattern implemented?
How is a method wrapped with a Hystrix circuit breaker?
What does the @HystrixCommand annotation do?
How is the circuit breaker pattern implemented using Netflix Hystrix?
How is the circuit breaker pattern implemented in Spring Cloud?
How can you customize the timeout on a circuit breaker?