Every network call can hang. Without a timeout, a hung call holds a thread, a connection or a slot in a pool until something breaks, and when a dependency slows down, every caller fills up with waiting requests. Timeouts are the cheapest resilience mechanism there is, and the most often forgotten.
Kinds of timeouts
| Timeout | Bounds | Typical value |
|---|---|---|
| Connect | Establishing the TCP (and TLS) connection | 1 to 3 s |
| Read / response | Waiting for the response after connecting | From the dependency's p99, plus margin |
| Overall / deadline | The whole operation including retries | What the caller can afford |
| Idle | How long an unused connection stays open | Aligned between client, proxies and server |
Defaults to distrust
Many clients have no timeout, or a very long one, by default. Some common examples: Go's http.Client{} has no timeout; Python requests waits indefinitely without a timeout argument; many database drivers have no statement timeout. Assume nothing; set timeouts explicitly everywhere.
Choosing values
Base read timeouts on measured latency: if a dependency's p99 is 300 ms, a timeout of around 1 second catches genuine hangs without cutting off normal slow requests. Then check the budget from the top: if your API promises a response within 2 seconds, the sum of sequential calls and their retries must fit inside it.
Deadline propagation
A request that has already used 1.8 of its 2 seconds should not start a call with its own 1-second timeout. Pass the remaining time down:
func handler(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second) // the request's budget
defer cancel()
price, err := pricing.Get(ctx, sku) // uses the remaining deadline
if errors.Is(err, context.DeadlineExceeded) {
http.Error(w, "pricing timed out", http.StatusGatewayTimeout)
return
}
// ...
}
gRPC propagates deadlines automatically; with HTTP you can pass a header with the remaining budget, or use per-call timeouts derived from it.
Layer timeouts must nest
The outer layer should wait longer than the inner one, so the inner layer can fail cleanly and return a useful error:
CDN 100 s > load balancer 60 s > Nginx 55 s > app request 30 s > DB statement 25 s
When the order is wrong, the outer layer gives up first and returns a generic 504, while the inner work continues uselessly.
Database statement timeouts
Set a statement timeout per role or session (SET statement_timeout = '5s' in PostgreSQL), so a runaway query cannot hold locks and connections for minutes. Long-running reports can use a separate role with a longer limit.
Worked example
An API called three services in sequence: inventory, pricing and tax, each with a 10-second timeout and two retries. When the tax service hung, each API request waited up to 30 seconds on tax alone, gunicorn workers filled, and the whole API stopped responding. New settings: 2-second request budget, inventory 400 ms, pricing 400 ms, tax 600 ms, one retry only where the budget allowed, and a cached tax rate as a fallback. The next tax-service incident added 600 ms to a fraction of requests and nothing else.
Observing timeouts
In traces, a timed-out client span shows the duration hitting the limit and an error status; alert on the dependency's timeout rate rather than individual timeouts. Watch p99 latency approaching the timeout, which predicts timeouts before they happen. VitalRix APM shows outgoing calls per dependency with latency percentiles and errors in each trace. Related: ETIMEDOUT and circuit breakers.
Checklist
- Explicit connect and read timeouts on every client.
- Values from measured percentiles, not guesses.
- A request budget that inner calls respect.
- Outer layers wait longer than inner ones.
- Statement timeouts on the database.
- Alerts on timeout rates and p99 approaching limits.
Common mistakes
- Relying on library defaults that are infinite or minutes long.
- The same timeout everywhere, ignoring each dependency's normal latency.
- Retries that exceed the caller's budget: three attempts of 2 seconds inside a 3-second request.
- Outer timeouts shorter than inner ones, producing generic 504s while work continues behind them.
- No timeout on acquiring a pool connection, so requests wait forever when the pool is exhausted. See connection pool exhausted.
A quick audit
List every outgoing call in a service: HTTP clients, database, cache, queue, external APIs. For each, write down the connect timeout, read timeout and retry policy. Gaps on that list are the next incident.
What is a good default timeout?
There is no universal value. Start from the dependency’s p99 latency plus margin, and make sure the total fits your own response budget.
Should timeouts be the same everywhere?
No. Each call has its own normal latency, and outer layers need longer timeouts than inner ones.
Do timeouts cancel work on the server?
Not automatically. The server may keep working. Propagate deadlines or cancellation so servers can stop early.
Try VitalRix free
Uptime monitoring, APM, logs, real user monitoring and status pages in one place. No credit card needed.