The somaxconn
parameter in Linux determines the maximum number of backlog connections a socket can hold when the system is not accepting new connections. The default value is typically 128, which might be insufficient for high-traffic servers.
# Check current value
cat /proc/sys/net/core/somaxconn
You can temporarily modify the value using sysctl (effective until reboot):
# Set to 1024 temporarily
echo 1024 > /proc/sys/net/core/somaxconn
# Or using sysctl
sudo sysctl -w net.core.somaxconn=1024
For persistent changes across reboots:
# Edit sysctl.conf
sudo nano /etc/sysctl.conf
# Add this line
net.core.somaxconn = 1024
# Apply changes
sudo sysctl -p
After modification, verify the change:
sysctl net.core.somaxconn
cat /proc/sys/net/core/somaxconn
Note that some applications (like Nginx, Redis) have their own backlog settings that may need adjustment to match or exceed somaxconn:
# For Redis example
# In redis.conf:
tcp-backlog 511
- Higher values consume more memory
- Needs root privileges for permanent changes
- Should match or be lower than application-specific backlog settings
- Monitor system performance after changes
For web servers, you might test with different values:
# Apache Benchmark test
ab -n 10000 -c 1000 http://yourserver/
Compare results before and after adjusting somaxconn to measure improvement.
The somaxconn
parameter defines the maximum number of backlog connections a socket can queue when handling new connection requests. You can view the current value using:
cat /proc/sys/net/core/somaxconn
Typical default values range from 128 to 1024, which might be insufficient for high-traffic servers.
For immediate changes (lost on reboot):
echo 1024 > /proc/sys/net/core/somaxconn
# Or with sysctl:
sudo sysctl -w net.core.somaxconn=1024
For persistent configuration, add this line to /etc/sysctl.conf
:
net.core.somaxconn = 1024
Then apply with:
sudo sysctl -p
Many server applications have their own backlog settings that must align with somaxconn. For example, in Nginx:
server {
listen 80 backlog=1024;
...
}
For Python socket servers:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', 8080))
s.listen(1024) # Should be <= somaxconn value
Check current TCP connection queue status:
ss -lnt
Output columns show Send-Q (backlog limit) and Recv-Q (current queue length). Monitor with:
watch -n 1 'ss -lnt'
- If changing somaxconn doesn't take effect, check application-specific limits
- Kernel versions before 2.4.25 had a hardcoded maximum of 128
- Values beyond 65535 are generally unnecessary and may waste memory
- Consider
net.ipv4.tcp_max_syn_backlog
for SYN flood protection