Timeouts y Deadlines

Propagación del deadline

Escrito por Darjus Kazakevic · revisado el

Every network call needs a time limit, and it must fit the caller's.

A timeout is the longest you wait for a call before you give up. Without one, a slow dependency holds your thread, socket and memory until it answers, which may be never. Many HTTP and database clients have no limit by default, or one of several minutes.

A deadline is a timeout that travels with the request. The first service sets the total time the user can wait, and every hop below spends from that same budget. The cost is a choice per call: too short fails good requests, too long holds resources for answers nobody reads.

En esencia — Give every remote call a time limit taken from real latency, and pass the remaining deadline down, so no service works longer than its caller waits.

Cuándo recurrir a él

Recurre a él cuando

  • Every call that crosses a network: HTTP, gRPC, database, cache, queue
  • A request passes through several services before it can answer
  • Users wait on the answer, so slow is the same as broken

Reconsidéralo cuando

  • Pure in-process work, which cannot hang on a network
  • Long-lived streams such as WebSockets, which need an idle timeout instead
  • A long batch step, where one job-level limit fits better than per-call ones

Conceptos clave

Connect timeout
How long to wait to open the connection (the TCP and TLS handshake). Inside one datacenter this takes a few milliseconds, so 100–500 ms is plenty. A host that does not accept the connection is down or unreachable.
Read timeout (Response timeout)
How long to wait for the answer after the request is sent. It covers the server's real work, so it is longer than the connect timeout. When it fires, you do not know whether the work was done.
Choosing a value from p99
Measure the dependency and start a little above its p99 (the time within which 99% of calls finish). Then check how many good calls that limit would have cut, and review it when latency changes.
Deadline propagation (gRPC deadlines)
The caller sends the time it has left with the request. Each hop subtracts the time it spent and passes the rest on. gRPC does this for you with the grpc-timeout header.

Reglas de diseño

Make each timeout shorter than its caller's
If the gateway gives up at 2 s and the service below waits 5 s, the last 3 s are work nobody receives. Each layer's limit must fit inside the time its caller still has left.
Set a timeout on every remote call
Defaults are too long or missing: Node's fetch waits up to 300 s for headers, and Python's requests waits forever. Set connect and read timeouts on every HTTP, database and queue client.
Take the value from measured latency
Start a little above the dependency's p99 and review it when latency changes. A guessed value is either too long to protect you or short enough to fail good calls.
Treat a timeout as an unknown result
A write that timed out may still have happened. Retry it only if it is idempotent, or check its status first. Never assume it failed.

Errores habituales

  • Relying on client defaults. The symptom is threads stuck for minutes on one slow host while the pool is empty.
  • A backend timeout longer than the frontend's. The user sees an error at 2 s, but the database keeps working for 30 s and adds load during the incident.
  • Only a connect timeout. The connection opens fast, the server then stops sending, and the read waits forever.
  • Retries with no total deadline. Three tries of 2 s each, plus backoff, turn a 2 s limit into more than 6 s.