I have been reading some articles about TCP implementation on Linux and I got confused, what is the difference between net.ipv4.tcp_max_syn_backlog
and net.core.somaxconn
and the backlog
passed as parameter to listen()
system call, and what is the relation between them.
P.S. I want explanation for kernel 4.15 because I found that there are some differences between oldest and newer kernels on this subject.
tcp_max_syn_backlog : Maximal number of remembered connection requests, which have not received an acknowledgment from connecting client. The minimal value is 128 for low memory machines, and it will increase in proportion to the memory of machine. If server suffers from overload, try increasing this number.
SOMAXCONN defines the maximum number you're allowed to pass to listen() which is 128 on my system. You can read more about it in the man page. Follow this answer to receive notifications.
net. core. netdev_max_backlogThis parameter sets the maximum size of the network interface's receive queue. The queue is used to store received frames after removing them from the network adapter's ring buffer.
net. ipv4. tcp_rmem. Contains three values that represent the minimum, default and maximum size of the TCP socket receive buffer. The minimum represents the smallest receive buffer size guaranteed, even under memory pressure.
sysctl is an API. So you can just read the Linux kernel documentation for appropriate version:
tcp_max_syn_backlog - INTEGER
Maximal number of remembered connection requests, which have not
received an acknowledgment from connecting client.
The minimal value is 128 for low memory machines, and it will
increase in proportion to the memory of machine.
If server suffers from overload, try increasing this number.
somaxconn - INTEGER
Limit of socket listen() backlog, known in userspace as SOMAXCONN.
Defaults to 128. See also tcp_max_syn_backlog for additional tuning
for TCP sockets.
Let's consider a TCP-handshake.. tcp_max_syn_backlog
represents the maximal number of connections in SYN_RECV
queue. I.e. when your server received SYN, sent SYN-ACK and haven't received ACK yet. This is a separate queue of so-called "request sockets" - reqsk
in code (i.e. not fully-fledged sockets, "request sockets" occupy less memory. In this state we can save some memory and not yet allocate a full socket because the full connection may not be at all in the future if ACK will not arrive). The value of this queue is affected (see this post) by listen()
's backlog
argument and limited by tcp_max_syn_backlog
in kernel.
somaxconn
represents the maximal size of ESTABLISHED
queue. This is another queue.
Recall the previously mentioned SYN_RECV
queue - your server is waiting for ACK from client. When the ACK arrives the kernel roughly speaking makes the big full-fledged socket from "request socket" and moves it to ESTABLISHED queue. Then you can do accept()
on this socket. This queue is also affected by listen()
's backlog
argument and limited by somaxconn
in kernel.
Useful links: 1, 2.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With