Server Timeout: Complete Diagnosis and Solutions
Understand and resolve timeouts impacting your users and availability
A server timeout occurs when an operation doesn't complete within the allotted time. It's a protection mechanism that prevents a stuck request from consuming resources indefinitely. For the user, this manifests as an error or a page that never loads.
Timeouts can occur at different stack levels: network connection, application processing, database queries, external service calls. Each level has its own timeout parameters and its own causes of exceeding. Identifying the concerned level is the first diagnostic step.
Effective monitoring allows detecting timeouts before they massively impact your users. MoniTao monitors your endpoint response times and alerts you when a timeout occurs or when response times approach critical thresholds.
Different Types of Timeout
Understanding the timeout type helps identify its cause and solution:
- Connection timeout: unable to establish TCP connection. Server is unreachable, overloaded, or firewall blocks connection. Typical codes: connection refused, host unreachable.
- Read timeout: connection is established but server doesn't send data back. Backend is probably processing an operation that's too long.
- Write timeout: client cannot send data to server. Often related to saturated server no longer accepting incoming data.
- Gateway timeout (504): proxy or load balancer didn't receive response from backend in time. Backend exists but is too slow.
Common Timeout Causes
Timeouts generally have identifiable and fixable causes:
- Overloaded server: CPU at 100%, saturated memory, or too many simultaneous requests. Server cannot process new requests fast enough.
- Slow database: unoptimized queries, deadlocks, locked tables, or overloaded DB server. Very common cause of application timeouts.
- External dependencies: calls to third-party APIs that don't respond or are slow. Without configured timeout, your application waits indefinitely.
- Network issues: packet loss, congestion, or routing problems. Can cause intermittent timeouts difficult to diagnose.
Diagnosing Timeouts
A methodical diagnosis allows quickly identifying the timeout cause:
- Identify the type: connection timeout (is server reachable?), read timeout (is server processing?), or gateway timeout (which proxy times out?). The exact error guides diagnosis.
- Check resources: monitor CPU, RAM, disk I/O, and network connections. A saturated server is the most common cause.
- Analyze logs: MySQL slow query log, Nginx/Apache error.log, application logs. Identify which operation takes too long.
- Test dependencies: check database and external API response times. A slow third-party service can cause cascading timeouts.
Timeout Configuration
Here are recommended timeout configurations for different layers of your stack:
# PHP - php.ini
max_execution_time = 30 ; Global script timeout
default_socket_timeout = 30 ; Timeout for file_get_contents, etc.
# PHP-FPM - pool.d/www.conf
request_terminate_timeout = 60 ; Kill worker after 60s
# Nginx - nginx.conf
proxy_connect_timeout 10s; ; Backend connection timeout
proxy_send_timeout 60s; ; Send to backend timeout
proxy_read_timeout 60s; ; Read from backend timeout
fastcgi_read_timeout 60s; ; PHP-FPM timeout
# MySQL - my.cnf
wait_timeout = 28800 ; Inactive connection timeout
interactive_timeout = 28800
lock_wait_timeout = 50 ; Lock wait timeout
# cURL in PHP
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // Connection
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Total
These values are reasonable starting points. Adjust based on your needs: shorter for operations that should be fast, longer for legitimately long processes.
Timeout Monitoring with MoniTao
MoniTao helps you detect and diagnose timeout issues:
- Automatic detection: MoniTao has its own configurable timeout. If your server doesn't respond within this time, an alert is triggered.
- Timeout type identified: the alert indicates whether it's a connection or read timeout, helping guide diagnosis.
- Response time tracking: monitor response time evolution to detect degradations before they become timeouts.
- History and patterns: analyze when timeouts occur to identify correlations (traffic spikes, crons, etc.).
Timeout Resolution Checklist
- Timeout type identified (connection, read, gateway)
- Server resources checked (CPU, RAM, I/O)
- Slow query logs analyzed and queries optimized
- Timeouts of all layers configured consistently
- Circuit breakers in place for external dependencies
- Monitoring and alerts configured for early detection
Frequently Asked Questions about Timeouts
What timeout should I configure for my application?
30 seconds is a good default for standard web operations. For legitimately long operations (exports, imports), create dedicated endpoints with higher timeouts. Don't increase the global timeout.
How does MoniTao detect timeouts?
MoniTao has a configurable check timeout (default 30s). If your server doesn't respond within this time, MoniTao records a timeout and can trigger an alert based on your configuration.
My timeouts are intermittent, why?
Intermittent timeouts are often load-related: server handles normal traffic fine but saturates at peaks. Analyze temporal correlations between timeouts and traffic, deployments, or crons.
What is a circuit breaker?
A resilience pattern that "opens the circuit" to a failing service after several failures, preventing cascading timeouts. After a delay, it tests the service again to see if it's restored.
How to prevent timeouts on external API calls?
Configure strict timeouts (5-10s max), implement circuit breakers, use caches for rarely changing data, and provide fallbacks (default values) when external service is slow.
Is the timeout on proxy side or application side?
If the proxy (Nginx) generates a 504, it's its timeout. If the application returns a timeout error, it's PHP or application timeout. Check both logs to identify which triggers first.
Conclusion
Timeouts are an essential protection mechanism but their triggering signals a performance issue. Resolution involves identifying the timeout type (connection, read, gateway), analyzing resources and logs, and optimizing slow operations.
Consistent timeout configuration across all stack levels is essential: proxy timeout should be higher than application's, which should be higher than database's. MoniTao allows you to monitor response times and be alerted before timeouts impact your users.
Useful Links
Ready to Sleep Soundly?
Start free, no credit card required.