Debug Playbook: WebSocket Disconnects at ~55 Seconds
Symptom: WebSocket clients connected to chat-service consistently disconnect after approximately 55 seconds of inactivity or low activity.
Hypothesis List:
- Proxy Idle-Timeout: An intermediary proxy (
Nginx) has an idle timeout configured near 55-60 seconds, terminating connections. - TCP Keepalive Misconfiguration: Default TCP keepalive settings on the server OS or an intermediary are closing connections.
- Load Balancer Stickiness: The load balancer (
AWS ALB) is not maintaining session affinity, causing connection resets.
Checks (with commands):
1. Proxy Idle-Timeout Investigation (`Nginx`):
- Check `Nginx` Configuration:
* Nginx: Inspect nginx.conf or relevant virtual host configurations for proxy_read_timeout, proxy_send_timeout, keepalive_timeout. Look for values around 60s. Example: grep -r 'timeout' /etc/nginx/conf.d/ /etc/nginx/nginx.conf * HAProxy: Examine haproxy.cfg for timeout client, timeout server, timeout connect, timeout http-request. Example: grep -r 'timeout' /etc/haproxy/haproxy.cfg * AWS ALB: Check the idle timeout setting for the target group associated with chat-service in the AWS console or via AWS CLI: aws elbv2 describe-target-groups --names chat-service-target-group (adjust target group name).
- Check Proxy Logs: Review
Nginx access and error logs for connection termination messages or specific timeout errors around the 55-second mark. Example (Nginx): tail -f /var/log/nginx/access.log /var/log/nginx/error.log | grep '504\|timeout'
2. TCP Keepalive Misconfiguration:
- Check Server OS TCP Keepalive Settings:
* Inspect sysctl parameters on the server hosting chat-service: sysctl net.ipv4.tcp_keepalive_time sysctl net.ipv4.tcp_keepalive_intvl sysctl net.ipv4.tcp_keepalive_probes * Look for tcp_keepalive_time values around 60 seconds. Default is often 7200s (2 hours), but can be overridden.
- Check Client-Side Keepalive (if applicable): If client-side libraries allow, verify their keepalive or heartbeat settings.
3. Load Balancer Stickiness (`AWS ALB`):
- Check `AWS ALB` Configuration:
* AWS ALB: Verify target group stickiness settings (e.g., 'Stickiness enabled' and duration). Ensure it's cookie-based and configured for a sufficient duration for WebSocket sessions. * Other LBs: Consult documentation for AWS ALB regarding session affinity or stickiness for WebSocket connections. Often requires IP-based or cookie-based stickiness.
- Network Trace (Optional but Recommended): Use
tcpdump or Wireshark to capture traffic between the client, load balancer, and server. Look for FIN or RST packets originating from an unexpected source or after the ~55s interval. Example: sudo tcpdump -i any -nn port 80 or port 443 -s0 -w websocket_debug.pcap
Likely Fixes:
1. Proxy Idle-Timeout Adjustment:
- Nginx: Increase
proxy_read_timeout and proxy_send_timeout to a value significantly higher than the expected idle period (e.g., 3600s for 1 hour). Also, ensure keepalive_timeout is adequate. - HAProxy: Increase
timeout client and timeout server in the relevant frontend/backend sections to a higher value (e.g., 3600s). - AWS ALB: Increase the 'Idle timeout' setting for the target group to a value like 3600 seconds (1 hour) or more.
- Implement WebSocket Heartbeats: Configure the WebSocket application to send small heartbeat messages (ping/pong frames) more frequently than the suspected timeout (e.g., every 30 seconds).
2. TCP Keepalive Configuration:
- Server OS: If
net.ipv4.tcp_keepalive_time is set low, increase it to a higher value (e.g., 300 seconds or more) or revert to the default. Apply with sudo sysctl -w net.ipv4.tcp_keepalive_time=300 and persist in /etc/sysctl.conf. - Proxy/Load Balancer: Some proxies/LBs have their own TCP keepalive settings that may need adjustment.
3. Load Balancer Stickiness Configuration:
- `AWS ALB`: Ensure session stickiness is enabled and correctly configured for WebSocket traffic. For HTTP/HTTPS load balancers, this often means cookie-based stickiness with a long duration. For pure TCP load balancers, IP-based stickiness might be the only option.
Verification:
- Monitor Client Connections: Observe client connections for an extended period (e.g., several hours) to confirm the absence of ~55-second disconnects.
- Check Logs: Review
chat-service application logs, Nginx logs, and AWS ALB logs for any connection termination events or errors after applying fixes. - Simulate Inactivity: Intentionally leave a WebSocket client idle for longer than the previous disconnect interval (e.g., 2-3 minutes) to confirm the connection persists.
- Network Trace (Post-Fix): If
tcpdump was used for initial diagnosis, run it again to confirm FIN/RST packets are no longer prematurely terminating connections.