Resilience Patterns
In distributed systems, failures are inevitable. Networks lag, services crash. Your system must remain responsive even when parts of it are broken. We use Resilience4j, a lightweight fault tolerance library designed for Java 8 and functional programming.1. The Circuit Breaker Pattern
IfInventory Service is slow or down, Order Service keeps waiting for it, consuming threads. Eventually, Order Service runs out of threads and crashes too (Cascading Failure).
A Circuit Breaker detects failures and “opens the circuit”, failing fast immediately without waiting for the timeout, giving the downstream service time to recover.
States:
- CLOSED: Normal operation. Requests pass through.
- OPEN: Too many failures. Requests fail immediately.
- HALF-OPEN: Testing if the service is back online. Lets a few requests through.
2. Implementation
add dependency:spring-cloud-starter-circuitbreaker-resilience4j.
application.yml):
3. Retry Pattern
For transient failures (temporary network blip), it makes sense to try again.4. Rate Limiting
Prevent one user or service from overwhelming your system.5. Bulkhead Pattern
Isolate resources. If one part of the system is exhausted, others shouldn’t be affected. It creates separate thread pools for different calls. If the “Image Processing” thread pool is full, the “User Login” thread pool still works fine.Summary:
- Circuit Breaker: Stop calling a dead service.
- Retry: Try again for temporary glitches.
- Rate Limiter: Control traffic flow.
- Bulkhead: Isolate failures.