Retry policy
A retry policy shared by every outbound HTTP client in the service. It must let a dependency have a bad minute without turning it into a bad hour, and it must never retry a request that cannot succeed.
Five hundred clients see the same dependency fail at the same instant, and all retry after exactly two seconds. What is wrong?
- Two seconds is too short a delay
- They retry in unison, hitting the recovering dependency with a synchronised wave
- Exponential backoff is missing
- Retries should be immediate
A fixed delay preserves the synchronisation the outage created, so the dependency takes a full-load spike at the exact moment it is trying to recover — then again at four seconds, then eight. Jitter, randomising within the window, is what breaks the herd apart, and it matters more than which backoff curve you picked.
Assign each responsibility.
- Run the call, wait, run it again, and eventually give up
- RetryExecutor
- Compute how long to wait before attempt number n
- Backoff
- Decide whether this particular error is worth retrying at all
- FailureClassifier
- Hold the attempt limit, the base delay and the overall deadline
- RetryPolicy
- Add randomness so callers do not retry in lockstep
- Backoff
Classification is its own object because it is the part that differs per dependency and gets edited most — a newly discovered error code should never mean touching the retry loop. Keeping backoff separate makes fixed, exponential and jittered schedules interchangeable, which is Tier 2 Strategy pattern doing real work.
Which failure should the policy refuse to retry?
- A 503 Service Unavailable
- A connection timeout
- A 422 rejecting the request body
- A 429 with Retry-After
A 422 means the request itself is invalid, so every attempt returns the same answer — retrying burns the budget and delays the error the caller actually needs to see. The classifier exists to separate "the dependency is temporarily unwell" from "this will never work", and only the first is a retry.
The gateway, the service and the client each retry three times. One dependency fails. How many requests does it see?
- 3
- 9
- 27
- It depends on the backoff curve
Retries multiply through every layer that has them: three layers of three attempts is twenty-seven requests for one user action, so a dependency already struggling receives an order of magnitude more traffic at its worst moment. The fixes are a retry budget — cap retries as a fraction of total traffic — and retrying at one layer only, usually the one closest to the failure.