What is circuit breaking pattern in Node.js microservices?
Why Interviewers Ask This
This is a differentiating question used for senior and lead roles. Interviewers want to see if you can explain not just what happens, but why — and what the trade-offs are in different approaches.
Answer
The circuit breaker pattern prevents cascading failures in distributed systems by detecting when a downstream service is failing and stopping requests to it temporarily, allowing it time to recover. Named after electrical circuit breakers. States: (1) Closed (normal) — requests flow through, failures are counted; (2) Open — after reaching a failure threshold, the circuit "trips" — requests immediately fail without calling the downstream service (fast failure); (3) Half-Open — after a timeout, a limited number of test requests are allowed through; if they succeed, the circuit closes; if they fail, it opens again. Benefits: fast failure instead of waiting for timeouts, system stability, self-healing. Node.js implementations: opossum — popular Node.js circuit breaker library: const breaker = new CircuitBreaker(asyncFunction, { timeout: 3000, errorThresholdPercentage: 50, resetTimeout: 30000 }); breaker.fire(args); cockatiel — policy-based resilience library with circuit breaking, retry, timeout, and bulkhead patterns. Combine with retry patterns (exponential backoff with jitter) for comprehensive resilience.
Common Mistake
A common mistake is memorizing definitions without understanding implications. When asked this question, go one level deeper — explain what happens when this concept is misused or ignored.
Previous
What is the N+1 query problem and how do you solve it in Node.js?
Next
What is graceful shutdown in Node.js and how do you implement it?
More Node.js Questions
View all →- Advanced How does Node.js handle concurrency without multiple threads?
- Advanced What is the Node.js memory model and how does garbage collection work?
- Advanced What are memory leaks in Node.js and how do you detect them?
- Advanced What is the difference between process.exit() and throwing an uncaught exception?
- Advanced What is the N+1 query problem and how do you solve it in Node.js?