How to Fix HTTP 408 Request Timeout Error
In this tutorial, you'll learn about How to Fix HTTP 408 Request Timeout Error. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
Your API client receives:
HTTP/1.1 408 Request Timeout
Or:
408 Request Timeout: The server timed out waiting for the request.
The client took too long to send the complete request, or the server-side processing exceeded the timeout limit.
Quick Fix
1. Increase the client timeout
The client side may have a timeout that is too short:
// Node.js with fetch — set timeout
const response = await fetch(url, {
signal: AbortSignal.timeout(30000) // 30 seconds
})
// Axios — increase timeout
const response = await axios.get(url, { timeout: 30000 })
// Python requests
response = requests.get(url, timeout=30)
2. Optimize server response time
The server may be too slow to respond within the timeout:
// Express.js — set server timeout
server.timeout = 120000 // 2 minutes (Node.js http.Server)
// Express.js with req.setTimeout
app.use((req, res, next) => {
req.setTimeout(60000) // 1 minute per request
next()
})
3. Check network latency
High latency between client and server can cause timeouts:
# Measure round-trip time
ping -c 10 api.example.com
# Measure TCP connection time
curl -w "TCP handshake: %{time_connect}s\nTotal: %{time_total}s\n" -o /dev/null -s https://api.example.com
If latency is high, move the server closer to the client or use a CDN.
4. Check server proxy timeout
A reverse proxy (Nginx, HAProxy) may have a lower timeout than the application:
# Nginx — increase proxy timeout
location /api/ {
proxy_pass http://backend:3000;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
}
Restart Nginx after changes:
sudo nginx -s reload
5. Check the load balancer timeout
AWS ALB, GCP HTTP(S) Load Balancer, and others have default timeouts:
- AWS ALB: 60 seconds idle timeout
- GCP HTTP LB: 30 seconds (configurable up to 10 minutes)
- Azure App Gateway: 20 seconds
Increase these in the cloud provider settings for long-running requests.
6. Stream large requests
For large uploads or responses, use streaming instead of buffering:
// Node.js — stream the request body instead of buffering
const fs = require('fs')
const http = require('http')
const fileStream = fs.createReadStream('large-file.zip')
fileStream.pipe(http.request({
hostname: 'api.example.com',
path: '/upload',
method: 'POST'
}))
Prevention
- Set client timeouts to at least 30-60 seconds for production APIs.
- Monitor API response times and set alerts for slow endpoints.
- Use async processing for long-running operations (return 202 Accepted).
- Document timeout values in your API specification.
- Test API behavior under high latency conditions.
Common Mistakes with 408 timeout
- Misunderstanding that
Stringis[Char]with poor performance for large text operations - Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging
These mistakes appear frequently in real-world API code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro