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:

  1. Retry in only one layer. Usually the outermost caller or the lowest one, not all of them.
  2. 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.
  3. Backoff + jitter. 100ms, 200ms, 400ms plus a random extra on each, otherwise every client retries at the same moment.
  4. A retry budget. For example, retries must not exceed 10% of total requests. Resilience4j and Envoy have this.
  5. 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.