BBackend CourseLeroy Labs
0/0
Search the course

Resilience patterns (timeouts, retries, circuit breakers, bulkheads, fallbacks)

Protecting your app from slow or failing dependencies with Resilience4j. Timeouts, retry with backoff and jitter, circuit breaker states, bulkheads, rate limiters and graceful degradation.

● advancedReport-Manager-2SperoTek (in dev)

Why

A dependency (Paystack, HIBP, SMTP, a link-preview target) gets slow. Without protection, every request waits on it, threads and connections fill up, and your whole app goes down. That’s a cascading failure.

The patterns

Pattern Does Your nearest example
Timeout Give up after N ms SperoTek’s 10 s SMTP timeouts
Retry (backoff + jitter) Retry transient failures, not 4xx SperoTek mail retry (1 s, ×3); RabbitMQ listener retry
Circuit breaker Stop calling a failing dependency for a while (not yet)
Bulkhead Isolate resources per dependency SperoTek’s per-module executors
Rate limiter (outbound) Respect a third party’s quotas IR3D’s bounded clip-API executor
Fallback A degraded answer instead of an error RM’s ResilientPublisher
Report-Manager-2shared/messaging/ResilientPublisher.java

A fallback: if RabbitMQ is down, process in-process after commit, so the feature degrades instead of failing.

SperoTek (in dev)app.mail.retry · MailExecutorConfig · HibpBreachedPasswordChecker

Retry with exponential backoff and isolated executors. For the HIBP check, think about the fallback: if HIBP is down, should signup fail closed (block) or fail open (allow with a log)? For a breach check, failing open is usually right.

Circuit breaker states

 CLOSED --(failure rate > 50% over last 20 calls)--> OPEN --(after 30s)--> HALF_OPEN
   ^                                                  | fail fast,          | allow a few trial calls
   |____________________(trials succeed)______________|  no remote call     |--(trial fails)--> OPEN

Resilience4j in Spring Boot

@CircuitBreaker(name = "paystack", fallbackMethod = "transferQueued")
@Retry(name = "paystack")
@TimeLimiter(name = "paystack")
public CompletableFuture<TransferResult> initiateTransfer(Payout p) { /* HTTP call */ }

private CompletableFuture<TransferResult> transferQueued(Payout p, Throwable t) {
    payouts.markPendingRetry(p, t.getMessage());           // reconciliation job will finish it
    return CompletableFuture.completedFuture(TransferResult.queued());
}
resilience4j:
  circuitbreaker.instances.paystack: { slidingWindowSize: 20, failureRateThreshold: 50, waitDurationInOpenState: 30s }
  retry.instances.paystack: { maxAttempts: 3, waitDuration: 500ms, enableExponentialBackoff: true, exponentialBackoffMultiplier: 2,
                              retryExceptions: [java.io.IOException], ignoreExceptions: [com.x.PaymentDeclinedException] }
  timelimiter.instances.paystack: { timeoutDuration: 5s }

Rules

  • Always set timeouts: connect timeout plus read timeout, on every HTTP client (RestClient, WebClient, JavaMail, JDBC statement_timeout).
  • Retry only idempotent or idempotency-keyed operations, and only transient errors (timeouts, 503s, connection resets). Never retry 400 or 401.
  • Add jitter, otherwise all clients retry in sync and create a “retry storm”.
  • Retry budgets. Limit total retries per minute so retries don’t multiply an outage.
  • Monitor breaker state. Resilience4j exports metrics through Micrometer.

Check yourself

1. Circuit breaker in OPEN state does what?
2. Should you retry HTTP 400 responses?
3. Why add jitter to backoff?