Latency in TCP/IP networks over Ethernet stems from multiple factors across different layers of the network stack. At the physical layer, cable quality and switch port configurations can introduce delays. On the protocol level, TCP's congestion control algorithms, acknowledgment mechanisms, and buffer management significantly impact latency.
- Nagle's algorithm: Aggregates small packets but adds delay
- Delayed ACKs: Waits up to 200ms before acknowledging packets
- Buffer bloat: Excessive queuing in network devices
- Retransmission timeouts: Slow recovery from packet loss
- Path MTU discovery: Fragmentation overhead
Use these Linux commands to identify latency sources:
# Show TCP statistics including retransmissions
netstat -s | grep -i tcp
# Check interface statistics
ethtool -S eth0
# Monitor queue lengths
tc -s qdisc show dev eth0
# Measure RTT and packet loss
ping -c 10 example.com
mtr --report example.com
Modify these sysctl parameters in /etc/sysctl.conf:
# Disable Nagle's algorithm for low-latency apps
net.ipv4.tcp_low_latency = 1
# Reduce delayed ACK timeout
net.ipv4.tcp_delack_min = 20
# Increase initial congestion window
net.ipv4.tcp_initcwnd = 10
# Enable fast retransmit
net.ipv4.tcp_frto = 2
# Socket buffer sizes
net.core.rmem_default = 4194304
net.core.wmem_default = 4194304
For socket programming, consider these options:
int sock = socket(AF_INET, SOCK_STREAM, 0);
// Disable Nagle algorithm
int flag = 1;
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &flag, sizeof(int));
// Set custom buffer sizes
int bufsize = 1024*1024;
setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &bufsize, sizeof(bufsize));
setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &bufsize, sizeof(bufsize));
- "TCP/IP Illustrated, Volume 1" by W. Richard Stevens
- Linux kernel documentation: Documentation/networking/ip-sysctl.txt
- Bufferbloat project: https://www.bufferbloat.net
- TCP tuning guide: https://www.ibm.com/docs/en/linux-on-systems?topic=tuning-tcp-ip-networking
Latency in TCP/IP networks stems from multiple layers of the protocol stack. At the physical layer, Ethernet's CSMA/CD mechanism introduces collision-related delays. The TCP protocol itself adds latency through:
- Three-way handshake (1 RTT before data transfer)
- Nagle's algorithm (buffering small packets)
- Delayed ACKs (waiting up to 40ms for piggybacking)
- Congestion control algorithms (e.g., cubic, BBR)
The Linux networking stack provides powerful tools for latency analysis:
# Show TCP statistics including retransmits and latency spikes
netstat -s | grep -E 'segments retransmitted|timeout|delayed acks'
# Real-time latency monitoring
tcpping -C 192.168.1.1
# Detailed socket buffer analysis
ss -tempi
# Kernel TCP parameter inspection
sysctl -a | grep 'net\.ipv4\.tcp'
Critical metrics to watch:
- TCPDelayedACKs (excessive delayed ACKs indicate application pacing issues)
- TCPLostRetransmit (packet loss in retransmission path)
- TCPBacklogDrop (insufficient socket buffer space)
Modern Linux kernels (4.9+) offer several TCP stack optimizations:
# Disable Nagle's algorithm for latency-sensitive applications
int yes = 1;
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes));
# Optimized buffer sizing (units are in bytes)
sysctl -w net.ipv4.tcp_rmem='4096 87380 6291456'
sysctl -w net.ipv4.tcp_wmem='4096 16384 4194304'
For modern networks, consider these cutting-edge approaches:
# Enable BBR congestion control
echo 'net.core.default_qdisc=fq' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_congestion_control=bbr' >> /etc/sysctl.conf
sysctl -p
# Explicit Congestion Notification (requires router support)
sysctl -w net.ipv4.tcp_ecn=1
For a high-frequency trading application, we achieved 17μs latency improvements through:
# IRQ affinity tuning
#!/bin/bash
for irq in $(grep eth0 /proc/interrupts | cut -d: -f1); do
echo 1 > /proc/irq/$irq/smp_affinity_list
done
# Kernel bypass consideration (when extreme latency matters)
modprobe pf_ring
The complete tuning checklist included:
- TCP_NODELAY enabled
- RX/TX queues pinned to isolated cores
- Jumbo frames (MTU 9000) on private links
- SO_TIMESTAMPING enabled for nanosecond precision