Systemsfor Aravinda
0 / 78 core
Week 1 · Fundamentals

Networking for request paths

3 hours · core path

Read first: Latency numbers that matter

In one sitting

A cold TCP connection costs one round trip before data, and TLS 1.3 adds about one more. HTTP/2 shares one TCP connection across streams, so one lost packet stalls all of them; HTTP/3 and a WebSocket change that shape. Timeouts cap how many sends you hold when the persist stalls.

The application handler is rarely the clock the user feels. The clock is how many round trips you spend before the first byte, whether a lost packet blocks every call on the connection, and how long you are willing to wait when the next hop goes quiet. Latency numbers that matter is the price list. This page is which prices you actually pay on a send.

Core concepts

TCP and UDP do different jobs. TCP gives you a byte stream: it retransmits, it orders, and it slows down when the path is full. A cold TCP connection spends one round trip on the handshake before any of your bytes are delivered. UDP gives you datagrams. It does not retransmit, order, or congestion-control for you. If you send a chat line as one UDP packet and it disappears, nothing in the protocol tells you. You would be building TCP again, badly, unless that loss is acceptable.

QUIC is the case where someone did build those properties, on purpose, on top of UDP. HTTP/3 runs on QUIC. The cryptographic handshake can fold into the first flight, so a cold HTTP/3 connection is a different shape from “TCP, then TLS, then the request.” Connections can survive an address change: a phone that moves networks does not have to start the handshake count over. 0-RTT exists, and a replay of that early data is a real problem for a request that must not run twice. These are handshake shapes, not a promise about a particular browser. A resumed session can be cheaper than a full one. Do not quote a specific browser’s round-trip count as if it were the protocol.

TLS 1.3 adds about one round trip on top of TCP’s one, on a cold connection. Budget two round trips before HTTP bytes. A warm connection that is still open pays neither. The first request from a phone that just woke up is the cold one. Design the tail for that, and let the warm pool be the common case you measure separately.

Two kinds of head-of-line blocking. HTTP/1.1 is a text protocol. In practice one request is in flight per connection, because pipelining never worked well. If request A is slow, request B on that same connection waits. That is head-of-line blocking at the application: the connection is a single queue.

HTTP/2 multiplexes many streams on one TCP connection, so those calls share a round trip instead of lining up. A lost TCP packet still stalls every stream on that connection, because TCP delivers the byte stream in order. The loss is one packet and the stall is every call. That is head-of-line blocking at the transport.

HTTP/3 streams stall independently. The stream that needed the lost packet waits. Its neighbors do not. You pay for that by running over UDP and by treating 0-RTT writes as replayable.

gRPC is HTTP/2 plus a binary protobuf contract, with deadlines and streaming RPCs. It fits calls between your own services. It does not escape the HTTP/2 loss behavior: sibling RPCs on the same connection still share one TCP fate. A deadline is how you keep a stuck RPC from living forever. Streaming is a long RPC, not a reason to skip the deadline.

WebSockets start as an HTTP upgrade and then stay full duplex. The server can push a frame without a new request. The server that holds the socket is stateful: the next frame from that client has to land on the machine that owns the file descriptor, or on a shared backplane that can reach that machine. That is connection affinity for the life of the socket. It is not a reason to pin every future HTTP request from that user to the same box. Load balancing is where that pin goes rotten. Gateways and service mesh is the edge that terminates this, and it should not grow the chat rule inside it.

Timeouts are part of the path. A call with no deadline has no bound on how long it occupies a worker. Little’s law still applies: concurrency is arrival rate times time in system. Stretch the time to a lazy timeout and the concurrency stretches with it. A timeout longer than the user’s budget makes them wait for a failure you have already decided not to show. A timeout shorter than the dependency’s normal tail fails healthy calls. Set it from the budget that remains, and retry only when the retry still fits and the operation can be repeated safely. Idempotency and the exactly-once myth is the condition on that retry. Retries, backoff, and circuit breakers is what you do when the timeout fires for everyone at once.

A worked example

A chat send. The sender waits until the message is persisted, then the server pushes it to recipients. That split is a product decision: the sender does not wait for every phone on earth to render the line. Slack’s real-time path is the published version of this rule. A message is persisted before it is broadcast on the websocket, so the database sits on the send clock. Their published MySQL peak in that writeup was 2.3 million queries per second, about 2 million reads and 300,000 writes, median 2 ms, p99 11 ms. Those are their figures, not this sketch. A 2 ms median is a twentieth of a 40 ms fiber floor. You do not get to borrow it.

