HTTP Keep-Alive & Handshake
Hypertext Transfer Protocol (HTTP)
Hypertext Transfer Protocol (HTTP) is a asymmetric request-response client-server protocol. An HTTP client sends a request message to an HTTP server, then the server returns a response message. In other words, HTTP is a pull protocol, the client pulls information from the server.
HTTP Versions
- First documented in 1991 (HTTP/0.9)
- HTTP/1.0 introduced in 1996 (RFC 1945)
- HTTP/1.1 updated in 1999 (RFC 2616)
- HTTP/2.0 updated in 2015 (RFC 7540)
Round Trip Time (RTT)
In TCP/IP network communication, efficiency can be measured, in part, by a metric known as Round Trip Time (RTT). RTT describes the time it takes for a request to travel from client to server and back again.
For example, if a client makes a request to a server and the server responds; that’s 1 x RTT
.
HTTP Connections
Non-persistent and persistent are the two types of HTTP connections used to connect the client with the server. The HTTP 1.0 protocol, built on a backbone of non-persistent connections. In HTTP 1.1, all connections are considered persistent.
The non-persistent connection takes a total time of 2RTT + file transmission time
. It takes the first RTT to establish the connection between the server and the client. The second RTT is taken to request and return the object. This case stands for a single object transmission.
The persistent connection takes 1 RTT
for the connection and then transfers as many objects, as wanted, over this single connection.
The diagram above clearly shows some advantages to keeping a persistent connection open for multiple HTTP requests.
Keep-Alive
HTTP Keep-alive is the mechanism that instructs the client and server to maintain a persistent TCP connection, decoupling the one-to-one relationship between TCP and HTTP, effectively increasing the scalability of the server.
The HTTP 1.0 protocol does not support persistent connection by default.If the client supports keep-alive, it adds an additional header to the request.
Connection: keep-alive
When the server receives this request and generates a response, if it supports keep-alive then it also adds the same above header to the response. HTTP 1.0 is no longer used on the internet.
The HTTP 1.1 supports persistent connections by default. The HTTP persistent connections do not use separate keep-alive messages, they just allow multiple requests to use a single connection. However there is a default connection timeout.
To demonstrate this keep-alive process. I created a simple Express.js code to create a server. Let’s see what happens in each scenario using Wireshark.
Scenario 1: Send a single request.
Here I used a browser to call the get function.
As you can see, the client requests the data, and the server acknowledges (ACK) and sends the response. The client acknowledges the response and then the server sends a FIN flag to the client, but the client didn’t close the connection, and the client kept sending a Keep-alive header. After a while, the server sends the reset packet.
Scenario 2: Send 3 requests
In this case, the client didn’t receive any FIN flag because the client kept sending requests to the server, so the server didn’t close the connection. (The connection is persistent).
Scenario 3: Send a single request and close the browser (client)
When we close the browser (client), the client sends a FIN flag to the server and the server acknowledges.TCP is a fully duplex connection; because of that, the server also sends a message to the server to close the connection.
Scenario 4: Send a single request and wait for 1 minute for the response from the server
Here I changed the above code to wait for 1 minute for the response.
As you can see, the client sends the request but the server does not respond for 1 minute, so the client continues to send the Keep-Alive header to keep the connection open.
Scenario 5: Send a single request and terminate the server
In this case, the client is waiting for the response but the server terminated. Then the client tries to initiate the connection and the server keeps sending RST messages.
The Benefits of Connection Keep Alive
- Because of Keep-Alive, the server and the clients use fewer network resources to use a single connection.
- Reducing the number of TCP connections between server and clients can lead to a drop in network congestion.
- Reducing the number of three-way handshakes can lead to improved site latency.