What is a circuit breaker, and why is it implemented in a microservice architecture?

A circuit breaker is a resilience mechanism that stops calls to a service once it detects repeated failures, and it's implemented in microservice architectures to prevent one failing service from cascading into a system-wide outage.

Key Points: • It wraps calls to a dependency and tracks the failure rate, tripping open once failures exceed a configured threshold. • Once open, it fails fast without calling the struggling dependency, freeing up threads and connections that would otherwise be stuck waiting on timeouts. • After a cooldown period it allows a limited number of trial requests through to check if the dependency has recovered before fully closing again. • Without it, a single slow or failing service can exhaust caller threads and connection pools, dragging down otherwise-healthy services that depend on it, a cascading failure. • It's typically implemented with a library like Resilience4j and paired with a fallback response so the caller can degrade gracefully instead of erroring out.

Example: If the Recommendations service starts timing out, the circuit breaker around it opens after a threshold of failures, so the Product Page service immediately falls back to a cached or default recommendation list instead of hanging on every request and eventually running out of its own threads.

Interview Tip: A concise interview answer is:

"A circuit breaker monitors calls to a dependency and, once failures cross a threshold, stops calling it and fails fast, giving it time to recover. I'd implement it in a microservices architecture to stop one failing service from cascading into an outage across everything that depends on it, usually with a library like Resilience4j and a sensible fallback."