Nginx Timeouts and Retries: Bound Failure Recovery
Three upstream timeout phases
A connection timeout concerns establishing the upstream connection. A send timeout watches progress while Nginx writes the request to the upstream. A read timeout watches progress receiving the upstream response. The send and read limits are inactivity intervals, not a total request budget.
proxy_connect_timeout 2s;proxy_send_timeout 5s;proxy_read_timeout 3s;Example values are for a tiny local app. A healthy response can take longer than three seconds if upstream reads continue frequently. A connect refusal can fail immediately instead of waiting for the configured connect limit.
Trace a slow response
Consider a response whose first byte arrives after one second and remaining bytes arrive at two-second intervals. With a three-second read timeout, no single gap exceeds the limit, though the response can last much longer.
t=0 request sentt=1 first response bytest=3 next bytest=5 final bytesread timeout=3s; total=5sA pause greater than the timeout before any response header can result in a gateway timeout if no retry succeeds. If the response has already started reaching the client, the proxy cannot replace it with a fresh error page or retry transparently; the client may see a partial response. Implement true end-to-end deadlines at the application or appropriate request boundary.
Retry only when the operation allows it
For a small read-only catalog route, a second eligible peer may be useful when the first cannot respond. Limit attempts and the retry eligibility window. This does not guarantee two peers are available or bound the duration of a final attempt.
proxy_pass http://catalog_pool;proxy_next_upstream error timeout;proxy_next_upstream_tries 2;proxy_next_upstream_timeout 3s;An attempt count of two includes the initial attempt. A retry window limits moving to another peer, rather than cancelling every in-flight operation at the deadline. Once downstream response bytes have been sent, switching peers is no longer possible. HTTP status-based retries are explicit; do not retry arbitrary 4xx or 5xx responses indiscriminately.
Avoid duplicate effects
A write can succeed at the backend even if its response is lost. For example, an order might be saved before a connection fails. Replaying a POST can create another order. Nginx normally restricts retries of POST, LOCK and PATCH once sent; do not opt into non_idempotent as a generic recovery strategy.
proxy_pass http://catalog_pool;proxy_next_upstream off;Disable automatic upstream retries for this lab write route; the application still needs idempotency design and reconciliation for ambiguous outcomes, since clients can retry independently. Diagnose with private upstream status and timing logs; use bounded retries, backoff at the appropriate layer and a workload-wide retry budget. Do not mask an overloaded backend by indefinitely raising timeouts.
Practice
A read-only route has read timeout 3s and two attempts. Peer A does not send headers for four seconds. Peer B is eligible. A separate POST may already have saved an order before the response is lost. Explain what may happen on both routes and why the retry window is not a deadline.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Nginx Timeouts and Retries: Bound Failure Recovery
Distinguish inactivity timeouts from deadlines and choose a safe retry policy.
What does proxy_read_timeout measure?
Can a 10-second response succeed with a 3-second read timeout?
What is the maximum total attempt count with tries 2?
When response bytes have already reached the client, can a new peer transparently replace them?
What should a lost response after a possible order commit trigger?