Workspace

UDP, TCP and the byte stream

3 day streakD

UDP, TCP and the byte stream

UDP sends datagrams and promises nothing. TCP promises an ordered, reliable stream of bytes, but not messages. Learn why every protocol on top of TCP must frame its own messages.

BasicsLesson 4 of 718 min

The two main transports make opposite choices. UDP sends independent datagrams: each send becomes one packet, it may arrive, arrive twice, arrive out of order or not at all, and nobody tells you. TCP gives you a connection and an ordered, reliable stream of bytes, retransmitting lost data and slowing down when the network is busy.

UDPTCP
UnitDatagram, boundaries preservedByte stream, no boundaries
DeliveryBest effortReliable and in order
Connection setupNoneThree-way handshake
Congestion controlNone built inBuilt in
Used byDNS, video calls, games, QUICHTTP/1.1, HTTP/2, databases, SSH

The sockets API

Programs talk to the network through sockets, which are file descriptors (Module 2). A server calls socket, bind to a port, listen, then accept for each client. A client calls socket then connect. After that, both sides read and write bytes. Everything a harness does over HTTP rests on these few calls.

No message boundaries

This is the most important property of TCP and the most common bug. If the sender writes hello and then world, the receiver may read helloworld in one call, or hel then loworld, or one byte at a time. TCP only promises that the bytes arrive in order. How they are grouped into reads depends on packet sizes, timing and buffers.

Figure 4.1

Three messages, random reads

What the receiver's reads return · prefix written as digits for readability
  1. read 120:{"id":
  2. read 21,"tool":"ls"}21:{"id":2,"
  3. read 3tool":"cat"}22
  4. read 4:{"id":3,"tool":"grep"}
Messages the app sees
  • ✓ after read 2{"id":1,"tool":"ls"}
  • ✓ after read 3{"id":2,"tool":"cat"}
  • ✓ after read 4{"id":3,"tool":"grep"}

All 3 messages recovered intact, whatever the read sizes.

The sender writes three JSON messages. The network delivers them in arbitrary chunks. Parsing each read as a message breaks; length-prefixed framing accumulates bytes until a full message is available.

Framing: three common strategies

  1. 01
    Delimiters

    End each message with a byte that cannot appear inside it, such as a newline. JSON Lines and SSE (a blank line) work this way. Simple, but the delimiter must be escaped inside messages.

  2. 02
    Length prefixes

    Send the message length first, then exactly that many bytes. gRPC uses a 5-byte prefix; many binary protocols use a 4-byte big-endian length.

  3. 03
    Self-delimiting formats

    Some encodings reveal their own end, such as HTTP/1.1 headers ending in a blank line plus a Content-Length.

frame-reader.tsTypeScript
/** Reads 4-byte big-endian length-prefixed messages from a TCP stream. */
export function frameReader(onMessage: (msg: Buffer) => void) {
  let buffered = Buffer.alloc(0);
  return (chunk: Buffer) => {
    buffered = Buffer.concat([buffered, chunk]);
    while (buffered.length >= 4) {
      const length = buffered.readUInt32BE(0);
      if (length > 16 * 1024 * 1024) throw new Error("frame too large");  // never trust the peer
      if (buffered.length < 4 + length) break;                            // wait for more bytes
      onMessage(buffered.subarray(4, 4 + length));
      buffered = buffered.subarray(4 + length);
    }
  };
}

// socket.on("data", frameReader((msg) => handle(JSON.parse(msg.toString("utf8")))));

The size check matters: without it, a hostile or buggy peer can claim a 4 GB message and make you buffer forever.

Go deeperWhy would anyone choose UDP?

Reliability has a price. When a TCP packet is lost, every later byte waits until the lost one is retransmitted, even bytes that have already arrived. For a live video call, a frame that arrives 300 ms late is worse than a missing frame. UDP lets the application decide what to do about loss.

UDP is also the escape hatch for new transports. Changing TCP requires kernel updates on billions of devices and middleboxes that understand the change. A protocol built on UDP can live in user space and ship with an app. That is exactly how QUIC, and therefore HTTP/3, was deployed (Module 5).

Where you will meet it

Line-delimited logs

Log shippers read files and sockets as streams and split on newlines, keeping any partial last line for the next read.

Redis and Postgres

Their wire protocols prefix values with lengths or type bytes so clients can read exact message boundaries from the TCP stream.

LLM streaming

Model APIs send Server-Sent Events, framed by blank lines. Clients that parse each network chunk as one event drop or corrupt tokens under load.

Words to know

Datagram
A self-contained UDP message delivered whole or not at all.
Socket
A file descriptor for one end of a network connection.
Framing
How a protocol marks where one message ends and the next begins.
Length prefix
A fixed-size field stating how many bytes of message follow.

Check yourself

Answer out loud before you open each one.

The sender writes two 10-byte messages. How many reads will the receiver need?

Any number from 1 to 20. TCP preserves order, not boundaries.

Name three framing strategies.

Delimiters, length prefixes and self-delimiting formats.

Why check the length prefix against a maximum?

A peer can claim an enormous length and make the reader allocate or wait without limit.

Try it

Start nc -l 9000 in one terminal and nc localhost 9000 in another. Write a Node client that sends three messages in a loop with no delay, and log each data event on a Node server. Count events versus messages.

A good place to start

Explore a pattern

A little guidance

Start with a pattern, read through a problem, and mark it solved when the idea clicks. Save anything you want to revisit.

New tutorials are added one at a time. Your bookmarks and solved checkmarks stay in this browser.

Explore the roadmap