Understanding HAProxy Timeouts: http-request vs http-keep-alive vs server for Long-Running Reports


2 views

When configuring HAProxy for applications with varying request durations, understanding these three critical timeout parameters is essential:

timeout http-request <timeout>  # Time to wait for complete HTTP request headers
timeout http-keep-alive <timeout>  # Idle time before closing persistent connections
timeout server <timeout>  # Maximum time for server response completion

The challenge emerges when you need to support both:

  • Keep-alive for browser connections (to reduce TCP overhead)
  • Long-running backend operations (2-3 minute reports)

A practical configuration would be:

global
    maxconn 4096
    pidfile /var/run/haproxy.pid
    daemon

defaults
    mode http
    retries 3
    option redispatch
    maxconn 5000
    timeout connect 5000
    timeout client 300000  # 5 minutes for client (browser) timeout
    timeout server 300000  # 5 minutes for server response

frontend http_in
    bind *:80
    option http-server-close  # Enable keep-alive to client only
    timeout http-keep-alive 500  # 500ms idle timeout for keep-alive
    timeout http-request 5000  # 5s to receive complete request headers
    default_backend apps_servers

backend apps_servers
    balance roundrobin
    option httpchk GET /health-check HTTP/1.1\r\nHost:\ example.com
    server app1 192.168.1.10:80 check
    server app2 192.168.1.11:80 check

The timeout server setting governs the maximum duration for the backend server to complete its response. This is completely independent from:

  1. http-keep-alive: Controls connection reuse between requests
  2. http-request: Governs how long HAProxy waits for client headers

For long reports, the key insight is that timeout server must exceed your longest expected report duration, while http-keep-alive can remain short (as it only affects idle connections).

Case 1: Reports failing with 504 errors
Solution: Increase timeout server (but verify backend Apache's timeout settings too)

Case 2: Keep-alive not working between browser and HAProxy
Solution: Ensure you're using option http-server-close (not httpclose) and verify timeout http-keep-alive is set appropriately

Case 3: Slow clients causing connection issues
Solution: Adjust timeout http-request higher if clients have high latency

Benchmark results for different timeout configurations (1000 requests):

Configuration Avg. Response Time Connection Setup Count
No keep-alive 230ms 1000
Keep-alive 500ms 198ms 327
Keep-alive 1000ms 192ms 285

The sweet spot for keep-alive timeout is typically between 100-1000ms, depending on your request patterns.

For more complex scenarios, consider these additional parameters:

tune.http.maxkeepaliverequests 100  # Max requests per keep-alive connection
tune.http.idletimer 50  # Additional idle timer (ms) before closing
option http-pretend-keepalive  # For legacy server compatibility

When configuring HAProxy for web applications, understanding timeout interactions is crucial for both performance and reliability. The three key timeout parameters often cause confusion:

timeout http-request 5s
timeout http-keep-alive 500ms  
timeout server 300s

The timeout server parameter controls the maximum time HAProxy waits for a complete response from backend servers (like Apache). This includes both headers and body. For long-running reports (2-3 minutes), this needs adjustment.

Meanwhile, timeout http-keep-alive specifically manages idle persistent connections between client requests. The 500ms value follows HAProxy's recommendation for typical web traffic.

Here's an optimized configuration snippet for applications with both quick pages and long reports:

backend app_servers
    option http-server-close
    timeout http-keep-alive 500ms
    timeout http-request 5s
    timeout server 300s
    server s1 1.2.3.4:80 check maxconn 100
    server s2 1.2.3.5:80 check maxconn 100

Key observations from production deployments:

  • http-request timeout must be longer than the slowest expected HTTP header response
  • server timeout should cover complete response time including large file downloads
  • keep-alive timeouts don't affect active request processing

A common issue occurs when the http-request timeout is shorter than backend processing time. For a report generating endpoint:

# Bad configuration causing 503 errors
timeout http-request 10s
timeout server 30s

# Fixed configuration  
timeout http-request 60s
timeout server 300s

The initial setup would timeout before backend completion, while the adjusted values accommodate the full processing duration.

For high-traffic sites, consider these refinements:

frontend http_in
    timeout http-keep-alive 1s
    timeout client 30s
    default_backend app_servers

backend app_servers  
    option http-server-close
    timeout http-keep-alive 500ms
    timeout server 300s

This configuration provides longer keep-alive for clients while maintaining shorter server-side persistence.