01Rate limiting02Databases03Caching04Flash sale05Idempotency06Circuit breaker07Queues & DLQ

Lab / 06 · Circuit breaker

Break a downstream service and watch it fail fast, then recover.

When an external microservice slows to a crawl, uncapped timeouts exhaust your application's thread pool and bring down the whole system. A circuit breaker fails fast in <1ms, returns fallback data, and tests recovery via half-open probes.

LIVE · Resilience4j State Machine · 60% Threshold
Circuit State: CLOSEDDownstream: HEALTHY
Current Circuit StateCLOSED

Circuit is CLOSED. All requests pass through to downstream service normally.

Sliding Window (Last 5 Calls):
·
·
·
·
·
// Circuit Breaker Execution Log
Send requests or break downstream service to watch the state transitions.

On the live site, requests hit real Spring Boot endpoints managing thread-safe sliding windows and automatic probe timers.

Circuit Breaker State Machine

CLOSED (normal traffic) → trips to OPEN when failures exceed 60% → transitions to HALF-OPEN after 5s wait duration to probe recovery.

Client Request
API Traffic
Circuit Breaker
CLOSED / OPEN / HALF_OPEN
Fallback Handler
Graceful cached response
Downstream Service
External dependency
OPEN State: Fails fast in <1ms without blocking Tomcat threads | CLOSED: Normal 200 OK

Resilience4j Circuit Breaker Config

@Bean public CircuitBreaker customCircuitBreaker(CircuitBreakerRegistry registry) { CircuitBreakerConfig config = CircuitBreakerConfig.custom() .slidingWindowSize(5) .failureRateThreshold(60.0f) .waitDurationInOpenState(Duration.ofSeconds(5)) .permittedNumberOfCallsInHalfOpenState(2) .automaticTransitionFromOpenToHalfOpenEnabled(true) .build(); return registry.circuitBreaker("downstreamService", config); }
Full source on GitHub →

Trade-offs I weighed

Time-based vs Count-based Sliding Window

Count-based windows (e.g. last 10 calls) react predictably under burst traffic, while time-based windows (e.g. last 30s) are ideal for services with low, steady call volumes.

Fallback Data vs Clean Fast-Fail Error

Returning cached, degraded data (e.g., product recommendations without personalized rankings) keeps users engaged, while critical payment operations must fail fast with clean 503s.

Thread Pool Isolation (Bulkhead) + Circuit Breakers

Pairing a circuit breaker with a dedicated bulkhead thread pool ensures that even in the brief window before a breaker trips, a slow downstream cannot consume all container threads.

← ← 05 Idempotent paymentsNext: 07 Queues & retries → →