Circuit Breaker Pattern
- Circuit Breaker Pattern enables an application to break an operation in the expectation that it will fail.
- While designing for the success of an operation, failure should also be considered as there are chances when an operation may fail due to unanticipated events which cannot be fixed quickly and cannot be resolved even after some retry operations. In these cases, the application should accept that the operation has failed and handle this failure gracefully.
- Using Circuit Breaker pattern, system can quickly fail without compromising the response time and just keep waiting for the process to timeout or never return.
- In Circuit breaker pattern, monitor the number of recent failures which have occurred, and decide whether to allow the operation to proceed, or failed by simply returning an exception.
- Circuit breaker acts as a proxy and uses three states to decide whether to perform an operation or not. When a request occurs, it will be routed to Circuit Breaker where it will check the state and decide to perform an operation or not.
- Very first time the Circuit Breaker have “Closed” state and it allows to perform the operation.
- If operation performs successfully then it will remain “Closed”, return the result and accepting new request.
- If operation fails then it will use the failure counter and retry the operation until the failure counter reaches to the threshold limit.
- If operation fails and the failure counter reaches to the threshold limit then the Circuit Breaker will trip and change its state to “Open” and it will not be available to perform any request.
- Once the Circuit Breaker opened, it will wait for a particular time and after timeout, it will change the state to “Half-Open”.
- In half-open state, Circuit breaker can accept a limited number of request for processing.
- In case of failure, circuit breaker will change the state to “Open”.
- In case of success, circuit Breaker will increment the success counters and return the result.
- When success counter reached to threshold limit it will change the state to “Closed”, reset all the counters and start accepting new request .