TCP vs UDP · Lesson 6 of 8 · 3 min read
Performance and troubleshooting
Inspecting connections
ss -tan state established | wc -l # established TCP connections
ss -tan | awk 'NR>1 {print $1}' | sort | uniq -c # counts by state
ss -ti dst 10.0.2.15 # per-connection internals: rtt, cwnd, retrans, rto
ss -uan # UDP sockets
nstat -az | grep -i -E 'retrans|drop|overflow' # kernel counters
netstat -s | grep -i -E 'retransmit|listen|overflow'
ss -ti output like rtt:120/30 cwnd:10 retrans:0/57 tells you RTT, congestion window and retransmissions — immediate evidence for "slow because of loss".
Measuring and testing
iperf3 -s # on server
iperf3 -c server -t 30 # TCP throughput
iperf3 -c server -u -b 500M # UDP at 500 Mbps: reports loss and jitter
iperf3 -c server -P 8 # parallel streams (helps on high-BDP links)
nc -zv host 443 # TCP port check — handshake proves it's open
nc -u -zv host 53 # UDP "check" — unreliable; use dig @host instead
dig @10.0.0.2 example.com +tcp # force DNS over TCP to test TCP 53
tcpdump -i any -nn 'tcp[tcpflags] & (tcp-rst) != 0' # catch resets
tcpdump -i any -nn udp port 53 # watch DNS
For UDP services, a real application-level test (a DNS query with dig, an NTP query) is the only meaningful reachability check.
Symptom guide
| Symptom | Likely cause |
|---|---|
| Throughput far below link speed on a long path | BDP: small windows/buffers; packet loss; try BBR, larger buffers, parallel streams |
| High retransmissions | Loss or congestion on the path, overloaded NIC/instance limits (ethtool -S allowance exceeded counters on ENA), MTU issues |
| Periodic ~40 ms latency | Nagle + delayed ACK; set TCP_NODELAY |
| Requests hang at exactly 5 s in pods | UDP DNS conntrack race |
| Idle DB connections fail after a few minutes | NAT/LB/firewall idle timeout; enable keepalive, reduce pool idle time |
Cannot assign requested address |
Ephemeral port exhaustion — pooling, reuse, wider range |
| Missing UDP log lines under load | UDP drops; socket buffer overflow (nstat UdpRcvbufErrors); switch to TCP/agent |
| VoIP/video quality poor | Check jitter, packet loss and latency (and bufferbloat) |
| UDP service works locally, not through LB | Health check misconfigured (must be TCP/HTTP), SG missing UDP rule, targets not listening on UDP |
Kernel tuning you might mention
sysctl -w net.core.rmem_max=16777216 net.core.wmem_max=16777216
sysctl -w net.ipv4.tcp_rmem="4096 87380 16777216" net.ipv4.tcp_wmem="4096 65536 16777216"
sysctl -w net.ipv4.tcp_congestion_control=bbr net.core.default_qdisc=fq
sysctl -w net.ipv4.ip_local_port_range="1024 65000"
sysctl -w net.core.somaxconn=4096 # listen backlog
sysctl -w net.ipv4.tcp_keepalive_time=300
Change kernel settings only with measurement — defaults are good for most workloads.