Networking
Linux Socket Buffer Tuning: rmem, wmem and the Bandwidth-Delay Product
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيEvery socket on Linux has a receive buffer and a send buffer in kernel memory. The receive buffer holds data that arrived but has not yet been read by the application; the send buffer holds data the application wrote that has not yet been acknowledged by the other side. If these buffers are too small, a fast long-distance TCP connection cannot keep enough data in flight and never reaches the available bandwidth. If they are too large without reason, memory is wasted and queues grow. This guide explains how the kernel sizes them, which sysctl settings matter, and how to tune them with a clear method instead of copying random values.
The bandwidth-delay product
TCP throughput on a single connection is limited by how much unacknowledged data can be in flight. The amount needed to fill a path is the bandwidth-delay product (BDP):
BDP (bytes) = bandwidth (bits per second) x RTT (seconds) / 8Some examples:
| Bandwidth | RTT | BDP |
|---|---|---|
| 100 Mbit/s | 20 ms | 250 KB |
| 1 Gbit/s | 20 ms | 2.5 MB |
| 1 Gbit/s | 100 ms | 12.5 MB |
| 1 Gbit/s | 200 ms | 25 MB |
A server in Europe sending to a client in Egypt or a backup target in the USA sees far more RTT than a client in the same city. The buffers on both ends must be able to grow to roughly the BDP, or the connection will be window-limited no matter how fast the network is.
The settings that matter
Check the current values:
sysctl net.core.rmem_max net.core.wmem_max net.core.rmem_default net.core.wmem_default
sysctl net.ipv4.tcp_rmem net.ipv4.tcp_wmem net.ipv4.tcp_moderate_rcvbuf net.ipv4.tcp_mem- `net.ipv4.tcp_rmem` / `tcp_wmem`: three values, min, default and max, in bytes. TCP autotuning moves each socket's buffer between these limits. Common defaults are around
4096 131072 6291456for receive and4096 16384 4194304for send, but they vary by kernel and available memory. - `net.ipv4.tcp_moderate_rcvbuf`: when 1 (the default), the kernel automatically grows the receive buffer as needed.
- `net.core.rmem_max` / `wmem_max`: the ceiling for buffers that applications request explicitly with
setsockopt(SO_RCVBUF / SO_SNDBUF). They do not limit TCP autotuning, which followstcp_rmemandtcp_wmem. - `net.core.rmem_default` / `wmem_default`: the initial size for non-TCP sockets such as UDP.
- `net.ipv4.tcp_mem`: global TCP memory limits in pages, not bytes. The kernel computes this from RAM; leave it alone unless you know why.
An important detail: when an application sets SO_RCVBUF itself, autotuning is disabled for that socket, and the kernel doubles the requested value to account for overhead. Many applications get better throughput by not setting buffer sizes at all and letting autotuning work.
Seeing buffers in action
ss shows memory per socket:
ss -tmi state establishedThe skmem:(r...,rb...,t...,tb...) field shows the current receive queue (r), the receive buffer size (rb), the send queue (t) and the send buffer size (tb). If rb sits at the maximum of tcp_rmem during a slow long-distance transfer, the buffer may be the limit.
For UDP, look for drops caused by full buffers:
nstat -az | grep -E "UdpRcvbufErrors|UdpSndbufErrors"
netstat -su | grep -i "buffer errors"Rising RcvbufErrors means the application is not reading fast enough or the buffer is too small for bursts. This is common with DNS servers, VPN software, game servers and streaming tools.
Tuning step by step
- Estimate the BDP for your realistic worst case, for example 1 Gbit/s to a client 100 ms away, which is about 12.5 MB.
- Raise the TCP maximums so autotuning can grow to at least that value. Keep the min and default values unchanged.
- Raise `rmem_max` and `wmem_max` only if an application sets buffers explicitly, or for UDP workloads.
- Apply in a drop-in file and reload.
- Measure with a real transfer, such as
iperf3 -c <server> -t 30across the actual path, before and after.
An example for a VPS that serves large files to distant clients:
sudo tee /etc/sysctl.d/70-socket-buffers.conf >/dev/null <<'EOF'
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 131072 16777216
net.ipv4.tcp_wmem = 4096 16384 16777216
EOF
sudo sysctl --systemThese values allow each socket to grow to 16 MB when needed. Autotuning keeps idle or slow sockets small, so memory use stays proportional to real traffic. On a small VPS with 1 or 2 GB of RAM and thousands of connections, be more conservative, because the worst case is many sockets each using large buffers.
Common mistakes
- Copying huge values such as 128 MB maximums from tuning guides written for 100 Gbit data center links. They rarely help on a VPS and can increase memory pressure.
- Raising only `rmem_max` and expecting TCP to speed up. Autotuned TCP follows
tcp_rmem, notrmem_max. - Setting `SO_RCVBUF` in the application and accidentally disabling autotuning with a value smaller than the BDP.
- Ignoring the other end. The receiver's window limits the sender. If the client has small buffers, server tuning cannot fix it.
- Tuning buffers to fix packet loss. Loss is better handled by the congestion control; see our guide to TCP BBR.
Buffer size is one piece of network performance. How the initial window grows is covered in TCP slow start and initcwnd, and how packets are spread across CPU cores in receive side scaling.
FAQ
Do I need to tune socket buffers on every VPS?
No. Modern Linux defaults with autotuning handle most web and API workloads well. Tuning helps mainly for large transfers over long distances, high-throughput UDP services, or applications that report buffer errors.
What is the difference between rmem_max and tcp_rmem?
rmem_max caps the receive buffer that applications request explicitly with SO_RCVBUF. tcp_rmem defines the minimum, default and maximum that TCP autotuning uses for sockets that do not set their own size. For typical TCP servers, tcp_rmem is the setting that matters.
Will larger buffers reduce latency?
Usually not. Larger buffers let a single connection carry more data in flight, which raises throughput on high-latency paths. Oversized buffers can even add delay when queues fill up, so size them to the bandwidth-delay product rather than to the largest possible value.