Implementing Resilience Patterns

1. Bulkhead Pattern

AspectDetail
InspirationShip's compartments — flooding contained
MechanismIsolate resources (threads, connections) per dependency/tenant
BenefitOne slow dependency can't exhaust all threads
ToolsResilience4j Bulkhead, separate connection pools

2. Bulkhead Pool Configuration

ParamTypical
maxConcurrentCalls10-50 per dependency
maxWaitDuration0 (fail fast) or short (100ms)
queueCapacity0 or small for thread pool bulkhead
typeSemaphore (lighter) or ThreadPool (better isolation)

3. Retry Pattern

AspectDetail
WhenTransient failures only (5xx, timeouts, 429)
Never Retry4xx (validation, auth), non-idempotent without idempotency key
BackoffExponential with jitter
Max Attempts3-5 typical

Example: Exponential Backoff + Jitter

RetryConfig cfg = RetryConfig.custom()
    .maxAttempts(4)
    .intervalFunction(IntervalFunction.ofExponentialRandomBackoff(
        Duration.ofMillis(200), 2.0, 0.5))
    .retryOnException(e -> e instanceof IOException || e instanceof TimeoutException)
    .build();
Warning: Naive retries cause retry storms during outages. Always use jitter and cap retries — and disable retries for upstream errors that came from a downstream retry.

4. Retry Strategy Configuration

StrategyFormulaUse
Fixeddelay = cSimple polling
Lineardelay = n × cMild backoff
Exponentialdelay = c × 2^nMost common
Exp + Jitterdelay = rand(0, c × 2^n)Best — avoids thundering herd
Decorrelated Jitterdelay = rand(c, prev × 3)AWS-recommended

5. Timeout Pattern

LayerTypical Value
Connect Timeout1-2s
Read/Request Timeout500ms - 5s (per-call)
Total DeadlineSet at edge; propagated downstream
DB QueryStatement timeout (e.g., Postgres statement_timeout)

6. Fallback Pattern

StrategyExample
CachedReturn last-good cached value
DefaultEmpty array, neutral value
StubHardcoded safe response
Alternative SourceSecondary service / read replica
Graceful DegradationHide feature in UI

7. Fail Fast Pattern

AspectDetail
PrincipleReject immediately rather than queuing/blocking
TriggersOpen circuit, full bulkhead, exceeded rate limit
ResponseHTTP 503 with Retry-After
BenefitFrees resources; signals client to back off

8. Graceful Degradation Pattern

LayerStrategy
UIHide non-essential widgets if backend down
APIReturn partial response with errors in payload
ServiceUse cache instead of live data
DBRead-only mode if writes failing

9. Compensating Transaction Pattern

AspectDetail
Use Within ResilienceUndo partial work after non-recoverable failure
Differs From RollbackLogical undo (refund) not transactional
TriggerSaga failure, downstream irreversible failure

10. Shed Load Pattern

MechanismDetail
Detect OverloadQueue depth, latency p99, CPU/mem
RejectDrop low-priority requests with 503
PrioritizePremium / health checks pass through
AdaptiveUse TCP-Vegas-like control to find load ceiling