Handling Request Timeouts
1. Setting Server-Side Timeout Limits
| Layer | Typical Timeout |
| Load balancer | 30-60s |
| Reverse proxy (nginx) | 30-60s |
| Application server | 15-30s |
| Database query | 2-10s |
| External HTTP call | 3-10s |
Note: Outer layers should have higher timeouts than inner layers to allow graceful 504 responses.
2. Implementing Client Timeout Configuration
| Timeout Type | Purpose |
| Connection timeout | TCP handshake (1-5s) |
| Read timeout | Wait for response body chunks (10-30s) |
| Write timeout | Send request body |
| Total request timeout | End-to-end cap |
3. Handling Timeout Errors
| Status | Cause |
| 408 Request Timeout | Client took too long to send request |
| 504 Gateway Timeout | Upstream service didn't respond |
| 503 + Retry-After | Service overloaded; client should back off |
4. Implementing Request Cancellation
| Mechanism | Detection |
| HTTP/1.1 client disconnect | Server detects via socket close |
| HTTP/2 RST_STREAM | Cancel single stream without closing connection |
AbortController (JS) | Client-side fetch cancellation |
| Cooperative cancellation | Check req.aborted in long handlers |
| Header | Purpose |
Keep-Alive: timeout=30 | Persistent connection idle timeout |
Prefer: wait=10 | Client willing to wait 10s for async op |
X-Request-Timeout: 5000 | Custom: client desired timeout (ms) |
6. Implementing Graceful Timeout Handling
| Action | Goal |
| Cancel in-flight DB query | Free resources |
| Release locks | Avoid deadlocks |
| Log with traceId | Debug correlation |
| Return 504 with detail | Inform client |
7. Setting Database Query Timeouts
| DB | Setting |
| PostgreSQL | SET statement_timeout = 5000 |
| MySQL | SET max_execution_time=5000 |
| MongoDB | maxTimeMS(5000) |
| JDBC | Statement.setQueryTimeout(5) |
8. Handling Slow Third-Party Services
| Pattern | Benefit |
| Aggressive timeout (2-5s) | Fail fast |
| Bulkhead | Isolate thread pools per dependency |
| Cache last good response | Stale-while-error fallback |
| Circuit breaker | Stop calling failing service |
9. Implementing Circuit Breaker for Timeouts
| State | Behavior |
| Closed | Normal; track failure rate |
| Open | Reject immediately (no upstream call) |
| Half-Open | Allow limited probes; succeed → Closed; fail → Open |
| Libraries | Resilience4j (Java), Polly (.NET), opossum (Node) |
10. Logging Timeout Events
| Field | Use |
| endpoint, method | Identify slow operations |
| duration_ms | How long before timeout |
| upstream_service | Which dependency timed out |
| traceId | Correlate distributed trace |
| userId / clientId | Affected callers |