Video in progress
When a dependency slows down, retries bring the whole system down. How do you prevent it?
Retries multiply load: if each layer retries 3 times, one request becomes 27 across 3 layers. Short timeouts, exponential backoff with jitter, a retry budget and a circuit breaker have to work together.
📖 Answer
A classic retry storm: the payment service slows down by 2 seconds. The gateway retries 3 times, the order service also 3 times, and the client inside it also 3 times. The slow service gets dozens of times the normal load and can no longer recover at all.
What I do:
- Retry in only one layer. Usually the outermost caller or the lowest one, not all of them.
- Hierarchical timeouts. An inner call's timeout must be smaller than the outer one's. If the gateway waits 2 s but there is a 5 s timeout inside, the inner work keeps going for nothing.
- Backoff + jitter.
100ms, 200ms, 400msplus a random extra on each, otherwise every client retries at the same moment. - A retry budget. For example, retries must not exceed 10% of total requests. Resilience4j and Envoy have this.
- A circuit breaker. When the error rate rises, stop calling right away and return a fallback, which gives the service room to breathe.
resilience4j:
circuitbreaker:
instances:
payment:
failureRateThreshold: 50
slowCallDurationThreshold: 1s
slowCallRateThreshold: 60
waitDurationInOpenState: 10s
retry:
instances:
payment:
maxAttempts: 2
enableExponentialBackoff: true
enableRandomizedWait: true
And most important: retry only idempotent operations. Retrying POST /payments without an idempotency key means charging the money twice. Don't retry 4xx responses either; they won't fix themselves.
Sign up to mark studied questions and save your progress.