TCP and UDP

IP gets packets to a host but promises nothing about whether they arrive, in order, or intact. The transport layer is where that gap is filled — or deliberately left open. TCP builds a reliable, ordered connection on top of unreliable IP; UDP declines to, trading guarantees for speed. Choosing between them, and understanding TCP's costs, is core backend knowledge.

The last post established that IP is best-effort: no delivery, order, or integrity guarantees, with reliability pushed to the endpoints. The transport layer is those endpoints, and it offers two very different protocols: TCP, which turns IP’s chaos into a reliable ordered stream, and UDP, which adds almost nothing and stays fast. This post covers how TCP achieves reliability (and what it costs), when UDP’s minimalism wins, and the ports that let both address individual processes.

Ports: addressing the process

First, the piece both share. IP addresses get a packet to the right host, but a host runs many processes (a web server, a database, an SSH daemon). Ports — 16-bit numbers — address the specific process/service on that host. The combination of IP address + port identifies a specific endpoint of a connection:

  IP address  → which host        (203.0.113.10)
  Port        → which process     (:443 for HTTPS, :22 for SSH, :5432 for Postgres)
  together    → a specific socket  (203.0.113.10:443)

Well-known ports conventionally identify services (80 HTTP, 443 HTTPS, 53 DNS, 22 SSH, 5432 Postgres). A TCP or UDP header carries source and destination ports, which is how the transport layer delivers data to the right process and how replies find their way back. Ports are why one server can run many services and one machine can hold many simultaneous connections — each is a distinct (address, port) pair.

TCP: reliability on top of unreliable IP

TCP (Transmission Control Protocol) provides a reliable, ordered, connection-oriented byte stream over best-effort IP. Everything it does exists to compensate for IP’s non-guarantees:

The result is the abstraction most applications want: a reliable, ordered pipe. HTTP, database connections, SSH — all run on TCP because they need every byte, in order. TCP hides IP’s unreliability so completely that application developers rarely think about packets at all.

The TCP handshake (and its cost)

TCP establishes a connection with a three-way handshake before any data is sent:

Client → Server:  SYN            "I want to connect (here's my sequence number)"
Server → Client:  SYN-ACK        "OK, and here's mine"
Client → Server:  ACK            "Got it — connected"
   → NOW data can flow

This exchange synchronizes both sides’ sequence numbers and confirms both can send and receive. It’s essential for reliability — but it costs a full round-trip (one there-and-back) before any data moves. That round-trip time (RTT) is pure latency added to every new TCP connection, and it’s the source of a major backend performance concern:

Connection teardown has its own exchange (and leaves connections briefly in a waiting state), but the handshake’s setup RTT is the cost that most shapes backend performance.

UDP: speed by omission

UDP (User Datagram Protocol) is TCP’s opposite: it adds almost nothing to IP. It provides ports (to reach a process) and a checksum (basic integrity) and that’s essentially it — no connection, no handshake, no acknowledgments, no retransmission, no ordering, no congestion control. UDP sends independent datagrams and does not care whether they arrive:

UDP is “fire and forget.” That sounds worse than TCP, but for the right use cases the absence of TCP’s machinery is exactly what you want.

Choosing TCP or UDP

The choice is driven by whether you need reliability/ordering or speed/low-latency more:

The mental model: TCP is the reliable, ordered, but heavier default; UDP is the fast, minimal, but bare option for when you’d rather build (or skip) reliability yourself. Most backend work is TCP; know UDP for real-time, DNS, and the QUIC-based future. The next post covers a protocol that famously uses UDP — DNS, the internet’s name resolution.

Key takeaways

Further reading

Sources & References

TCP vs UDP