Beginner LevelREST APIs & Web Services
What is the Circuit Breaker Pattern in REST API client integrations?
❓ Interview Prompt: Explain Circuit Breaker states (`Closed`, `Open`, `Half-Open`), failure thresholds, preventing cascading failures, and fallback execution.
The Circuit Breaker Pattern wraps outbound REST API calls to prevent cascading system failures when downstream services experience outages:
3 States:
1
Closed (Normal): Requests flow through to downstream service normally. Failure counts are tracked.
2
Open (Tripped): When failure rate exceeds threshold (e.g. 50% errors), circuit trips OPEN. All requests fail instantly without attempting network calls, returning fallback responses!
3
Half-Open (Testing): After a sleep window, circuit allows a trial request to verify if downstream service has recovered.
Code Implementation Example
typescript
import opossum from 'opossum';
async function fetchThirdPartyApi() {
return await fetch('https://unstable-api.com/data');
}
// Wrap API call in Circuit Breaker!
const breaker = new opossum(fetchThirdPartyApi, {
timeout: 3000, // If request takes > 3s, fail
errorThresholdPercentage: 50,
resetTimeout: 10000 // Retry after 10s
});
breaker.fallback(() => ({ data: 'Fallback cached data' }));💡Wrapping external API calls in Opossum Circuit Breaker middleware.
🎯 Interview Key Takeaway
Circuit breakers fail fast when downstream APIs experience outages, preventing cascading thread starvation.
#Circuit Breaker#Resilience#Fault Tolerance#Beginner
Back to all REST APIs & Web Services Questions→