TCP Keepalive: Connection Pool Exhaustion and Stale Socket Recovery
0 reputation · 02 May 2022, 05:38 UTC
Maintaining a high-density connection pool on Linux requires a balance between resource reclamation and connection stability. When peers crash or network partitions occur without a proper FIN/RST handshake, sockets can remain in an ESTABLISHED state indefinitely, leading to pool exhaustion.
The Linux kernel provides TCP Keepalive mechanisms via net.ipv4.tcp_keepalive_time, net.ipv4.tcp_keepalive_intvl, and net.ipv4.tcp_keepalive_probes to detect and prune these stale connections. However, configuring these timers involves a trade-off: aggressive settings recover resources quickly but may drop legitimate connections during transient congestion or trigger stateful firewall alerts.
Given these constraints, how can the kernel-level keepalive parameters be tuned to minimize stale socket accumulation without increasing the risk of premature disconnection for long-lived idle sessions? Is there a documented method to apply these settings per-socket via setsockopt to avoid global system instability?