Skip to content

Lesson 8 of 9 · 24m

Timeouts, Retries, and Zero-Downtime Reloads

What each proxy timeout really means, when retrying upstream is dangerous, and how nginx reloads drain workers without dropping traffic.

Most nginx incident reports contain the word timeout, but rarely specify which one. That omission matters. Connecting, sending a request, waiting for a response, and streaming bytes are different operations with different failure modes. Retries add a second problem: trying another backend can save a read-only request or duplicate a payment. These knobs are safety controls, not universal latency cosmetics.

Name the timeout before changing it

proxy_connect_timeout 5s;
proxy_send_timeout 30s;
proxy_read_timeout 60s;
client_body_timeout 30s;
client_header_timeout 15s;

proxy_connect_timeout is time spent establishing the upstream connection. It should usually be short: if a backend cannot connect quickly, another backend or a fast failure is better. proxy_send_timeout and proxy_read_timeout are not total request deadlines; they are idle-time limits between socket operations. A slow but steadily streaming response can exceed an hour even with a 60-second read timeout.

This is why users still complain after you raise a timeout. If the backend stalls completely, a longer timeout only makes them wait longer for failure. Fix the stall or set an application-level deadline.

Retry only what can be retried

proxy_next_upstream error timeout http_502 http_503;
proxy_next_upstream_tries 2;
proxy_next_upstream_timeout 5s;

error includes connection-level failures. timeout means no response in the configured window. HTTP status retries sound useful, but consider semantics: a backend may have accepted and processed the request before returning a broken response. Retrying a non-idempotent POST can create duplicate charges, duplicate emails, or duplicate writes.

A defensible pattern is to restrict aggressive retry behavior to safe methods or specific internal endpoints and keep user-visible mutations conservative. nginx does not know your business semantics; the config must encode them.

Reload is graceful, but not magic

sudo nginx -t && sudo systemctl reload nginx

On reload, the master starts new workers with the new configuration and tells old workers to finish existing connections before exiting. New connections go to new workers. Long-lived WebSockets and streaming responses can keep old workers alive for minutes or hours.

ps -o pid,ppid,etime,cmd -C nginx

If you see old workers persisting after reload, that is often correct behavior, not a stuck process. Do not kill them casually; doing so drops the very connections graceful reload preserved. Config deployments need a worker-drain expectation, especially for realtime systems.

Know the difference between reload and restart

Reload preserves listening sockets and active connections. Restart stops the master and starts a new one, dropping connections and introducing a small bind-and-start window. Restart is occasionally needed for binary upgrades or deep runtime changes, but routine config changes should use reload. If reload fails, read the error log before trying again; repeated reload attempts do not repair missing files or invalid upstream state.

Field notes

  • proxy_read_timeout is idle time, not total request duration — repeat that in every incident review.
  • Set timeouts according to backend service-level objectives, not user impatience.
  • Retry budgets count across the whole request path; multiplying nginx retries by application retries creates amplification.
  • After reload, monitor both new-worker traffic and old-worker draining instead of assuming the change is complete.

Was this lesson helpful?

Sign in to rate