Calling another service means it will sometimes be slow or fail. How a program reacts decides whether a small fault stays small.
01 · Start with a timeout
Every call to something else needs a limit on how long to wait.
Without a timeout, a slow dependency holds a thread, a connection and a user's patience for as long as it likes. Enough of those and the caller is down too, though nothing is wrong with it.
Set the limit from what the caller can afford, not from what the dependency usually takes. A sensible limit is a little beyond the normal worst case, so that a stuck call is cut off while a slow one still completes.
02 · Retry carefully
Only retry what can safely be repeated, and only some failures.
A retry is worthwhile for a temporary fault: a timeout, a dropped connection, a busy server. It is pointless for a request that is wrong, such as a bad parameter or a missing permission, which will fail identically every time.
Retrying an action that changes something, like a payment, needs a way to make repeats safe, usually a unique request identifier the other side recognises. This is the same idea as an idempotent job.
03 · Space them out
Wait longer each time, and add randomness.
If every client retries immediately, a service that is struggling receives more load exactly when it can least cope. Exponential backoff waits, say, one second, then two, then four, so the pressure falls away.
Add jitter, a random amount of that wait. Without it, a thousand clients that failed together retry together, in synchronised waves. With it, the retries spread out.
A retry is a request too. Treat it as extra load on someone else's system.
04 · Know when to stop
Cap the attempts, and give up gracefully.
- Cap the numberThree to five tries is usually plenty. Retrying forever just hides an outage until it is large.
- Respect the signalIf the service says how long to wait, for example with a Retry-After header, wait that long.
- Stop calling a broken serviceA circuit breaker pauses calls after repeated failures and tests the service again later, so the caller fails fast instead of piling up.
- Show the user the truthWhen the work cannot be done, say so plainly. A clear failure is better than a spinner that never ends.
05 · Worked example
A backoff schedule, and why layers multiply.
A client retries after 1, 2, 4 and 8 seconds, each wait randomly varied by up to half. It gives up after about 15 seconds in total, having made five calls. A thousand clients that failed together are now spread across several seconds, not hitting the service in the same instant.
Layers multiply. If a page calls a service that calls a database, and each layer makes three tries, one slow database query can become twenty-seven calls. Retry in one layer, usually the one closest to the user, and let the others fail fast.
06 · Pitfalls
Common retry mistakes.
- Retrying a bad requestA wrong parameter or missing permission will fail every time. Retry only temporary faults, such as timeouts and busy-server responses.
- No upper limitWithout a cap, a retry loop hides an outage until it is large.
- No timeout at allThe default in many libraries is to wait forever. Set one explicitly.
- Retrying a payment blindlyWithout a unique request identifier, a retry can charge twice.
- Hiding failure from peopleAfter the retries, tell the user clearly what happened and what they can do.
What I took away
- Put a timeout on every outside call.
- Retry only temporary faults, and only where repeating is safe.
- Back off exponentially with jitter, cap the attempts, and fail clearly.