Implementing Error Recovery Patterns

1. Using Circuit Breaker Pattern

StateBehavior
ClosedCalls pass through; track failures
OpenFail fast with codes.Unavailable
Half-OpenProbe with limited traffic

Example: sony/gobreaker

cb := gobreaker.NewCircuitBreaker(gobreaker.Settings{
    Name: "user-svc", Timeout: 30*time.Second,
    ReadyToTrip: func(c gobreaker.Counts) bool { return c.ConsecutiveFailures > 5 },
})
_, err := cb.Execute(func() (any, error) { return client.GetUser(ctx, req) })

2. Implementing Retry Logic

AspectDetail
Built-ingRPC service-config retry policy (Section 32)
CustomExponential backoff + jitter
Only idempotentGET/list/idempotent updates

3. Implementing Fallback Strategies

FallbackUse
Cached valueRecent successful response
Default valueSafe minimal result
Degraded serviceReduced functionality

4. Using Bulkhead Pattern

MechanismDetail
Per-dependency goroutine poolLimit concurrency to a downstream
Per-tenant quotaPrevent noisy neighbor
Semaphorechan struct{} sized to N

5. Implementing Timeout Patterns

PatternDetail
Budget propagationParent deadline minus margin per hop
Hedged requestsSend second request after p99
AdaptiveIncrease timeout under sustained latency

6. Handling Partial Failures

PatternDetail
Best-effort batchReturn per-item status
Compensating actionRoll back successful sub-ops on failure
Eventual consistencyOutbox + async retry

7. Using Dead Letter Queues

StepDetail
Retry N timesWith backoff
On final failureEnqueue to DLQ topic
Manual replayOperator tool or scheduled job

8. Implementing Compensating Transactions

Saga StepCompensation
Reserve inventoryRelease inventory
Charge paymentRefund payment
Ship orderCancel shipment

9. Using Health Checks for Recovery

TierAction
Liveness failRestart pod
Readiness failRemove from LB
gRPC Health watchClient switches subchannel

10. Implementing Graceful Degradation

TechniqueDetail
Feature flagsDisable non-critical features under load
Reduced payloadReturn summary instead of full data
Static fallbackServe cached/precomputed content