Sub topic 15
Persistent Connections, Pipelining, and Newer HTTP Versions
This chapter assumes you know the structure of an HTTP request and response, the TCP three-way handshake, and the idea of round-trip time (RTT). It also assumes you have seen slow start and congestion control. The question here is how many TCP connections an HTTP client opens, and how that choice affects page load time.
Why connection handling matters
HTTP runs on TCP. Opening a TCP connection costs one RTT for the handshake before the client can send its request. The request and the first bytes of the response cost another RTT. A new connection also starts in slow start, so its congestion window is small and the transfer is slow at first.
If a page has one HTML file and several embedded objects, the way the client reuses connections decides how many RTTs the page takes. GATE questions on this topic usually ask you to count RTTs.
Non-persistent connections (HTTP/1.0 default)
In a non-persistent connection, the server closes the TCP connection after sending one object. Each object needs its own connection.
For each object the cost is:
- 1 RTT for the TCP handshake.
- 1 RTT for the request and the start of the response.
- The transmission time of the object.
So each object costs about 2 RTT plus its transmission time. The browser can open several connections in parallel to reduce the total, but every connection still pays the handshake.
Non-persistent connections also load the server. It must allocate buffers and state for every connection, and it must handle many short-lived connections.
Persistent connections (HTTP/1.1 default)
In a persistent connection, the server leaves the TCP connection open after it responds. The client sends later requests on the same connection. HTTP/1.1 makes this the default. A client or server can end it by sending the header Connection: close.
The server closes an idle connection after a timeout. Persistent connections have two benefits:
- The handshake cost is paid once, not once per object.
- The congestion window stays open from earlier transfers, so later objects avoid repeating slow start from a window of one segment.
Persistent connections come in two forms.
Without pipelining
The client sends a request only after it has received the response to the previous one. Each object costs 1 RTT plus its transmission time.
With pipelining
The client sends several requests back to back without waiting for each response. The server returns the responses in the same order as the requests. In the ideal case, all the requests for a page's objects cost about 1 RTT in total, plus the transmission time of all the data.
Counting RTTs: a worked example
A page has one base HTML file and 10 small embedded images, all on the same server. Ignore transmission time, so only RTTs count.
| Mode | Calculation | Total |
|---|---|---|
| Non-persistent, serial | 11 objects × 2 RTT | 22 RTT |
| Non-persistent, 5 parallel connections for the images | 2 RTT for the HTML, then 2 batches of 5 images × 2 RTT | 6 RTT |
| Persistent, no pipelining | 1 RTT handshake, then 11 × 1 RTT | 12 RTT |
| Persistent, with pipelining | 1 RTT handshake, 1 RTT for the HTML, 1 RTT for all 10 images | 3 RTT |
For the pipelined case, the client cannot request the images until it has read the HTML, because the HTML names them. So the HTML takes its own RTT, and the 10 image requests are then pipelined together in one more RTT.
Read the question carefully. Some questions count the handshake as 1 RTT and the request and response as another. Others state a different convention. Use the one the question states.
The head-of-line blocking problem
Pipelining has a weakness called head-of-line (HOL) blocking. The server must send responses in request order. If the first response is slow, for example a large file or a slow database query, every later response waits behind it, even if those responses are ready.
Pipelining had other problems in practice. Some proxies and servers handled it badly. A client could not safely pipeline requests that were not idempotent. Because of these problems, browsers largely disabled it. Instead, browsers open about six parallel persistent connections per host.
Note that this is HOL blocking at the HTTP level. TCP has its own form of it: if one segment is lost, TCP holds back all later bytes until the lost segment is retransmitted, because it must deliver bytes in order.
HTTP/2
HTTP/2 keeps the same methods, status codes, and header meaning as HTTP/1.1. It changes how messages travel on the wire. Its main features are:
- Binary framing. Messages are split into binary frames instead of plain text lines.
- Multiplexing. One TCP connection carries many independent streams. Frames from different streams are interleaved, so a slow response no longer blocks the others. This removes HTTP-level HOL blocking.
- Header compression (HPACK). Headers repeat a lot between requests, so HTTP/2 compresses them using a shared table.
- Stream prioritization. The client can mark some streams as more important than others.
- Server push. The server can send objects the client has not yet asked for. Browsers have since removed support for this, so treat it as a historical feature.
HTTP/2 uses a single TCP connection per host. That brings back the TCP-level problem. If one packet is lost, TCP stalls all streams until it is retransmitted. On lossy networks, HTTP/2 can perform worse than several HTTP/1.1 connections.
HTTP/3
HTTP/3 runs over QUIC instead of TCP. QUIC is a transport protocol built on UDP. It does the work that TCP did, but inside user space:
- Independent streams. Each stream has its own loss recovery. A lost packet stalls only the stream it belongs to. This removes TCP-level HOL blocking.
- Built-in encryption. QUIC includes TLS 1.3. The transport and security handshakes are combined, so a new connection needs fewer RTTs. A client that has talked to the server before can send data with zero extra handshake RTTs (0-RTT).
- Connection IDs. A QUIC connection is identified by a connection ID, not by the four-tuple of addresses and ports. A phone can switch from Wi-Fi to a mobile network and keep the same connection.
- Congestion control. QUIC has its own congestion control and flow control, similar in spirit to TCP's.
QUIC uses UDP because middleboxes on the Internet already pass UDP, and because changing TCP in operating system kernels is slow to deploy.
Comparison
| Feature | HTTP/1.0 | HTTP/1.1 | HTTP/2 | HTTP/3 |
|---|---|---|---|---|
| Transport | TCP | TCP | TCP | QUIC over UDP |
| Default connection | Non-persistent | Persistent | Persistent | Persistent |
| Message format | Text | Text | Binary frames | Binary frames |
| Multiplexing | No | No (pipelining only) | Yes | Yes |
| HTTP-level HOL blocking | Not applicable | Yes | No | No |
| TCP-level HOL blocking | Yes | Yes | Yes | No |
| Header compression | No | No | HPACK | QPACK |
Observing persistence in code
This Python program sends two requests on one TCP connection using HTTP/1.1 keep-alive. It reads each response by using Content-Length, which is how a client knows where one response ends and the next begins. It assumes the server sends a Content-Length header. Some servers use chunked transfer encoding instead, which this program does not handle.
import socket
host = "example.com"
def read_response(sock):
data = b""
while b"\r\n\r\n" not in data:
chunk = sock.recv(4096)
if not chunk:
raise ConnectionError("closed before headers ended")
data += chunk
head, body = data.split(b"\r\n\r\n", 1)
length = 0
for line in head.split(b"\r\n")[1:]:
name, _, value = line.partition(b":")
if name.strip().lower() == b"content-length":
length = int(value.strip())
while len(body) < length:
chunk = sock.recv(4096)
if not chunk:
raise ConnectionError("closed before body ended")
body += chunk
return head.split(b"\r\n")[0].decode()
request = f"GET / HTTP/1.1\r\nHost: {host}\r\n\r\n".encode()
with socket.create_connection((host, 80)) as s:
s.sendall(request)
print(read_response(s))
s.sendall(request) # same connection, no new handshake
print(read_response(s))Each print shows a status line. If the server closed the connection after the first response, the second call raises an error. That is how you would detect a non-persistent server.
Common errors
- Forgetting the handshake RTT. A persistent connection still pays the handshake once.
- Pipelining the HTML and the images together. The client does not know the image names until the HTML arrives.
- Assuming persistence means no state. HTTP stays stateless. The server keeps the TCP connection open, but it does not remember earlier requests.
- Saying HTTP/2 removes all head-of-line blocking. It removes it at the HTTP level only. TCP still delivers bytes in order.
- Saying HTTP/3 uses TCP. It uses QUIC over UDP.
Exercise
A page has one HTML file and 8 embedded objects on the same server. Ignore transmission time. Count one RTT for the TCP handshake and one RTT for each request-response exchange, with the handshake counted only when a new connection opens. How many RTTs does the page take with (a) non-persistent connections, serial, and (b) a persistent connection with pipelining?
Show answer
(a) There are 9 objects, and each one needs a handshake and a request-response, so 9 × 2 = 18 RTT. (b) One handshake takes 1 RTT. The HTML request and response take 1 RTT. The 8 object requests are pipelined and take 1 RTT together. The total is 3 RTT.
Exercise
Why can HTTP/2 still suffer from head-of-line blocking, and how does HTTP/3 avoid it?
Show answer
HTTP/2 multiplexes all streams on one TCP connection. TCP delivers bytes in order, so one lost packet stalls every stream until it is retransmitted. HTTP/3 uses QUIC, which recovers losses per stream. A lost packet delays only the stream it carried.