QA
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→

More REST APIs & Web Services Interview Questions

Beginner LevelREST APIs & Web Services

What is HTTP Method Idempotency and how do you implement Idempotency Keys for REST APIs?

Beginner LevelREST APIs & Web Services

What are standard HTTP Status Codes (200, 201, 204, 400, 401, 403, 404, 409, 429, 500, 503)?

Beginner LevelREST APIs & Web Services

What are HTTP Verbs (GET, POST, PUT, PATCH, DELETE) and when to use PUT vs PATCH?