Connection pooling and keep-alive
Pay once, use many times
Section titled “Pay once, use many times”The last two lessons put a price on a new connection: with TCP and TLS 1.3 it is two round trips before the request can leave, and three before the response has come back. A client that makes many requests to the same server does not have to pay it many times. Once a connection is open, a request on it costs one round trip and the server’s time.
That is keep-alive: the connection stays open after a response, and the next request uses it. HTTP/1.1 makes connections persistent by default, and a message with the close option ends one, as RFC 9112 describes it. MDN names the benefit: a persistent connection can be reused for several requests, saving the need for a new TCP handshake.
Mental model. A connection is an asset that costs round trips to build. Build it once, and spread the cost over every request that uses it.
Try it: two requests
Section titled “Try it: two requests”The lab below makes two requests, one after the other. In the first variant each request gets a new connection. In the second, the connection from the first request is kept open and reused. Step through both and count the messages.
Two requests: a new connection each time, or one kept open
The connection is closed after each response, so the second request pays for TCP and TLS again.
- TCP handshake40 msClient to Server: SYN. Arrives at 40 ms.SYN
- 80 msServer to Client: SYN-ACK. Arrives at 80 ms.SYN-ACK
- TLS handshake120 msClient to Server: ACK + ClientHello. Arrives at 120 ms.ACK + ClientHello
- 160 msServer to Client: ServerHello … Finished. Arrives at 160 ms.ServerHello … Finished
- HTTP request and response200 msClient to Server: Finished + HTTP request. Arrives at 200 ms.Processing: 20 msFinished + HTTP request
- 260 msServer to Client: HTTP response. Arrives at 260 ms.HTTP response
- TCP handshake300 msClient to Server: SYN. Arrives at 300 ms.SYN
- 340 msServer to Client: SYN-ACK. Arrives at 340 ms.SYN-ACK
- TLS handshake380 msClient to Server: ACK + ClientHello. Arrives at 380 ms.ACK + ClientHello
- 420 msServer to Client: ServerHello … Finished. Arrives at 420 ms.ServerHello … Finished
- HTTP request and response460 msClient to Server: Finished + HTTP request. Arrives at 460 ms.Processing: 20 msFinished + HTTP request
- 520 msServer to Client: HTTP response. Arrives at 520 ms.HTTP response
Time until the second response arrives: 520 ms6 round trips on the wire · 40 ms of processing
Model notes
Assumptions
- Each message takes half the round-trip time of the link it crosses. Messages are never lost, never queue and never overlap, and each leaves when the one before it has arrived, once its receiver has finished any processing.
Simplifications
- The second request is sent after the first answer has arrived. Requests that are made at the same time, over one connection or several, overlap in a real client.
- A connection that is kept open is assumed to still be open for the second request. Real servers and clients close an idle connection after a while, and the next request then pays for a new one.
- TLS 1.3 over TCP is assumed for both variants.
Parameter values are illustrative. They are not benchmarks of any real product.
At a round-trip time of 80 ms and a server that takes 20 ms, a new connection for each request costs 520 ms in all: 260 ms twice. With the connection kept open, the second request costs a round trip and the server’s time, 100 ms, and the total is 360 ms. With a round-trip time of 200 ms the figures are 1,240 ms and 840 ms.
The saving is the handshake for every request after the first: 160 ms here. The more requests, the more it saves. With ten requests, one connection each costs 10 × 260 = 2,600 ms, and one connection kept open costs 260 + 9 × 100 = 1,160 ms, less than half. With thirty, 7,800 ms against 3,160 ms.
A client that makes requests from many threads, to a service or a database, usually does not hold one connection. It holds a connection pool: a set of open connections that requests borrow, use and return. A request that finds one free uses it at once, without a handshake. If every connection is busy, the request waits for one, or the pool opens another, up to a limit.
The size of the pool follows from Little’s Law. A service calls another at 400 requests per second, and each call holds a connection for 50 ms. The number of connections in use at once is 400 × 0.050 = 20 on average. A pool of 20 would be fully busy at the average, and any burst would queue for a connection, so the pool is sized larger, to give headroom: 30, say. The pool size is a ceiling on how many requests can be in flight to that server, which protects the server from being overrun, and a request that waits for a free connection is waiting in a queue, with everything the earlier module said about queues.
A pool that is too small makes requests wait. A pool that is too large wastes memory on both sides, and can send more requests at once than the server can handle. RFC 9112 notes that extra connections consume server resources.
What happens if the server closes an idle connection?
Section titled “What happens if the server closes an idle connection?”A connection that nobody uses is not kept open forever. Servers and intermediaries can close idle connections after a timeout, to free what they hold. The client may not learn of it at once. Its pool still lists the connection as open, and the next request that borrows it is sent into a connection that is gone. It fails, or it hangs until a timeout.
Several practices follow from this. A pool should close connections that have been idle for less than the server’s timeout, so that it is the one to close them. It should check a connection, or detect the failure, and open a new one. And a client that retries a request on a fresh connection after such a failure must be careful about what it sends twice: it is safe for a request that can be repeated, such as reading, and it needs the protections of the idempotency module for one that changes something.
- Opening a connection costs round trips: two before the request leaves with TCP and TLS 1.3. Reusing it costs none.
- Keep-alive keeps the connection open after a response. HTTP/1.1 makes it the default.
- At 80 ms and 20 ms of server time, two requests cost 520 ms with a new connection each and 360 ms with one connection kept open. For ten requests, 2,600 ms against 1,160 ms.
- A pool holds open connections for requests to borrow. Its size follows Little’s Law: 400 requests per second at 50 ms need 20 connections in use, so size it with headroom.
- Idle connections get closed by the other side. A pool must close its own idle connections first, detect a dead one, and retry carefully.
Next: CDNs, edge caching and anycast, which bring the content as well as the connection close to the user.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.