Table of contents
HTTP's Keep-Alive is implemented by the application layer (user mode), and is called HTTP long connection;
TCP's Keepalive is implemented by the TCP layer (kernel mode) and is called the TCP keepalive mechanism.
HTTP 的 Keep-Alive
The HTTP protocol adopts the request-response mode. The client initiates a request, and the server returns a response, one back and one back.
Keep-Alive is to realize that the same TCP connection can be used to send and receive multiple HTTP requests/responses, avoiding the overhead of connection establishment and release. This method is called HTTP long connection .
Add to the header of the request: ( Starting from HTTP 1.1, Keep-Alive is enabled by default )
Connection: Keep-Alive
In order to avoid resource waste, web service software generally provides keepalive_timeout
parameters to specify the timeout period of HTTP long connections. Once the timer expires, the callback function will be triggered to release the connection.
TCP keepalives
TCP's Keepalive is actually the TCP's keepalive mechanism, which needs to be set through the socket interface SO_KEEPALIVE
to take effect. This function is implemented by the "kernel". When the client and the server have not exchanged data for a certain period of time, the kernel will send a detection message to check whether the other party is still online in order to ensure whether the connection is still valid, and then to decide whether to close the connection.
Summarize:
HTTP's Keep-Alive is also called HTTP long connection. This function is implemented by the "application", which can use the same TCP connection to send and receive multiple HTTP requests/responses, reducing the number of HTTP short connections. Overhead for TCP connection establishment and release.
TCP's Keepalive is also called the TCP keep-alive mechanism. This function is implemented by the "kernel". When the client and the server have not exchanged data for a certain period of time, the kernel will send a probe to ensure that the connection is still valid. text, to detect whether the other party is still online, and then decide whether to close the connection.