Circuit Breaker
There are situations where failures can be caused by unexpected situations and take relatively longer to fix. Retry or waiting for the request to timeout may not be the best option as it may cause further cascading issues such as resource contention and/or blocked threads.
Solution — Fail fast
Prevent the application from retrying an operation likely to fail:
- A circuit breaker acts as a proxy that monitors recent failures of an operation
- The proxy maintains a count of failures and if it crosses the set threshold, it’s placed in an Open state
- In the Open state, the request fails immediately and is handled appropriately by the application
- However, limited requests are still allowed to pass through to check if the operation is still failing or has been successful (fixed)
- If the operation continues to return failures, the Open state continues
- If the operation was successful, it is assumed the issue has been fixed and the circuit breaker switches to Closed state
Error and failure handling in this pattern requires careful consideration to create an acceptable end-user experience