Circuit Breaker là pattern bảo vệ hệ thống khỏi cascading failures khi một dependency bị lỗi – lấy cảm hứng từ cầu dao điện.
- Hoạt động qua 3 states: Closed (hoạt động bình thường, theo dõi failure rate), Open (sau khi failure rate vượt threshold, tất cả requests đều fail fast ngay lập tức mà không gọi service lỗi – cho service thời gian recover), Half-Open (sau timeout, cho một số requests thử đến service, nếu thành công thì đóng lại, nếu thất bại thì mở lại).
Ví dụ không có Circuit Breaker: Payment Service gọi Fraud Detection Service bị chậm → threads bị block chờ timeout → thread pool exhaustion → Payment Service cũng down → cascading failure lan rộng.
The Circuit Breaker pattern protects a system from cascading failures when a dependency goes down — inspired by an electrical circuit breaker.
- It operates through three states: Closed (normal operation, monitoring the failure rate), Open (once the failure rate exceeds a threshold, all requests fail fast immediately without calling the failing service — giving it time to recover), and Half-Open (after a timeout, a small number of probe requests are allowed through; if they succeed, the circuit closes; if they fail, it reopens).
- Without a Circuit Breaker: the Payment Service calls a slow Fraud Detection Service → threads are blocked waiting for timeouts → thread pool exhaustion → Payment Service also goes down → cascading failure spreads.
- With a Circuit Breaker: after N failures, the circuit opens, requests fail fast with a cached response or fallback → the system remains partially functional.
- Netflix Hystrix is the well-known library (now in maintenance mode); Resilience4j is the modern choice for Java; Python has pybreaker; Istio Service Mesh has it built in.