When designing a distributed system, your microservices, databases, and client apps must constantly stream data packets across the network. While you will often use higher-level Application Layer protocols (Layer 7) like HTTP, WebSockets, or gRPC to handle this data, their actual network delivery is managed entirely by the Transport Layer (Layer 4).

At Layer 4, the choice reduces to a fundamental architectural trade-off between two core protocols:

  • TCP (Transmission Control Protocol)

  • UDP (User Datagram Protocol)

Choosing the wrong one can cause severe issues—either introducing lag to real-time features or causing data corruption in your storage layers.

One: Transmission Control Protocol (TCP)

TCP is a connection-oriented, highly reliable protocol designed to guarantee that data arrives exactly as it was sent, without errors or missing pieces.

The Mechanics of Trust: The Three-Way Handshake

Before TCP transmits a single byte of actual application data, it must establish a formal connection channel between the client and the server.

It does this through a process called the Three-Way Handshake.

Step 1: SYN (Synchronize)

The client sends a special packet with a random initial sequence number (ISN₍c₎) to the server, saying:

"I want to open a connection with you."

Step 2: SYN-ACK (Synchronize-Acknowledge)

The server receives the packet, generates its own sequence number (ISN₍s₎), and replies with a combined packet that confirms receipt of the client's request.

Step 3: ACK (Acknowledge)

The client receives the server's confirmation and sends back a final acknowledgment packet.

Once this three-step exchange completes, a stable, virtual connection is established, and data transmission can begin.

Core Reliability Features of TCP

Guaranteed Delivery (Retransmission)

Every packet sent requires an acknowledgment (ACK) from the receiver.

If a packet gets dropped by a faulty router mid-transit, the sender's timer expires, and it automatically retransmits the missing packet.

In-Order Sequencing

Networks are unpredictable; Packet 3 might arrive before Packet 2.

TCP attaches sequence numbers to every packet header, allowing the receiving operating system to reassemble them into the exact correct order before passing them up to your application code.

Flow & Congestion Control

TCP tracks network conditions.

If it detects that the receiving server's buffer is filling up, or that intermediate network routers are congested, it automatically slows down its transmission rate to prevent dropping packets.

Two: User Datagram Protocol (UDP)

UDP is a lightweight, connectionless protocol that prioritizes speed and efficiency over absolute reliability.

[Client] ───(Sends Datagram 1)───> [Server]
[Client] ───(Sends Datagram 2)───> [Server]
(No handshake, no confirmation)

The "Fire-and-Forget" Strategy

UDP does away with handshakes, acknowledgments, and connection maintenance entirely.

When an application layer sends data via UDP, the protocol wraps it in a packet (called a datagram) and fires it directly into the network.

The sender has no idea if the packet actually reached its destination, if it was dropped entirely, or if it arrived out of order.

It simply keeps pushing out data without waiting for feedback.

Why Use an Unreliable Protocol?

Zero Connection Overhead

Because there is no three-way handshake, UDP can stream data instantly without waiting for network round-trip setup times.

No Head-of-Line Blocking

In TCP, if Packet 2 is lost, Packet 3 and Packet 4 must sit in a queue waiting until Packet 2 is retransmitted and verified.

This delay is called Head-of-Line (HoL) Blocking.

UDP avoids this completely; if a packet drops, the system simply moves on to the next incoming datagram.

Minimal Packet Headers

A standard TCP header is complex and requires at least 20 bytes of metadata overhead.

A UDP header requires only 8 bytes, saving significant network bandwidth on high-volume streams.

Deep Comparison Matrix

Architectural MetricTCP (Transmission Control Protocol)UDP (User Datagram Protocol)
Connection StateConnection-oriented (Requires Handshake)Connectionless (Fire-and-forget)
Delivery Guarantee100% Guaranteed via RetransmissionsBest-effort (Packets can be lost)
Data OrderingStrictly ordered sequencingUnordered (Arrives as the network delivers it)
Data BoundaryContinuous Byte StreamDiscrete Datagram Packets
Header SizeLarge (Minimum 20 Bytes)Compact (Fixed 8 Bytes)
Transmission SpeedSlower (Throttled by congestion controls)Blazing Fast (No internal throttling)

High-Level Design Use Cases

Your choice of protocol directly defines how you handle real-world traffic patterns at scale.

Choose TCP When Data Integrity is Mandatory

If losing a single bit of data breaks your application state, you must use TCP.

Database Connections

Relational databases (PostgreSQL, MySQL) and caches (Redis) use TCP connections to ensure that queries and data mutations are processed accurately without corruption.

Financial Ledger Transactions

Payment processors and banking APIs rely on TCP's strict safety checks.

Web Browsing & REST APIs

Standard web traffic (HTTP/1.1 and HTTP/2) operates on top of TCP because an omitted file fragment can completely break an HTML page layout or corrupt an image download.

Choose UDP When Raw Speed Outweighs Minor Packet Loss

If your application can tolerate occasional missing data frames but cannot survive latency or buffering lag, choose UDP.

Live Video Streaming & VoIP

During a video call (Zoom, WebRTC), if a few network packets drop, the video might stutter briefly or skip a frame, which a human user barely notices.

However, if the app used TCP, the call would freeze completely while waiting for the dropped packets to retransmit, ruining the real-time experience.

Online Multiplayer Gaming

Fast-paced competitive games stream player coordinate updates via UDP.

Old positional data is useless; the game engine only cares about the absolute newest position packet.

Domain Name System (DNS)

DNS lookups use UDP because they require minimal, single-packet request-and-response sequences.

If a DNS query fails, the client simply times out and tries again, avoiding the connection overhead of TCP.

Modern Evolution Note: QUIC and HTTP/3

For decades, the industry treated the choice between TCP and UDP as a rigid trade-off.

However, modern high-availability web systems use HTTP/3, which runs on top of a protocol called QUIC.

QUIC uses UDP as its underlying transport layer to avoid TCP's slow handshake times and Head-of-Line blocking.

It then implements its own custom error correction and reliability features inside the application space.

This gives modern architectures the best of both worlds:

  • The blistering speed of UDP

  • The reliable data safety of TCP

Summary

  • TCP is a connection-oriented protocol that uses sequence numbers, retransmissions, and flow control to guarantee complete, in-order data delivery.

  • UDP is a connectionless protocol that avoids metadata checks, handshakes, and retransmissions to maximize data delivery speeds.

  • Systems architects use TCP for strict stateful applications like database transactions, and deploy UDP for real-time, lag-sensitive workflows like video streaming and multiplayer gaming.