Circuit breaker design pattern: Difference between revisions

Content deleted Content added
WP:NOTHOWTO
Removed uncited content. Made the tone more neutral
Line 1:
{{Short description|Software development pattern}}
'''Circuit breaker''' is a [[Design pattern (computer science)|design pattern]] used in [[software development]]. It is used to detect failures and encapsulates the logic of preventing a failure from constantly recurring, during maintenance, temporary external system failure or unexpected system difficulties. Circuit breaker pattern preventscan prevent [[cascading failure]]s particularly in [[Distributed computing|distributed systems]].<ref>{{Cite book |title=Machine Learning in Microservices Productionizing Microservices Architecture for Machine Learning Solutions |publisher=Packt Publishing |year=2023 |isbn=9781804612149}}</ref>
{{Multiple issues|{{cleanup rewrite|date=September 2013}}
{{how-to|date=September 2013}}}}
'''Circuit breaker''' is a [[Design pattern (computer science)|design pattern]] used in [[software development]]. It is used to detect failures and encapsulates the logic of preventing a failure from constantly recurring, during maintenance, temporary external system failure or unexpected system difficulties. Circuit breaker pattern prevents [[cascading failure]]s particularly in [[Distributed computing|distributed systems]].<ref>{{Cite book |title=Machine Learning in Microservices Productionizing Microservices Architecture for Machine Learning Solutions |publisher=Packt Publishing |year=2023 |isbn=9781804612149}}</ref>
 
CircuitThe circuit breaker pattern shouldcan be used alongin conjunction with other patterns, such as retry, fallback, and timeout pattern. This helps the system, to be more [[Softwareenhance fault tolerance|fault tolerant]]in systems. <ref>{{Cite book |title=Kubernetes Native Microservices with Quarkus and MicroProfile |publisher=Manning |year=2022 |isbn=9781638357155}}</ref>
According to Marc Brooker, circuit breakers can misinterpret a partial failure as total system failure and inadvertently bring down the entire system. In particular, sharded systems and cell-based architectures are vulnerable to this issue. A workaround is that the server indicates to the client which specific part is overloaded and the client uses a corresponding mini circuit breaker. However, this workaround can be complex and expensive.<ref>{{Cite book |title=Understanding Distributed Systems |isbn=9781838430214}}</ref><ref>{{Cite web |title=Will circuit breakers solve my problems? |url=https://brooker.co.za/blog/2022/02/16/circuit-breakers.html}}</ref>
 
==Challenges==
Circuit breaker pattern should be used along with other patterns such as retry, fallback and timeout pattern. This helps the system to be more [[Software fault tolerance|fault tolerant]].<ref>{{Cite book |title=Kubernetes Native Microservices with Quarkus and MicroProfile |publisher=Manning |year=2022 |isbn=9781638357155}}</ref>
 
According to Marc Brooker, circuit breakers can misinterpret a partial failure as total system failure and inadvertently bring down the entire system. In particular, sharded systems and cell-based architectures are vulnerable to this issue. A workaround is that the server indicates to the client which specific part is overloaded and the client uses a corresponding mini circuit breaker. However, this workaround can be complex and expensive.<ref>{{Cite book |title=Understanding Distributed Systems |isbn=9781838430214}}</ref><ref>{{Cite web |title=Will circuit breakers solve my problems? |url=https://brooker.co.za/blog/2022/02/16/circuit-breakers.html}}</ref>
==Common uses==
 
Assume that an application connects to a database 100 times per second and the database fails. The application designer does not want to have the same error reoccur constantly. They also want to handle the error quickly and gracefully without waiting for [[TCP connection]] timeout.
 
Generally Circuit Breaker can be used to check the availability of an external service. An external service can be a database server or a web service used by the application.
 
[[Circuit breaker]] detects failures and prevents the application from trying to perform the action that is doomed to fail (until it's safe to retry).
 
==Performance implication==
 
While it's safe to say that the benefits outweigh the consequences, implementing Circuit Breaker will negatively affect storage space, application complexity, and computational cost to the executing application. This is because it adds additional code into the execution path to check for the state of the circuit. This can be seen in the PHP example below, where checking APC for the database status costs a few extra cycles. Also, running the circuit breaker code itself consumes resources on the system where it is running, thus leaving less execution power for "real" applications.{{why|date=January 2021}}
 
By how much depends on the storage layer used and generally available resources. The largest factors in this regard are the type of cache, for example, disk-based vs. memory-based and local vs. network.
 
== Different states of circuit breaker ==