In this lesson, you’ll learn how to tune the Linux TCP stack specifically for latency-sensitive API traffic by balancing buffer sizes and congestion control parameters.
When dealing with high-frequency API traffic, the default Linux kernel settings are often optimized for general-purpose throughput rather than the low-latency responsiveness required for small, request-response payloads. If your buffers are too large, you risk "bufferbloat," where packets queue up unnecessarily, increasing round-trip time (RTT). If they are too small, you trigger packet drops during micro-bursts.
Optimizing the TCP Buffer Window
For low-latency APIs, we want to prevent the kernel from holding onto data longer than necessary. We adjust the tcp_rmem and tcp_wmem parameters. These define the minimum, default, and maximum memory allocated for receive and send buffers.
For a latency-sensitive service, you typically want a smaller default value to prevent excessive buffering, but a sufficient maximum to handle bursts:
# Set min, default, and max TCP buffer sizes (in bytes)
# For API traffic, keep defaults lean to reduce latency
sysctl -w net.ipv4.tcp_rmem="4096 87380 4194304"
sysctl -w net.ipv4.tcp_wmem="4096 16384 4194304"
Selecting the Right Congestion Control
Beyond buffers, the congestion control algorithm is vital. While cubic is the default, it is designed to maximize throughput over long-fat pipes. For web services that frequently operate over low-latency connections, switching to bbr (Bottleneck Bandwidth and Round-trip propagation time) can significantly reduce queuing delay:
# Enable BBR congestion control
sysctl -w net.core.default_qdisc=fq
sysctl -w net.ipv4.tcp_congestion_control=bbr
By enabling fq (Fair Queuing) alongside bbr, the kernel paces packets more smoothly rather than bursting them all at once. This pacing prevents the network interface card (NIC) from being overwhelmed, which directly minimizes jitter and tail latency for your API clients.
Remember, these changes should be tested under synthetic load, as reducing buffer sizes too aggressively can lead to packet loss if your application’s throughput suddenly spikes.
Key Takeaway: To optimize latency for API traffic, shrink your TCP default buffer sizes to minimize queuing delay and switch to the BBR congestion control algorithm with Fair Queuing to pace packet delivery.