tcp_tw_reuse vs tcp_tw_recycle : Which to use (or both)? tcp_tw_reuse vs tcp_tw_recycle : Which to use (or both)? linux linux

tcp_tw_reuse vs tcp_tw_recycle : Which to use (or both)?


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.

As described here, The TCP_TW_RECYCLE could cause some problems when using load balancers...

EDIT (to add some warnings ;) ):

as mentionned in comment by @raittes, the "problems when using load balancers" is about public-facing servers. When recycle is enabled, the server can't distinguish new incoming connections from different clients behind the same NAT device.


pevik mentioned an interesting blog post going the extra mile in describing all available options at the time.

Modifying kernel options must be seen as a last-resort option, and shall generally be avoided unless you know what you are doing... if that were the case you would not be asking for help over here. Hence, I would advise against doing that.

The most suitable piece of advice I can provide is pointing out the part describing what a network connection is: quadruplets (client address, client port, server address, server port).

If you can make the available ports pool bigger, you will be able to accept more concurrent connections:

  • Client address & client ports you cannot multiply (out of your control)
  • Server ports: you can only change by tweaking a kernel parameter: less critical than changing TCP buckets or reuse, if you know how much ports you need to leave available for other processes on your system
  • Server addresses: adding addresses to your host and balancing traffic on them:
    • behind L4 systems already sized for your load or directly
    • resolving your domain name to multiple IP addresses (and hoping the load will be shared across addresses through DNS for instance)