Sub topic 9
Client-Side Timeouts, Retries, and Endpoint Lists
This chapter assumes you know basic client-server architecture, what a load balancer does, and what a timeout is. Load balancers and DNS move traffic away from failed servers, but they are not always fast or available. The client can also take part in failover. A client with a list of endpoints, a short timeout, and a bounded retry policy can survive a failed server without waiting for the infrastructure to react.
Why the client matters
Traffic failover through a load balancer or DNS has delays. Health checks need several failed probes before they act, and DNS caches keep old answers until the TTL expires. During that window, some requests still go to the dead server.
The client sees each failure directly. If it handles errors well, users notice little. If it does not, one dead server can freeze a whole application.
Timeouts decide how fast you notice
A call to a dead server does not fail on its own. Without a timeout, the client can wait for minutes. Set a timeout on every network call. Use two kinds if your library supports them:
- A connect timeout limits how long the client waits to open a connection. It can be short, such as 1 second, because connecting is normally quick.
- A read timeout limits how long the client waits for a response after connecting. Base it on the slowest normal response, not the average.
A timeout that is too short causes false failures on a slow but healthy server. A timeout that is too long makes users wait. This is the same speed-versus-false-positive trade-off as in health checks.
Endpoint lists
An endpoint list gives the client more than one address for the same service. The client tries the first address and moves to the next on failure. The example below uses a 1-second timeout and moves on after a connection error, a timeout, or an HTTP error status.
import urllib.request
ENDPOINTS = [
"http://primary.example.internal:8080",
"http://secondary.example.internal:8080",
]
def fetch(path, timeout=1.0):
last_error = None
for base in ENDPOINTS:
try:
with urllib.request.urlopen(base + path, timeout=timeout) as resp:
return resp.read()
except OSError as err:
last_error = err
raise RuntimeError("all endpoints failed") from last_errorurllib raises HTTPError for 4xx and 5xx responses, and HTTPError is a subclass of OSError. That is why one except OSError clause covers connection errors, timeouts, and error statuses. Be careful with this in real code. A 404 or 400 means the request is wrong, and a second server will give the same answer. Retry only on errors that another endpoint could fix, such as connection failures, timeouts, and 502 or 503.
Retries need limits
Retrying the same endpoint can help with brief glitches. Unlimited retries cause harm. If a server is overloaded and every client retries at once, the retries add load and keep the server down. This is called a retry storm.
Apply these limits:
- Cap the number of attempts, for example 3 in total.
- Wait between attempts, and increase the wait each time (exponential backoff).
- Add random jitter to the wait so clients do not retry in sync.
- Set an overall deadline for the whole operation, not only for each attempt.
Common errors
| Mistake | Effect |
|---|---|
| No timeout | One dead server blocks the client indefinitely |
| Retrying every error | Bad requests are repeated and load grows |
| Retrying without backoff | Retry storm against a struggling server |
| Always starting at the first endpoint | Every request pays the timeout while the primary is down |
| Retrying non-idempotent writes | Duplicate side effects |
The fourth mistake is easy to miss. If the primary is down, each call waits one full timeout before reaching the secondary. A better client remembers that the primary failed and skips it for a short period.
Exercise
A client has a 5-second connect timeout and two endpoints. The primary is down and the client always tries it first. How long does each request take at minimum, and what two changes would reduce it?
Show answer
Each request waits at least 5 seconds for the primary to time out before trying the secondary. Lower the connect timeout (for example to 1 second) and remember recent failures so the client skips the primary for a short period.