%% date:: [[2023-02-23]], [[2023-11-30]] %% # [[Circuit breaker pattern]] The Circuit Breaker Pattern is a design pattern in [[Site Reliability Engineering]] that involves inserting a proxy in front of a component, with the goal of rejecting requests after the component's response times decrease beyond a threshold. It is so named because it acts as a switch that shuts off electricity when it is "tripped", or when it detects excessive electrical load. The Circuit Breaker Pattern is paradoxical because its goal is to increase overall application reliability by inducing failure. ## Preventing [[Retry storm]]s A retry storm is a type of [[Cascading Failures|Cascading failure]] that occurs when a degraded service fails to respond to a client's request, but the client continues to repeat the request. It is particularly prevalent in [[Microservices|microservices]], because one microservice does not know the state of another. The repetitive requests only exacerbate the degradation. The Circuit Breaker Pattern prevents retry storms by instituting a middleman, like a traffic controller, between microservices. ## Workings of a circuit breaker - A proxy is instituted to mediate requests between microservices. - When a service returns failures in excess of a defined threshold, the circuit breaker proxy "trips" and automatically fails any future requests going to that service for a timeout period. - The faulty service recovers or is replaced, and begins to function normally again. - The circuit breaker, after the timeout period expires, begins to serve requests to the service again. ### Closed state The closed state is when the circuit breaker has not yet tripped. It represents normal functioning of the system. [^nerdfortech] ### Open state The open state is when the circuit breaker is tripped and returns failures to the client without passing them on to the degraded service. ### Half-open state The half-open state exists somewhere between closed and open: the circuit breaker has begun to return errors for some requests, but continues to allow others to pass through. In this way, it provides an [[Artificial bottleneck]] that gives the degraded service time to recover. ## Implementations of the circuit breaker pattern - [[Dimming]] - [[Exponential backoff]] [^nerdfortech]: Pubudu, N. (2021). _Design patterns for microservices - Circuit breaker pattern_. Retrieved from https://medium.com/nerd-for-tech/design-patterns-for-microservices-circuit-breaker-pattern-ba402a45aac2 . [[Design Patterns for Microservices — Circuit Breaker Pattern]]