408 vs 504: What's the Difference?
- 408 Request Timeout — The server waited too long for the client to finish sending the request. The client was too slow.
- 504 Gateway Timeout — A proxy/gateway waited too long for the upstream server to respond. The server was too slow.
Debugging 504 Gateway Timeout
504 is far more common in production. The chain looks like:
Client → CDN/LB → Reverse Proxy → App Server → Database
The timeout can occur at any hop.
Step 1: Identify the Slow Component
Check response times in your application logs. Is the database query slow? Is an external API call taking too long?
Step 2: Check Timeout Configuration
Each component has its own timeout. The innermost timeout should be shortest:
- Database query timeout: 30s
- App server request timeout: 60s
- Reverse proxy (Nginx): 90s
- CDN/Load balancer: 120s
Step 3: Fix the Root Cause
- Slow queries: Add indexes, optimize queries, add caching
- External API calls: Add timeouts, implement circuit breakers
- Heavy processing: Move to background jobs/queues
Debugging 408 Request Timeout
Less common. Usually caused by:
- Client on a very slow network
- Very large file uploads on slow connections
- Client-side bugs not sending the request body