Reading time · 11 min

Count Round Trips, Not Bytes

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:

time≈round trips×RTT\text{time} \approx \text{round trips} \times \text{RTT}

So 3×87≈2613 \times 87 \approx 261 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:

bash
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:

text
dns=0.014213 connect=0.101487 tls=0.192534 ttfb=0.281406
StepMeasuredPredicted
TCP connection~87 ms87 ms
TLS~91 ms87 ms
Request~89 ms87 ms
Total~267 ms261 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 10×87≈87010 \times 87 \approx 870 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.

rust
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:

bash
cargo run --bin client sequential
text
908.983125ms

Our 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.

rust
for _ in 0..REQUESTS {
    socket.send(MESSAGE)?;
}

for _ in 0..REQUESTS {
    recv_and_check(socket, &mut buf)?;
}

Let’s run it:

bash
cargo run --bin client pipelined
text
92.51975ms

As 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 NN requests in flight.

First, we send NN 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:

throughput≈NRTT\text{throughput} \approx \frac{N}{\text{RTT}}

Every round trip, a full window of NN requests comes back.

Like the pipelining mode, we need two loops. The first one fills the window. It sends NN consecutive requests, or all of them if NN 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.

rust
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.

bash
cargo run --bin client window 1
text
18.11214975s

As 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:

bash
cargo run --bin client sequential
text
18.042476792s

See? 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):

bash
cargo run --bin client window 200
text
96.224375ms

Sending all requests should match the pipelining mode, with REQUESTS still set to 200:

bash
cargo run --bin client pipelined
text
95.9625ms

Quite 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, 20×87 ms≈1.74 s20 \times 87\,\text{ms} \approx 1.74\,\text{s}:

bash
cargo run --bin client window 10
text
1.814752666s

We 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 200/1.81≈110200 / 1.81 \approx 110 requests per second, against 10/0.087≈11510 / 0.087 \approx 115 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 10×87≈87010 \times 87 \approx 870 ms, 87 ms being the RTT from Paris to New York. The transmission adds 10×1 KB/1 Gbps≈0.0810 \times 1\,\text{KB} / 1\,\text{Gbps} \approx 0.08 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”.