Circuit Breaker
Background
There are problems when calls to remote service failed usually because of slow network, timeout, etc.
This problem usually fixed by using retry pattern strategies where the caller will retry to call remote service (can add backoff too here).
But sometimes it failed due to something that is take longer to fix.
Hence retry pattern here will be pointless because it will exhaust the caller resources.
Additionally if the service is very busy this could create cascading failures.
Example:
Request comes in from user to our service.
Then our service need to do external request to 3rd party API.
But because of some issue, the request is timed out after
10s
.The response is timeout and we have retry patter that will retry request at max 5 times.
This will make the user request stuck for at least
50s
.Imagine if our service is very busy and handler thousand request each second.
This could lead to resource locked by at leash
50,000
request that could lead to cascading failure.
Solution
Circuit Breaker act as a proxy for application that might fail. The proxy should monitor the number of recent failures that occurred. And use that information to decide wether to allow operation or simply return error immediately.
3 States of circuit breaker patter:
Closed: The normal state, where all requests are allowed to pass through to the service.
Open: The circuit breaker rejects all requests to the service to prevent further failures
Half-open The circuit breaker allows a limited number of requests to pass through to the service to test if it's working again
Benefits:
Can prevent application from repeatedly trying to execute and operation thats likely to fail.
Allow to continue without waiting.
Prevent wasting CPU and Memory resource.
Example
Using Sony's Library
Here are example of circuit breaker usage from the popular sony/gobreaker
library.
Last updated