Assumptions for the sketch, said as assumptions:

  • The client path is the fiber floor for 4,000 km. Light in fiber is about 200 km per millisecond, so one way is 4,000 / 200 = 20 ms and the round trip is 40 ms before routers. A real coast-to-coast path is this floor plus switching, and the planning band is tens of milliseconds. Using 40 ms makes every extra round trip visible. It is a lower bound, not an SLA.
  • The gateway and the database are in one datacenter. Dean’s same-datacenter round trip is 0.5 ms. We use that for the persist’s network hop and we do not add a 10 ms disk seek. If the log waits on a spinning disk, add 10 ms and say so.
  • The WebSocket upgrade is one HTTP request and response on the connection you just opened, so it costs one more round trip after the handshake.
  • Steady send rate, for the concurrency math only: 10 million daily users, 50 messages each. Both are assumptions. 500,000,000 / 86,400 = 5,787 sends per second average. The shortcut 500,000,000 / 100,000 = 5,000 is the same order.
  • A healthy persist returns in 5 ms. That is longer than 0.5 ms and shorter than a disk seek, a stand-in for “local commit, measured later.”
fiber floor                         4,000 km / 200 km per ms = 20 ms one way
client RTT used here                40 ms
cold TCP                            1 * 40 ms = 40 ms
TLS 1.3                             about 1 * 40 ms = 40 ms
handshake before any HTTP           80 ms
WebSocket upgrade                   40 ms
cold socket ready                   120 ms
persist network hop                 0.5 ms
warm accept, sender waits           40 + 0.5 = 40.5 ms
cold accept                         120 + 40 + 0.5 = 160.5 ms

8 history calls, one HTTP/1.1 conn  8 * 40 = 320 ms
same 8, HTTP/2 or HTTP/3, no loss   about 40 ms
HTTP/2 after one loss               every stream waits at least another 40 ms
HTTP/3 after one loss               only streams that needed the packet

healthy concurrency                 5,787 * 0.005 = 29
stuck until a 2 s timeout           5,787 * 2 = 11,574

On a warm socket the sender’s wait is one client round trip plus a same-building persist. The database is noise next to the fiber. The design move that changes the 40 ms is putting a gateway near the user, not rewriting the handler in a faster language. A same-city fiber path shows why. Assume 50 km across a city, labeled as a sketch of a city, not a map: 50 / 200 = 0.25 ms one way, half a millisecond round trip of propagation. The handshake still costs the same number of round trips, and each one is now cheap. Slack’s gateways sit in several regions so the client connects nearby. The round-trip count does not change. The length of the trip does.

A cold open is a different product. Two handshake round trips plus the upgrade is 120 ms before the socket exists, and the accept is another 40 ms, about 160 ms before the sender sees success. Phones that wake up pay this. A dashboard of warm connections will not show it.

History is where the HTTP version shows up. Eight small fetches on one HTTP/1.1 connection are serial: eight times 40 ms, 320 ms, before server time. That is application head-of-line. Eight warm connections would overlap, at the cost of eight sockets and eight cold handshakes if they are not warm. HTTP/2 puts the eight streams on one connection and the wall clock falls to about one round trip, until a packet is lost. Then every stream waits out the retransmission. A retransmission cannot finish faster than a round trip, because the peer has to learn the packet is missing, so budget at least another 40 ms and often more. HTTP/3 takes the same loss and stalls the streams that needed the packet. The other seven can finish.

The recipient is a second clock. If they are on a California-to-Netherlands path, the published round trip is about 150 ms, and that is after you push. The sender already got the accept at about 40.5 ms. Do not quote 40 ms as “delivered to a phone in Amsterdam.” Figma multiplayer uses the same socket shape for a different product: the server is the authority, clients send updates, the server broadcasts. The order is arrival order at that server. The socket is still state on one machine.

flowchart LR
  sender[Sender]
  edge[Gateway socket]
  db[Message store]
  fan[Channel fanout]
  recv[Recipient gateway]
  sender --> edge
  edge --> db
  db --> fan
  fan --> recv

The arrow the sender waits for ends at the store. The arrows after it are the push. Mixing those clocks is how a design claims a 40 ms send and then blocks on the slowest recipient.

The timeout is a capacity number. Healthy concurrency at 5,787 sends per second and 5 ms is about 29 in flight across the fleet. A database stall that runs every call out to a 2 second timeout holds 5,787 * 2 = 11,574 sends. You did not get a slightly slower query. You got about 400 times the occupancy, because 2 / 0.005 = 400. Little’s law did not change. The time each send stayed in the system changed. The 2 second figure is the bad timeout in this sketch, not a recommended one.

The accept budget on the warm path was about 45 ms. A 2 second server timeout outlives that budget by about forty times. The user has retried or left, and the retry is a second write unless the send carries an idempotency key. gRPC between the gateway and the store should inherit what remains of the budget. If you have 45 ms and you spent 5, the inner deadline is under 40 ms. A deadline that cannot fit a 10 ms disk seek will fail those seeks. That is the correct failure if your budget cannot afford a spindle. A default deadline of many seconds is how the 11,574 figure happens.

