Never wait forever: set a limit on every call, and pass the remaining time down the chain.
A timeout is a limit on how long you will wait for an answer. A deadline is the moment by which the whole request must be finished, and it is passed down from service to service.
Every network call can fail in a nasty way: not with an error, but with silence. The other side is slow, the line is cut, or the process is stuck. If your code waits with no limit, a thread (or a connection) is held for as long as the silence lasts. Soon every thread is waiting, and your service stops too. This is how one slow service brings down a whole system.
The first rule is simple: put a timeout on every call. There are three common kinds: a connect timeout (how long to wait to open the connection), a read or response timeout (how long to wait for the answer) and an overall timeout for the whole request, retries included.
The second rule is for chains. If the client waits 3 seconds, and service A calls B, and B calls C, each with its own 2-second timeout, then A and B may keep working for seconds after the client has left. A deadline fixes this. The client says "I need an answer by 3 s from now." Every hop passes on the remaining time, and stops when it runs out.
| Timeout | What it limits | Typical problem it solves |
|---|---|---|
| Connect timeout | Time to open the connection (handshake) | A server that is down or unreachable. This should be short. |
| Read / response timeout | Time waiting for data after the request was sent | A server that accepted the call but is stuck or slow |
| Write timeout | Time to send the request body | A very slow client or network |
| Overall (total) timeout | The whole call, with retries and waits included | Many small waits that add up to a long one |
| Idle timeout | How long a quiet connection may stay open | Connections kept open for no reason, or cut by a firewall |
| Timeout | Deadline | |
|---|---|---|
| It is | A length of time: wait up to 2 s | A point in time: finish by 12:00:03.500 |
| Set by | Each caller for its own call | The first caller. Then it is passed on. |
| Each hop | Starts a fresh clock | Uses the time that is left |
| After the caller leaves | Lower services may carry on | Lower services see the time is up and stop |
| Best for | A single call to one dependency | Chains of calls through many services |
A simplified, deterministic model of the chain client, A, B, C. C always needs the same time, network delay between hops is ignored, and the client does not retry. With fixed timeouts, nobody cancels a call after its caller has given up. All times are illustrative.
| Term | Meaning in easy words |
|---|---|
| Timeout | The longest time you will wait for something before giving up |
| Connect timeout | A limit on how long opening a connection may take |
| Read timeout | A limit on how long to wait for the answer after sending a request |
| Deadline | The point in time by which the whole request must be finished |
| Deadline propagation | Passing the remaining time to every service down the call chain |
| Time budget | The time still available for the rest of the request |
| Cancellation | Stopping work in progress because its result is no longer needed |
| Cascading failure | One slow or failed service causing its callers, and then their callers, to fail |
| Retry budget | A limit on how many retries are allowed, so retries do not flood a weak service |
| Fail fast | Return an error quickly instead of waiting a long time for a likely failure |
| Tail latency (p99) | The slowest 1% of requests, often used to choose a timeout |
| Choice | We gain | We lose |
|---|---|---|
| Short timeout | Fast failure, free resources | More false failures on a slow day, and more retries |
| Long timeout | Fewer false failures | Threads held longer, and slow failures spread |
| Fixed timeout per hop | Simple to set up | Work continues after the caller has left, and the totals do not fit |
| Shared deadline | No waste after the caller leaves, and a total that fits | Every service must pass it on and respect it |
| Retries inside the deadline | A chance to recover from a short fault | Less time per try, and more load on the dependency |
| Cancelling work | Saves resources | Half-done work may need clean-up |