Interview Prep/Networking/Sockets, ports, TIME_WAIT and conntrack

Clients that vanished (laptops closed, mobile networks switched) leave connections `ESTAB` on the server for days.

IntermediateTroubleshootingSockets, ports, TIME_WAIT and conntrack
Answer

Likely causes → TCP has no traffic on an idle connection, so if the client disappears without a FIN/RST, the server never notices. These half-open connections hold FDs and memory. TCP keepalive is off by default per socket, and even when on, the default first probe is after 7200 s.

Diagnose →

ss -tno state established '( sport = :5000 )' | grep -c keepalive
sysctl net.ipv4.tcp_keepalive_time net.ipv4.tcp_keepalive_intvl net.ipv4.tcp_keepalive_probes

ss -o shows timer:(keepalive,...) only for sockets with SO_KEEPALIVE set.

Fix → Enable SO_KEEPALIVE in the app and tune it, e.g. 60 s idle, 10 s interval, 6 probes:

sudo sysctl -w net.ipv4.tcp_keepalive_time=60 net.ipv4.tcp_keepalive_intvl=10 net.ipv4.tcp_keepalive_probes=6

Better still, add application-level heartbeats/idle timeouts (WebSocket pings, gRPC keepalive).

Prevent → Set idle timeouts at every layer and keepalive intervals shorter than the shortest middlebox idle timeout.

All Networking questions Roadmap: Networking for DevOps

More on Sockets, ports, TIME_WAIT and conntrack