Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

When to use tcp _ tw _ reuse or tcp-tw-recycle?


Asked by Lucia Barnett on Dec 13, 2021 FAQ



It seems to be a good option when dealing with a web server that have to handle many short TCP connections left in a TIME_WAIT state. As described here, The TCP_TW_RECYCLE could cause some problems when using load balancers... as mentionned in comment by @raittes, the "problems when using load balancers" is about public-facing servers.
Indeed,
According to Linux documentation, you should use the TCP_TW_REUSE flag to allow reusing sockets in TIME_WAIT state for new connections. It seems to be a good option when dealing with a web server that have to handle many short TCP connections left in a TIME_WAIT state.
In fact, After the randomization of TCP timestamp offsets in commit 8a5bd45f6616 (tcp: randomize tcp timestamp offsets for each connection), the tcp_tw_recycle is broken for all types of connections for the same reason: the timestamps received from a single machine is not monotonically increasing, anymore. Remove tcp_tw_recycle, since it is not functional.
Moreover,
These settings are designed to reduce the amount of time TCP connections states are left open, following the cessation of user data traffic flowing across the socket. The reuse and recycle options differ slightly in their operation, with the recycle option being more problematic for devices behind NAT rules.
Keeping this in consideration,
This is because Linux now randomizes timestamps per connection and they do not monotonically increase. If you're using Linux 4.12 with machines using tcp_tw_recycle and TCP timestamps are turned on you will see dropped connections.