Mobile system design, from a packet to an app · Puzzle 5 · 6 of 11
One connection, many requests
Puzzle 3 counted round trips per request. A brand-new HTTPS connection adds its own before the first request even leaves. Here's what they cost, and how OkHttp makes them a one-time price.
The problem
Puzzle 3 said one request costs one round trip. You measure your first API call of the day on a 250 ms network: it takes about three times that. The next calls are fast. What did the first one pay for?
See it
A first request on a new connection: a TCP handshake, then a TLS handshake, each one round trip, then the request itself. Three trips.
Step 1 of 4Connection reuse: the connection stays open, and the next request goes straight over it.
Step 2 of 4No handshakes this time: one round trip. Predict: what if the screen needs several requests at once?
Step 3 of 4HTTP/2 sends them all at once over that one connection: multiplexing. One shared OkHttpClient keeps it open for later requests.
Step 4 of 4
The building block
Before HTTPS can carry a request, two handshakes happen on a new connection:
- TCP handshake: client and server agree to talk. One round trip before any data flows.
- TLS handshake: the key agreement from the Encryption course. TLS 1.3 needs one more round trip.
- Then your request and its answer: the round trip from puzzle 3.
So a first request on a new connection costs about three round trips: 750 ms on that network, for a few kilobytes.
Connection reuse keeps the connection open and sends the next request over it. Both handshakes are skipped: one round trip, as in puzzle 3.
HTTP/2 goes further. With HTTP/1.1, a connection carries one request at a time. HTTP/2 does multiplexing: many requests and responses interleaved on one connection, all in flight at once. That's what makes "send them together" from puzzle 3 cheap.
In an Android app, OkHttp (under Retrofit) does both for you, with one condition: use one OkHttpClient for the whole app. Each client keeps its own connection pool, the set of connections it keeps open for reuse; a new client per screen, or per request, starts cold and pays the handshakes again. For per-call settings, use client.newBuilder(): the new client shares the original's pool.
The analogy
Phoning a busy office. The first call, you go through the switchboard and prove who you are (the handshakes). After that, you stay on the line and ask everything you need. With HTTP/2, you can even ask several questions without waiting for each answer. Hang up after every question, and you start at the switchboard again.
Check yourself
Sources
- High Performance Browser Networking: Building blocks of TCP ("each new connection will have a full roundtrip of latency before any application data can be transferred"; "The delay imposed by the three-way handshake makes new TCP connections expensive to create, and is one of the big reasons why connection reuse is a critical optimization")
- RFC 8446, TLS 1.3 (the full handshake is the "1-RTT handshake")
- High Performance Browser Networking: HTTP/2 ("The primary goals for HTTP/2 are to reduce latency by enabling full request and response multiplexing")
- OkHttp README and OkHttpClient docs ("HTTP/2 support allows all requests to the same host to share a socket"; "OkHttp performs best when you create a single OkHttpClient instance and reuse it for all of your HTTP calls. This is because each client holds its own connection pool and thread pools.")
What you now know
A new HTTPS connection costs a TCP handshake (one round trip) and a TLS 1.3 handshake (one more) before the request's own round trip. Connection reuse skips both for every later request; HTTP/2 goes further and sends many requests at once over that one connection. In code, that means one shared OkHttpClient, because each client has its own connection pool.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.