Count Round Trips, Not Bytes

In Where Network Latency Comes From, we found that a round trip from Paris to New York costs 87 ms, and that most of it is distance, which no upgrade can buy back. So if a round trip can’t get cheaper, the only lever left is to make fewer of them. But how many round trips does a request really need, and how much can we save by cutting them?
The Round Trip Is the Unit of Cost
That 87 ms is the round-trip time (RTT) of a small message from Paris to New York.
But sometimes we need multiple round trips for an operation. For example, loading an HTTPS page for the first time needs 3 round trips:
- first, the connection,
- then, securing it,
- finally, the request.
The formula to estimate the total time of an operation is:
So ms for a page of a few bytes.
To know if an operation will be slow, we don’t count the bytes, we count the round trips (the article title was chosen carefully).
How Many Round Trips in One Request?
Now it’s time to test if our prediction of 261 ms is accurate. We are going to use the same server as in the previous article (nj-us-ping.vultr.com), so the 87 ms RTT still applies.
Here is the command:
curl -I -s -o /dev/null -w 'dns=%{time_namelookup} connect=%{time_connect} tls=%{time_appconnect} ttfb=%{time_starttransfer}\n' https://nj-us-ping.vultr.com/Let’s break it down:
curl: a tool that sends a request to a URL and prints the response,-I: sends a HEAD request, so the server replies with headers only, no page body (the server barely has to work, so we measure the network),-s: silent mode (no progress bar),-o /dev/null: throws the output away (we only care about the timings here, not the headers),-w '...': prints a line once the transfer ends, where each%{...}is replaced with a timer:time_namelookup: the DNS lookup is done,time_connect: the TCP connection is open,time_appconnect: the TLS handshake is done,time_starttransfer: the first byte of the response (here the headers) arrived.
Here is the output:
dns=0.014213 connect=0.101487 tls=0.192534 ttfb=0.281406| Step | Measured | Predicted |
|---|---|---|
| TCP connection | ~87 ms | 87 ms |
| TLS | ~91 ms | 87 ms |
| Request | ~89 ms | 87 ms |
| Total | ~267 ms | 261 ms |
We predicted 261 ms and measured 267 ms. Each step costs about one RTT, as expected. TLS is a bit slower because the client and the server also have to compute the encryption keys. The request is a bit slower too, because the server still has a little work to do. The DNS lookup (14 ms) is not in the total: it goes to a resolver close to us, not to New York. Three round trips, about three RTTs: the formula holds.
Ten Requests, One Round Trip
Now that the connection is open, each new request costs one RTT. So what if the page needs 10 small pieces of data from the same server (a profile, notifications, etc.)? One after the other, it takes ms. If only we had a method to send all requests together to save on RTT.
There is a method (unbelievable, right?): pipelining. This means sending all requests without waiting for a response, then reading the responses as they come back. Requests travel together, so we pay only about one RTT. It’s like posting 10 letters and waiting for the responses instead of posting one letter, waiting for a response, then repeating this 9 more times.
Let’s build a small local Rust UDP server and a client that talks to it, so we can see the difference in latency between classic sequential requests and pipelining.
To simulate the 87 ms RTT we measured, the server creates a new thread for each request. That thread sleeps for 87 ms, then replies to the client with the message it received. The client will send 10 requests, to stick to our previous example.
Creating a new thread allows the server to handle multiple requests at the same time, so they don’t block each other.
The code for both the server and the client is available on the writing-lab repository.
The Sequential Mode
The sequential mode in the client is a simple loop that sends a message, waits for the reply, and repeats.
for _ in 0..REQUESTS {
socket.send(MESSAGE)?;
recv_and_check(socket, &mut buf)?;
}Here, recv_and_check receives the reply and checks it’s the same message. And ? just stops the program in case of an error.
Let’s run it:
cargo run --bin client sequential908.983125msOur prediction was 870 ms for 10 requests and the result is ~909 ms. It’s really close, only ~4 ms off per request. The small difference comes from sleep, which wakes up late, and from the thread that can take some time to be created. We can conclude that the sequential mode costs about one RTT per request.
The Pipelining Mode
The pipelining mode in the client splits the loop from the sequential mode in two: one sending all requests in a row, one receiving all responses in a row. The requests leave one right after the other, without waiting, so they travel together.
for _ in 0..REQUESTS {
socket.send(MESSAGE)?;
}
for _ in 0..REQUESTS {
recv_and_check(socket, &mut buf)?;
}Let’s run it:
cargo run --bin client pipelined92.51975msAs we can see, the responses came back about 10 times faster: we predicted about 87 ms and measured 92.5 ms. We casually demonstrated that we paid the RTT only once and not for each request (pretty neat, huh?).
Notice that both modes sent exactly the same bytes. The only thing that changed is the number of round trips: same bytes, ten times fewer round trips, ten times faster.
But pipelining has a catch: it only works when the requests don’t depend on each other. If a request needs the answer to another request, it must wait for that answer. As we saw earlier, each request waiting for another one to respond costs one round trip.
Filling the Pipe
Based on what we saw, the sequential and the pipelining modes might look totally opposed. In reality, they are the two ends of the same dial: the number of requests we keep in flight. The sequential mode keeps one, the pipelining mode keeps all of them. And in practice, we rarely live at the extremes.
One request at a time is too slow, as the sequential mode showed. All of them at once doesn’t work either, because with too many requests, the server and the memory can’t keep up. So instead, we use a window: we keep at most requests in flight.
First, we send requests to fill the pipe (hence the section title). Then, we wait for the responses, like in the pipelining mode. But each response means one request is no longer in flight, so we send the next one right away to keep the pipe full. With this mode, we expect the throughput, the number of requests handled per second, to be:
Every round trip, a full window of requests comes back.
Like the pipelining mode, we need two loops. The first one fills the window. It sends consecutive requests, or all of them if is greater than the total. The second one slides the window. For each response, we send the next request so the number of requests in flight doesn’t change. Once everything is sent, the window empties until all responses come back.
let initial_window = min(n, WINDOW_REQUESTS);
for _ in 0..initial_window {
socket.send(MESSAGE)?;
}
let mut sent = initial_window;
let mut in_flight = initial_window;
while in_flight > 0 {
recv_and_check(socket, &mut buf)?;
if sent < WINDOW_REQUESTS {
socket.send(MESSAGE)?;
sent += 1;
} else {
in_flight -= 1;
}
}For our examples, the window mode sends 200 requests, set by WINDOW_REQUESTS in the client. With only 10 requests, a window of 10 would already send everything at once, and there would be no middle of the dial to test.
Let’s try with a maximum of one request in flight.
cargo run --bin client window 118.11214975sAs you can see, that’s pretty slow. It’s also equivalent to the sequential mode. To compare with the same number of requests, set REQUESTS to 200 in the client first, then run the sequential mode:
cargo run --bin client sequential18.042476792sSee? That’s totally normal. With a maximum of one request in flight, we wait for a response after each request we send, exactly like the sequential mode.
Now let’s try with 200 requests (all of them):
cargo run --bin client window 20096.224375msSending all requests should match the pipelining mode, with REQUESTS still set to 200:
cargo run --bin client pipelined95.9625msQuite similar. Now, the interesting part: the middle of the dial. In real life, we can’t send everything at once, so let’s try a window of 10 requests, only 5% of the total. Each round trip now brings back 10 requests, so we expect 200 requests to take about 20 round trips, that is, :
cargo run --bin client window 101.814752666sWe predicted 1.74 s and measured 1.81 s: our prediction was almost on point, and the formula holds. With only 5% of the requests in flight, we are already 10 times faster than the sequential mode. In terms of throughput, that’s requests per second, against predicted by the formula.
Now that we can trust our formulas, we can use them without running any code.
Back of the Envelope
This article is almost over. You did the hardest part (congrats!). Now, we are going to find out whether cutting the round trips is better than doubling the bandwidth to send small requests from Paris to New York.
To answer, let’s take our 10 requests from the Ten Requests, One Round Trip section. Requests are 1 KB each, sent one after the other from Paris to New York over a 1 Gbps connection.
Since each request waits for the previous answer, the initial cost is ms, 87 ms being the RTT from Paris to New York. The transmission adds ms (1 KB = 8000 bits).
Our first lever here is doubling the bandwidth. The transmission drops to around 0.04 ms. We gain 0.04 ms over 870 ms, so basically nothing.
The second lever is cutting the round trips. With pipelining, the 10 requests travel together, and the total drops to about 87 ms. We gain about 780 ms (that’s a lot).
To conclude, for small messages, we count the round trips, not the bytes. That’s our title (but now with numbers).
What It Means for Distributed Systems
In a distributed system, most messages are quite small: votes, acks, heartbeats, log entries. As in Back of the Envelope, the bandwidth barely matters, so we need to count round trips.
In real systems, pipelining is quite popular. For example, in a Raft cluster, the leader sends every follower the entries they must append (AppendEntries requests). But instead of waiting for each response, it keeps multiple AppendEntries requests in flight, so it costs fewer round trips. That’s exactly our window mode.
A useful thing to do is to count the round trips of a protocol. For example, a Raft commit costs at least 1 RTT (the leader sends the entry to the followers, then waits for the responses of a majority). 2PC costs 2 RTT (prepare then commit). With one replica in Paris and one in New York, each write costs at least 87 ms, no matter the bandwidth. So to know the cost of a protocol, we count its round trips.
But there is a limit to pipelining, as we saw in Ten Requests, One Round Trip: if a step depends on another one, they cannot be sent together. A 2PC commit must wait for the response of the prepare request. Pipelining cannot solve this. The only ways out are to change the protocol or to move the replicas closer.
Takeaways
For small messages, the bytes are almost free. We pay the number of round trips times the RTT, and no bandwidth upgrade can make a round trip cheaper. So we have two levers left: send independent requests together instead of one by one, and count the round trips of a protocol before choosing it. In short, count round trips, not bytes.
Further Reading
- Computer Networking: A Top-Down Approach, by James Kurose and Keith Ross, section “Principles of Reliable Data Transfer”.
- High Performance Browser Networking, by Ilya Grigorik, chapters “Building Blocks of TCP” and “Transport Layer Security (TLS)”.
- In Search of an Understandable Consensus Algorithm, by Diego Ongaro and John Ousterhout.
- Designing Data-Intensive Applications, by Martin Kleppmann, chapter “Consistency and Consensus”.
nj-us-ping.vultr.com into an IP address. The lookup goes to a resolver close to us, not to the server itself.
Small messages a node sends at regular intervals to say it’s still alive. If they stop coming, the others assume it crashed.
Sent, but not answered yet: the request is on its way to the server, or its reply is on its way back.
The operations a leader copies to the other nodes, like “set x to 3”, kept in order in a log.
A consensus algorithm: a leader copies a log of entries to the other nodes, the followers, and an entry is committed once a majority of the nodes has it.
A copy of the same data kept on another machine, so the data survives if one machine fails.
The TLS handshake: the client and the server agree on encryption keys. With TLS 1.3, it takes one round trip.
The TCP handshake: the client says hello (SYN), the server answers (SYN-ACK). One round trip before any data can flow.
A unit of execution inside a program. Several threads can run at the same time, so one can sleep while the others keep working.
A protocol that sends each message as is: no connection, no retries, no ordering. Here, it means we only measure our simulated RTT, with none of TCP’s machinery in the way.
Messages nodes send to elect a leader: a node becomes leader once a majority votes for it.