Understanding API Circuit Breaker Pattern

Problem
In modern distributed systems, APIs often face the challenge of handling failures gracefully. When an API service experiences high latency or becomes unavailable, it can lead to cascading failures, overwhelming the system and degrading the user experience. To manage this, systems need a way to fail fast and recover gracefully, preventing system-wide outages.
Solution with Code
The circuit breaker pattern is a design pattern used to detect failures and encapsulate the logic of preventing a failure from constantly recurring. It is particularly useful for handling transient failures in distributed systems.
Below is a basic implementation of a circuit breaker pattern in JavaScript:
class CircuitBreaker {
constructor({ successThreshold, failureThreshold, timeout }) {
this.successThreshold = successThreshold;
this.failureThreshold = failureThreshold;
this.timeout = timeout;
this.state = 'CLOSED';
this.failureCount = 0;
this.successCount = 0;
this.nextAttempt = Date.now();
}
async call(request) {
if (this.state === 'OPEN') {
if (this.nextAttempt <= Date.now()) {
this.state = 'HALF-OPEN';
} else {
throw new Error('Circuit is open');
}
}
try {
const response = await request();
return this.success(response);
} catch (error) {
return this.failure(error);
}
}
success(response) {
if (this.state === 'HALF-OPEN') {
this.successCount++;
if (this.successCount > this.successThreshold) {
this.state = 'CLOSED';
this.successCount = 0;
}
}
this.failureCount = 0;
return response;
}
failure(error) {
this.failureCount++;
if (this.failureCount >= this.failureThreshold) {
this.state = 'OPEN';
this.nextAttempt = Date.now() + this.timeout;
}
throw error;
}
}
Key Concepts
-
States: The circuit breaker can be in one of three states:
- Closed: The system is operating normally.
- Open: The system has encountered a failure and prevents subsequent calls.
- Half-Open: The system is testing if the underlying issue has been resolved.
-
Thresholds: Define when the breaker should trip from one state to another:
- Failure Threshold: The number of consecutive failures needed to open the circuit.
- Success Threshold: The number of successful calls needed to close the circuit from a half-open state.
-
Timeout: The period the circuit remains open before transitioning to the half-open state.
Implementing a circuit breaker can significantly increase the resilience and reliability of your API services, allowing systems to self-heal and reduce downtime during partial service outages. This pattern is essential for maintaining high availability in distributed architectures.