What is a Circuit Breaker?
The circuit breaker pattern is a resilience pattern to prevent cascade failure. In a distributed system, the failure of one component can quickly lead to cascading failures as other dependent components also fail. A circuit breaker can mitigate this problem by failing fast: when a procedure call exceeds a specific failure threshold, the circuit breaker trips, and automatically returns an error without executing the failing procedure call.
Imagine a microservice relies on a slow database query to return a result. A horde of requests trigger the microservice to run multiple versions of this slow query, which causes the database CPU usage to spike. This in turn causes the existing queries to start to timeout and fail. Instead of continuing to execute queries against the database, a circuit breaker stops executing the queries, giving the database an opportunity to recover.
- Hystrix was one of the first libraries that implemented the circuit breaker pattern