TCP Keep-Alive & Connection Pool

To understand TCP Keep-Alive / Persistent Connection, you have to start from the TCP protocol.

TCP Flow:

_____ _____ | | | | | | | | | | | —> —> —> ————– SYN – ————> —> —> — || — —> —> ——— —– ACK ————–> —> —> — || —> —> —> —- ———- … ————–> —> —> — || —

Establishing a TCP link requires 3 steps, SYN, SYN / ACK, ACK, 4 steps


When the user opens a page via the browser, only 2 packets are required, and the http get request is a response (Response from server). But here is not included in the TCP link (3 packets) and close (4packets), of course, each packet requires ACK, but the ACK information is generally included in the response in the response, in order to quantify, it is ignored.

Take the web app as follows:

There is a page that contains 5 small pictures, so in order to load these content into the browser, it takes 5 HTTP requests. In the case where Keep-alive is not turned on (3 + 2 + 4) ¡Á 545 packets; only 3+ (2) ¡Á 5 + 417 Packets is required to open Keep-alive.

Of course, the calculation formula is not as simple as the actual situation, and it is also related to other factors such as the connected connections (such as IE6 2), Header information, but the principle is the same, so in today’s page elements. Next, Keep-Alive can reduce great overhead.

When Internet actual engineering applications, there is a need to solve some problems, such as leaving bandwidth, small picture combination, avoiding the number of links, avoiding link exceptions, etc., etc., requires the appropriate resource configuration and Enhance the user experience.

connection pool:

Above mentioned TCP’s Keep-Alive, and we will encounter the same problem in the application, in order to reduce additional overhead, after verifying, starting a series of parameter status, keep existing resources, for the next or other need for this resource The program is used to increase efficiency.

For example, we can keep the Agent on the Web Server, the Agent keeps the database so that the Web App only needs to quickly pass the Agent with the database through the local Domain Socket, thus maximizing server efficiency and load capacity, achieving cost savings .


TCP Level Keep-Alive has its own mechanism to keep a link state, ie, default, after 2 hours, then after 2 hours, once every 75 seconds, no response is not obtained in a row, it is determined that the link is invalid. MySQL keeps the connection By default, it is 8 hours, and if there is no request 8 hours, the corresponding connection is turned off, so it can make Timer from the new count through the mysql ping.

refer to: