Skip to main content

TCP Recycle

TCP connection tear down is one part which is least documented in the internet. In an ideal web server client architecture, once the server sends FIN, client will send FIN+ACK, server sends an ACK and enters time wait state. Look at this diagram

Now lets consider our problem. We have deployed new servers which receive huge traffic with less processing time(100 ms). Now all these connections enter into time wait state to tear down connection. Time Wait state is useful for two reasons
  1. If the last ack sent by the server is missed half way, client will retransmit Fin+Ack. If the socket was reused by some other connection, there would be confusions in the network
  2. If  a router malfunctioned and any of the data from client to server was lost. Client would have retransmitted it after RTO (retransmission timeout) , There is a high probability that router might reinject the old packet if its segment lifetime is less than MSL. Now this packet could also cause conflict if the same tcp connection tuple is used on server side
In our case the incoming connection rate is constant(almost) say lambda. These packets reach timewait after 100ms. The timewait is constant in linux 60s. In kernel code it is defined as a macro


After receiving live traffic for some 20 minutes, number of connections in timewait shoots upto 1.4 million. The bottle neck is not in the application, its in the timewait state. But time wait is needed for gracefully closing TCP.

There are lot of links in internet which says changing tcp_fin_timeout in /etc/sysctl.conf will reduce time wait length. This is seriously wrong, tcp_fin_timeout reduces FIN_WAIT 2 state time(Refer diagram above).

Now there came a new kernel parameter in TCP called tcp_tw_recycle in /etc/sysctl.conf. In this case the server waits only for retransmission timeout (around 1-3s) and reuses the socket for new connection.
The case 1 of timewait, failed ACK will be handled in this scenario as it waits for RTO. But the case 2, where a packet stays alive for a MSL(Maximum Segment Length), is not handled gracefully. To avoid conflicts TCP stores the recent timestamp from the client and accepts packets only in increasing order of time stamps from clients. This seems well and good and time spent on time_wait state is reduced. We implemented this on our systems and connections in time wait drastically reduced. Then why didnt TCP use this by default? Lets consider a scenario, 

Some 4 systems are trying to connect to a server which uses recycle parameter. The clients are sharing a wifi. So they are behind a NAT and  they use same public ip. The clocks in 4 systems need not be in sync. So if the system which has a higher timestamp (say t1) has succesfully established a connection with server and closed it. Other clients cant establish connection with server until their timestamp is greater than t1.  Abnormal behaviour will be seen for devices behind NAT and firewall. So this parameter is not encouraged in most of the cases.


Popular posts from this blog

How we have systematically improved the roads our packets travel to help data imports and exports flourish

This blog post is an account of how we have toiled over the years to improve the throughput of our interDC tunnels. I joined this company around 2012. We were scaling aggressively then. We quickly expanded to 4 DCs with a mixture of AWS and colocation. Our primary DC is connected to all these new DCs via IPSEC tunnels established from SRX. The SRX model we had, had an IPSEC throughput of 350Mbps. Around December 2015 we saturated the SRX. Buying SRX was an option on the table. Buying one with 2Gbps throughput would have cut the story short. The tech team didn't see it happening. I don't have an answer to the question, "Is it worth spending time in solving a problem if a solution is already available out of box?" This project helped us in improving our critical thinking and in experiencing the theoretical network fundamentals on live traffic, but also caused us quite a bit of fatigue due to management overhead. Cutting short the philosophy, lets jump to the story.

The server, me and the conversation

We were moving a project from AWS to our co-located DC. We have setup KVMs scheduled by Cloudstack for each of the component in the architecture. The KVMs used local storage. The VMs are provisioned with more than required resources because we have the opinion that in our DC scaling during peak load and then downscaling doesn't offer much benefits financially as we are anyways paying for the hardware in advance and its also powered on. Its going to be idle if not used. Now we found something interesting our latency in co-located DC was 2 times more than in AWS. The time for first byte at our load balancer in aws was 60ms average and at our DC was 112ms. We started our debugging mission, Mission Conquer-AWS. All the servers are newer Dell hardwares. So the initially intuition was virtualisation is causing the issue. Conversation with the Hypervisor We started with CPU optimisation, we started using the host-passthrough mode of CPU in libvirt so VMs dont see QEMU emulated CPUs,


Ptrace is a nice setup ( some people call dirty setup) on linux to debug running processes. This ptrace in sys/ptrace.h is used by strace and gdb. To trace a child process, the child process should call PTRACE_TRACEME. The kernel during each system call(or execution of each instruction) checks if the process is traced. If it is traced, it issues a SIGTRAP, the parent process if in wait() state, will get a signal. The parent issues a SIGSTOP to hold current state of child and can access the registers and memory of child using PEEKDATA and alter the values in register and memory using POKEDATA. Once the required job is done, parent will allow the child to run with a SIGCONT signal. Since one can access registers, the next instruction to be executed can be easily found using instruction pointer, this comes in handy when we need to set breakpoints while debugging. The entire code base can also be changed using ptrace. PTRACE_ATTACH attaches a running process. It does some hack to become