Inside the region, keep the gateway-to-store connections warm. A cold one pays TCP’s round trip plus about one more for TLS 1.3, on a 0.5 ms datacenter RTT: about a millisecond, small next to 40 ms, large if you open it on every send. Encrypt that hop anyway. A warm pool is how you avoid putting the handshake on the user’s clock.

Tradeoffs

Choice What you gain What you spend
Several HTTP/1.1 connections A loss or a slow call hurts one request Many sockets, and calls on one socket stay serial
One HTTP/2 connection Parallel streams, one handshake A lost TCP packet stalls every stream
HTTP/3 on QUIC Streams stall independently, and an address change can keep the connection 0-RTT replays if early data is a write; the path has to allow UDP
A long-lived WebSocket The server can push with no new handshake The machine that holds the socket is stateful
gRPC between your services A binary contract and a deadline Still HTTP/2, so sibling calls share a TCP loss

Pitfalls

  • Budgeting a warm connection for a phone that has been asleep. The cold path is two round trips plus an upgrade, about 120 ms at a 40 ms floor, before the send.
  • Treating HTTP/2 as “no head-of-line blocking.” It removes the application queue and keeps the TCP one. One loss stalls every stream on that connection.
  • Using 0-RTT for a send that charges, inserts, or notifies. A replay runs the side effect twice.
  • Holding a WebSocket and also pinning every HTTP call from that user to the same machine. The socket has to stay. The REST call does not.
  • Setting a 30 second timeout because the default felt safe. At a few thousand sends per second that is thousands of stuck calls, and the client already gave up.
  • Retrying a send that timed out, with no idempotency key. The first attempt may still commit.
  • Opening a new TLS connection inside the region on every RPC. You add a handshake to a path whose whole budget was a millisecond.
  • Quoting the server log line as the user-visible latency. The user started at the tap, which includes the round trips, and they finish when the last byte arrives.
  • Forgetting that a raw UDP send does not retry. A lost datagram is a lost message unless you built the retry.

In the interview

In the first five minutes, name the connection as cold or warm, say how many round trips that is, and say whether the user waits for the database or only for an ack you have not persisted. For chat, the sentence is: warm socket, one client round trip plus a local persist, fanout after the ack, timeout inside that budget.

Skip the TCP state machine, cipher suites, and a tour of every HTTP header. The grader is listening for the path and for head-of-line blocking when you put many calls on one connection.

The question that usually follows is “the connection drops” or “the database stalls.” Dropped connection: the client reconnects, pays the cold handshake, and must not duplicate the send. Stalled database: the deadline fires, concurrency stays bounded, and the user sees a failure in the time you promised. Design a chat system is the full product. This page is the path you should already be able to price.

Check yourself

A cold connection crosses a 4,000 km fiber path. How long before the first HTTP byte, and what did you assume?

Fiber is about 200 km per millisecond, so 4,000 km is 20 ms one way and 40 ms round trip before routers. TCP takes one of those round trips before data. TLS 1.3 adds about one more. About 80 ms, as a handshake shape, assuming a full handshake rather than a resumed session and ignoring router delay. A WebSocket is not usable yet: the upgrade is another round trip, about 120 ms to a live socket.

Eight small history calls share one warm connection at a 40 ms round trip. Compare HTTP/1.1, HTTP/2, and a single lost packet.

HTTP/1.1 runs them one at a time, about 320 ms. HTTP/2 overlaps them, about one round trip, 40 ms, until a TCP packet is lost. That loss stalls every stream, for at least another round trip. HTTP/3 overlaps them too, and the loss stalls the streams that needed the packet rather than the whole connection.

The fleet takes 5,787 sends per second and a healthy persist takes 5 ms. A stall runs each call to a 2 second timeout. What is in flight in each case?

Little’s law. Healthy: 5,787 * 0.005 is about 29. Stuck: 5,787 * 2 is about 11,574. The ratio is 2 / 0.005 = 400. The timeout is the W you chose. A long one is a capacity incident, not a safety margin.

Why does the machine that accepted the WebSocket have to see the next frame from that client?

The socket is a file descriptor on that machine. UDP and TCP will not deliver the next byte to a peer that does not hold the connection. Either the balancer sends that client back to the same process, or another process publishes through a shared backplane to the one that holds the socket. A later HTTP request from the same user is a new connection and can land anywhere that can read the shared store.

The sender's accept was 40.5 ms and the recipient is on a California to Netherlands path. What number do you quote for delivered?

40.5 ms is persist-and-ack on the warm path in this sketch, using a 40 ms client floor plus a 0.5 ms local hop. The recipient still has about a 150 ms round trip on that ocean path before their screen updates, and only after the server pushes. Quote both clocks. One of them is the sender’s success. The other is delivery.

Based on: Jeff Dean, Numbers Everyone Should Know; Slack Engineering, real-time messaging; Figma, multiplayer posts; Marc Brooker, AWS Builders' Library.