Timeouts, Retries and Idempotency

Topics Covered

Timeouts

Why every remote call needs a timeout

Picking a value

Connect timeouts and request timeouts

Timeouts compose along a call chain

Timeouts do not mean failure

Asymmetric costs guide the choice

Observability for timeouts

Timeouts in asynchronous systems

Timeouts on the server side

A worked example of a timeout budget

Every call across a network is a bet that a reply will come. Timeouts decide how long to hold that bet before cutting losses, retries decide whether to place it again, and idempotency decides whether placing it again is safe. These three tools appear in every distributed system, from a mobile app calling an API to a database replicating its log, and getting them wrong causes some of the most common and damaging outages in production. This lesson treats them as a single design problem, starting with how long to wait.

Why every remote call needs a timeout

A remote call with no timeout can wait forever. If the server is stuck, because it deadlocked, lost its disk or is behind a partition, every caller that reaches it blocks indefinitely. Each blocked call holds resources on the caller: a thread, a connection from a pool, memory for the request and often a lock or a database transaction. Within seconds, a busy service can exhaust its thread pool waiting on one stuck dependency, at which point it stops answering its own callers, who then exhaust their pools. A single bad node deep in a call graph can freeze an entire system this way. Timeouts bound the damage: a stuck dependency costs each caller at most the timeout, and resources are released to serve other work.

Default timeouts are a common trap. Many HTTP clients, database drivers and RPC libraries ship with no timeout or with very long ones, such as several minutes. Production code should set connect and request timeouts explicitly on every client, and code reviews should treat a missing timeout as a bug.

A call with no timeout holds a thread for as long as the dependency is stuck, and a full pool turns one stuck node into a stopped service.

Picking a value

There is no correct timeout, only tradeoffs. A short timeout frees resources quickly and lets the caller fail over or degrade sooner, but it gives up on requests that would have succeeded, wasting the work already done and possibly triggering retries that add load. A long timeout gives slow requests a chance to finish, but ties up resources and makes users wait when the dependency really is broken.

A good starting point is the latency distribution of the dependency under normal load. If the ninety ninth percentile is 80 milliseconds and the ninety ninth point nine percentile is 200 milliseconds, a timeout of around 300 to 500 milliseconds lets almost all healthy requests finish while cutting off the stuck ones. Revisit the value as the dependency changes, and measure how often the timeout fires. A timeout that fires on more than a small fraction of requests is either too short or signals a real problem.

Connect timeouts and request timeouts

Most clients have more than one timeout. A connect timeout bounds the time to establish a connection, which should be short, often tens or hundreds of milliseconds inside a data center, because a host that cannot accept a connection quickly is probably down. A request or read timeout bounds the time waiting for a response once connected, which depends on the work the server does. Some clients also have an idle timeout for pooled connections and a total timeout covering retries. Setting only one of these leaves gaps; for example, a request timeout without a connect timeout can still hang for a minute on a dead host while the operating system retries the connection.

Timeouts compose along a call chain

In a chain of services, timeouts must nest. If a front end gives a request 1 second, and it calls service A with a timeout of 2 seconds, and A calls B with 5 seconds, then the front end gives up long before A and B do, and they keep working on a request nobody is waiting for. Each layer should give its downstream calls less time than it has itself, leaving room for its own work. The cleanest way to enforce this is to pass a deadline with each request, which the last section of this lesson covers.

Timeouts do not mean failure

A timeout tells the caller that it stopped waiting, not that the operation failed. The server may have completed the work a moment later, or may still be completing it. Code that treats a timeout as a definite failure, for example by telling the user their payment failed, will be wrong some of the time. The safe interpretation is that the outcome is unknown. The caller must either find out, by querying the result later, or make the operation safe to retry, which is where idempotency comes in.

Asymmetric costs guide the choice

The right timeout also depends on what happens after it fires. If a timeout leads to a cheap fallback, such as serving a cached result, a short timeout is fine, since giving up early costs little. If a timeout leads to an expensive action, such as failing over a database primary or alerting an on call engineer, it should be long and require repeated evidence, because a false positive is costly. Many systems use two thresholds for exactly this reason: a short one for user facing requests and a longer, more conservative one for decisions that change the system.

Observability for timeouts

Timeouts are invisible unless you measure them. Count timeouts per dependency and per endpoint, record the configured value next to the observed latency distribution, and alert when the timeout rate rises. During incidents, a spike in timeouts to one dependency is often the first clue to where the problem is. Logging the deadline remaining when each request starts also reveals when upstream services are passing down too little time for the work to succeed.

Timeouts in asynchronous systems

Message queues and background jobs need timeouts too, in different forms. A queue consumer that takes a message gets a visibility timeout or a lease; if it does not acknowledge the message within that time, the queue assumes the consumer died and redelivers the message to another consumer. Too short a visibility timeout causes duplicate processing of slow messages; too long delays recovery when a consumer really dies. Long running jobs should extend their lease periodically while making progress, a pattern called heartbeating, so that the timeout reflects actual liveness rather than a guess about job duration.

Timeouts on the server side

Clients are not the only place timeouts belong. Servers should bound how long they hold resources on behalf of a request: a maximum time for a database transaction, a limit on how long a lock may be held, a cap on how long a request may sit in a queue before being discarded, and idle timeouts on connections that stop sending data. A server that enforces these limits protects itself from slow or misbehaving clients, which otherwise can hold connections open for hours and exhaust capacity just as surely as a stuck dependency exhausts a client. Server side limits also make capacity planning possible, because the worst case cost of a request becomes a known quantity.

A worked example of a timeout budget

Imagine a product page that must render within 800 milliseconds. The page service spends about 50 milliseconds on its own work and calls three dependencies in parallel: inventory, pricing and reviews. Because the calls run in parallel, each can be given up to around 600 milliseconds, leaving room for serialization and network overhead. Pricing in turn calls a currency service; if pricing needs 100 milliseconds of its own work, it can give the currency call at most around 450 milliseconds. Written out this way, the budget makes it obvious when a deep call is configured with a longer timeout than the user will ever wait, and it shows where a slow dependency should be made optional, such as reviews, which can be omitted if they are late.

Every remote call needs a deadline

Set connect and request timeouts on every client, choose values from the dependency latency distribution, and make each layer give its downstream calls less time than it has. Treat a timeout as an unknown outcome, not a failure, and measure how often each timeout fires.