The internet is not magic. It is a collection of agreements — agreements between machines, encoded in language so precise and unambiguous that billions of devices from a 1990s router in rural India to a 2024 server farm in Singapore can understand each other perfectly, every single time. These agreements are called protocols, and among them, the Transmission Control Protocol — TCP — is the one that made the modern internet feel reliable.
Most engineers, even experienced ones, think of TCP as a checkbox. "Use TCP for reliability, UDP for speed." That understanding is approximately as useful as knowing that a car runs on petrol. True, but it tells you nothing about why your engine knocks at high RPM, why your fuel efficiency halved in winter, or how a turbocharger changes the equation entirely. This document intends to correct that.
We will go from the packet-switched networks of the 1960s all the way to Google's QUIC protocol running under every YouTube video you watch today. By the end, TCP will not feel like a black box. It will feel like an old friend — one with interesting flaws, a remarkable history, and a complicated future.
To understand why TCP was invented, you need to understand the chaos it replaced. In the 1960s, communication between machines happened over circuit-switched networks — the same technology as telephone calls. When you called someone, a physical (or logical) circuit was established between you and them for the entire duration of the call. The circuit was exclusive, dedicated, and expensive. The resources it consumed were reserved whether you were speaking or sitting in silence.
This worked fine for voice. But for computer data, it was catastrophically wasteful. Computer communication is bursty — a machine might send nothing for ten seconds, then dump an entire file in 200 milliseconds. Dedicating a circuit for that pattern is like reserving a 16-lane highway for a single car that appears once per hour.
In 1961, MIT's Leonard Kleinrock published a thesis proposing that data could be broken into discrete blocks — packets — and each packet could be routed independently across a shared network. Paul Baran at RAND Corporation independently proposed the same concept in 1964, framing it explicitly as a military communication system that could survive nuclear strikes: because there was no single dedicated circuit to destroy, a distributed packet network had no single point of failure.
The insight was radical: rather than one sender and one receiver monopolizing a channel, thousands of senders could share the same wires. Each packet would carry a destination address and would be forwarded hop-by-hop through intermediate nodes (routers) toward its destination. Different packets from the same message could take entirely different paths and arrive out of order — or not at all, if a router along the way failed.
"A communication network should be designed from the outset to be able to operate even when portions of it have been destroyed." — Paul Baran, RAND Corporation, 1964
This was the intellectual foundation of everything that came after. But it immediately created a new problem: who is responsible for making sure all the packets actually arrive, in the right order, without corruption? On a circuit-switched network, the circuit itself provides those guarantees. On a packet-switched network, there is no circuit. There is only chaos, and whatever layer of software you build on top of it.
That software layer is TCP. But before TCP, there was ARPANET, and before reliability, there was just connectivity.
In 1969, the United States Department of Defense's Advanced Research Projects Agency funded a network connecting four universities: UCLA, Stanford Research Institute, UC Santa Barbara, and the University of Utah. This network, ARPANET, became the first operational packet-switched network in history. On October 29, 1969, a student at UCLA named Charley Kline attempted to type "LOGIN" to connect to SRI. The system crashed after the letters "L" and "O". The first message ever sent on the internet was "LO". Unintentionally poetic.
ARPANET ran on the Network Control Protocol (NCP), specified in 1970. NCP was a primitive protocol that assumed the underlying network was fundamentally reliable — because ARPANET was a single, controlled network with known, trustworthy nodes. It handled basic data transfer but had no mechanism for flow control, error recovery, or operating across heterogeneous networks. It worked fine as long as everything worked fine.
By 1973, it was becoming clear that ARPANET would not remain a single isolated network. Satellite links, radio packet networks, and other experimental networks were being developed. The dream was to interconnect them — to build an internet of networks. And NCP, with its assumptions of reliability, was completely unequipped for that world.
In May 1974, Vint Cerf (Stanford) and Bob Kahn (DARPA) published a paper in IEEE Transactions on Communications titled "A Protocol for Packet Network Intercommunication." This 19-page paper introduced the concept of a Transmission Control Program — which would later be split into two protocols: TCP and IP.
Their design was guided by four core principles that remain elegant even today:
The original Transmission Control Program was a single monolithic protocol. By 1978, it became apparent that different applications needed different properties. Real-time voice communication, for example, could tolerate some packet loss but could not tolerate the delays introduced by retransmission. It needed fast delivery, not guaranteed delivery.
The protocol was split into two distinct layers: IP (Internet Protocol) would handle routing — getting packets from source to destination across arbitrary networks — and TCP would handle reliability at the endpoints, sitting on top of IP and using its delivery service. This is the architectural decision that gave us TCP/IP as a phrase, and it was one of the most consequential software design decisions in history.
On January 1, 1983, ARPANET officially switched from NCP to TCP/IP. This date is widely considered the symbolic "birth of the internet." All nodes had to be upgraded simultaneously — it was called Flag Day, because there was no backward compatibility: you were either on TCP/IP or you were off the network. It was the largest forced software migration in computing history at the time.
In 1989, Tim Berners-Lee at CERN proposed the World Wide Web — a hypertext system built on top of TCP/IP. By 1993, Mosaic, the first graphical web browser, was released. The rest is the world you live in.
The internet is organized into layers. Each layer has a specific job and communicates only with the layers immediately above and below it. This layering concept is what allows a WiFi chip from Qualcomm, a fiber optic cable from a Japanese manufacturer, and a software application written in Python to all work together seamlessly. You do not need to know how the physical cable works to write a web server, and the cable does not need to know what a "web page" is.
The theoretical reference model is the OSI (Open Systems Interconnection) model, which defines 7 layers. The practical model used by the actual internet is the simpler TCP/IP model, which collapses these into 4 layers:
OSI Model (7 layers) TCP/IP Model (4 layers) ───────────────────────────────────────────────────────────── 7 Application ─┐ 6 Presentation ├──────→ Application (HTTP, FTP, DNS, SMTP...) 5 Session ─┘ ───────────────────────────────────────────────────────────── 4 Transport ────────→ Transport (TCP, UDP, SCTP, QUIC) ───────────────────────────────────────────────────────────── 3 Network ────────→ Internet (IP, ICMP, ARP) ───────────────────────────────────────────────────────────── 2 Data Link ─┐ 1 Physical ─┴──────→ Network Access (Ethernet, WiFi, Fiber...) ─────────────────────────────────────────────────────────────
TCP lives at the Transport layer. Its job is to take the unreliable, out-of-order, packet-dropping service provided by IP and transform it into a reliable, ordered, byte-stream service for the Application layer above it. An HTTP server doesn't think about packets — it thinks about a continuous stream of bytes arriving in order. TCP is the illusion-maker that creates that stream.
TCP is defined by five properties that together make it what it is. Removing any one of them fundamentally changes the protocol:
These five properties have a cost: overhead. Every guarantee requires extra communication, extra state, extra time. This is the fundamental tension in network protocol design, and understanding it is the key to understanding why UDP, QUIC, and every other transport protocol exists.
TCP is fundamentally a state machine — a protocol that moves through defined states in response to events. To understand it fully, we must walk through each phase of a TCP connection's lifecycle: establishment, data transfer, and termination.
Before a single byte of application data is exchanged, TCP performs a three-way handshake to establish the connection. This handshake serves several critical functions: it synchronizes sequence numbers between client and server, it verifies that both parties are reachable and willing to communicate, and it establishes the initial parameters (window sizes, options) for the connection.
Client Server │ │ │ ── SYN ──────────────────────────► │ │ seq=x, SYN=1 │ │ │ │ ◄── SYN-ACK ────────────────────── │ │ seq=y, ack=x+1, SYN=1, ACK=1 │ │ │ │ ── ACK ──────────────────────────► │ │ seq=x+1, ack=y+1, ACK=1 │ │ │ │ ═══ Connection Established ═══ │ │ │
Step 1 (SYN): The client picks a random initial sequence number x and sends a segment with the SYN (synchronize) flag set. "I want to connect, and I'll start numbering my bytes from x."
Step 2 (SYN-ACK): The server acknowledges the client's SYN (ack = x+1, meaning "I received everything up to byte x, send me byte x+1 next") and simultaneously sends its own SYN with its own random initial sequence number y. Two handshakes combined into one segment — hence the hyphen.
Step 3 (ACK): The client acknowledges the server's SYN (ack = y+1). The connection is now fully established in both directions. This final ACK carries no data.
Why is the initial sequence number random? To prevent an attacker who was previously eavesdropping on an old connection from injecting forged packets into a new one. Sequence number prediction was a real attack vector in TCP's early days.
Every byte in a TCP stream is assigned a unique sequence number. This is the mechanism that enables ordered, reliable delivery. If the client sends 1000 bytes starting at sequence number 5000, those bytes are numbered 5000 through 5999. The server, upon receiving them, sends an ACK with acknowledgment number = 6000, meaning "I've received everything through 5999, send me 6000 next."
This cumulative acknowledgment model has an important implication: if segment 3 is lost but segments 1, 2, 4, and 5 arrive, the receiver must buffer 4 and 5 and wait. It cannot acknowledge beyond the gap. The sender, seeing no acknowledgment for segment 3's data, will eventually retransmit it. Only then can the receiver acknowledge through segment 5. This is what "ordered delivery" actually means at the protocol level — there is no magic, just patient buffering and retransmission.
Imagine you are sending files to a friend who has a small inbox. If you send faster than they can read, their inbox overflows and messages are lost. Flow control solves exactly this problem in TCP: it prevents the sender from overwhelming the receiver's buffer.
TCP uses a sliding window mechanism. The receiver advertises a window size (in bytes) in every ACK it sends back. This number represents how much free space remains in the receiver's buffer. The sender is only allowed to have that many unacknowledged bytes "in flight" at any given moment.
Sender's view (window size = 4 segments): │ Sent & ACK'd │ Sent, not ACK'd │ Can send │ Cannot send yet │ │──────────────│─────────────────│────────────│──────────────────│ │ 1 2 3 4 │ 5 6 7 │ 8 9 10 │ 11 12 13 14 │ ↑ ↑↑ ↑ SND.UNA SND.NXT Window limit As ACKs arrive, the window "slides" right, allowing more data to be sent.
When the receiver's buffer fills up completely, it advertises a window size of 0. The sender stops transmitting and sends periodic window probe segments to check when the receiver is ready again. This is called a zero-window condition and it can be a significant performance bottleneck in practice.
Flow control manages the relationship between sender and receiver. Congestion control manages the relationship between the sender and the network. It answers the question: how do I avoid being the asshole who sends so much data that every router between me and the destination starts dropping packets for everyone?
TCP congestion control is one of the most beautiful pieces of distributed algorithm design ever created. There is no central authority telling each connection how much bandwidth it can use. Instead, each TCP connection independently infers the state of the network by observing packet loss and delay, and adjusts its sending rate accordingly. The result — remarkably — is that hundreds of millions of TCP connections sharing the same backbone links reach a stable, fair equilibrium without coordination.
When a new connection begins, the sender knows nothing about network capacity. It starts with a congestion window (cwnd) of 1 MSS (Maximum Segment Size, typically 1460 bytes). For every ACK received, cwnd doubles. This is exponential growth, but it starts small. The goal is to quickly find the available bandwidth without initially over-committing.
Once cwnd reaches a threshold (ssthresh), exponential growth switches to linear growth — cwnd increases by 1 MSS per round-trip time rather than doubling. The connection is now cautiously probing for bandwidth ceiling.
When a packet is lost, TCP detects it in two ways: a timeout (no ACK received within a calculated retransmission timeout period) or three duplicate ACKs (the receiver keeps acknowledging the same sequence number because a gap exists). The responses differ:
The original algorithm described above is called TCP Reno. Modern operating systems use more sophisticated variants:
TCP CUBIC (default on Linux since 2006) uses a cubic function to determine window growth rather than purely linear, allowing faster recovery and better utilization of high-bandwidth, high-latency links (such as intercontinental connections).
TCP BBR (Bottleneck Bandwidth and RTT, developed by Google, 2016) takes a fundamentally different approach: instead of reacting to packet loss, it models the network's delivery rate and RTT directly to maintain optimal sending rate. It is significantly better on paths with shallow buffers or satellite links where traditional loss-based algorithms behave poorly. BBR is what Google uses on their infrastructure and it is increasingly being deployed across the web.
Every TCP segment carries a 16-bit checksum computed over the header and data. If a segment arrives with a mismatched checksum — indicating corruption in transit — it is silently discarded. No error message is sent. The sender's retransmission timer will eventually fire and resend the segment.
The retransmission timeout (RTO) is calculated dynamically using an exponentially weighted moving average of observed round-trip times. TCP maintains two values: SRTT (smoothed RTT) and RTTVAR (RTT variance). The RTO = SRTT + 4*RTTVAR. If retransmission is required, the RTO doubles each time (exponential backoff), up to a maximum — typically 64 seconds. After a configured number of retries, the connection is declared dead.
Because a TCP connection is full-duplex (each side can send independently), closing it requires each direction to be shut down independently. This results in a four-way process:
Client Server │ │ │ ── FIN ──────────────────────────► │ │ seq=u, FIN=1 │ Client done sending │ │ │ ◄── ACK ────────────────────────── │ │ ack=u+1 │ Server ACKs the FIN │ │ (Server may still send data) │ ◄── FIN ────────────────────────── │ │ seq=v, FIN=1 │ Server done sending │ │ │ ── ACK ──────────────────────────► │ │ ack=v+1 │ Client ACKs the server's FIN │ │ │ ⏱ Client enters TIME_WAIT (2*MSL) │ │ ~60-120 seconds │
The client enters TIME_WAIT for twice the Maximum Segment Lifetime (typically 60–120 seconds) after sending the final ACK. This is to handle the case where the server's FIN was duplicated — if the server didn't receive the final ACK, it will retransmit its FIN. If the client had already closed completely, it would respond with a RST, which would confuse the server into thinking something went wrong. TIME_WAIT prevents this. It's also why rapidly restarting a server on the same port can fail with "address already in use" — TIME_WAIT sockets occupy that port-pair.
A TCP segment consists of a header followed by optional options followed by data. The header is a minimum of 20 bytes and a maximum of 60 bytes (due to the 4-bit data offset field supporting up to 15×4 = 60 bytes). Understanding every field is essential for debugging, security analysis, and performance tuning.
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 ┌───────────────────────────────┬───────────────────────────────┐ │ Source Port │ Destination Port │ ├───────────────────────────────┴───────────────────────────────┤ │ Sequence Number │ ├───────────────────────────────────────────────────────────────┤ │ Acknowledgment Number │ ├───────────┬───┬───────────────┬───────────────────────────────┤ │Data Offset│Rsv│ Control Bits │ Window Size │ ├───────────────────────────────┬───────────────────────────────┤ │ Checksum │ Urgent Pointer │ ├───────────────────────────────┴───────────────────────────────┤ │ Options (0–40 bytes) │ ├───────────────────────────────────────────────────────────────┤ │ Data Payload │ └───────────────────────────────────────────────────────────────┘
Source Port (16 bits): The port number of the sending application. Ports 0–1023 are "well-known" (reserved for system services), 1024–49151 are registered, 49152–65535 are ephemeral (dynamically assigned by the OS for client connections).
Destination Port (16 bits): The port number of the receiving application. HTTP is port 80, HTTPS is 443, SSH is 22, FTP is 21, SMTP is 25, DNS is 53 (though DNS primarily uses UDP).
Sequence Number (32 bits): If SYN is set, this is the initial sequence number. Otherwise, it is the sequence number of the first data byte in this segment. With 32 bits, sequence numbers wrap around at 4GB — on a high-speed link, this can happen quickly enough to cause ambiguity, which the TCP Timestamps option was designed to address.
Acknowledgment Number (32 bits): The sequence number of the next byte the receiver expects. Only valid if the ACK control bit is set.
Data Offset (4 bits): The size of the TCP header in 32-bit words. Used to find where the data begins. Minimum value is 5 (20 bytes), maximum is 15 (60 bytes).
Control Bits (9 bits): The famous TCP flags:
SYN — Synchronize sequence numbers. Used only during connection establishment.ACK — Acknowledgment field is valid. Set on virtually all segments after the initial SYN.FIN — No more data from sender. Initiates connection teardown.RST — Reset the connection. Abort immediately, no graceful teardown.PSH — Push: deliver data to application immediately, don't buffer.URG — Urgent pointer field is significant (rarely used in practice).ECE & CWR — Explicit Congestion Notification flags (RFC 3168).NS — Nonce Sum (ECN-related, RFC 3540).
Window Size (16 bits): The receiver's current receive window — how many bytes the receiver can accept beyond the acknowledgment number. Maximum value without scaling is 65,535 bytes. The TCP Window Scale option (RFC 1323) allows this to be multiplied by a power of 2, supporting windows up to 1GB — essential for high-speed long-distance links where the bandwidth-delay product is enormous.
Checksum (16 bits): Computed over a pseudo-header (source IP, destination IP, protocol, TCP length) plus the entire TCP segment. Catches bit-level corruption in transit.
Common TCP Options: MSS (Maximum Segment Size — the largest payload the endpoint will accept), Window Scale, SACK (Selective Acknowledgment — allows acknowledging non-contiguous blocks, vastly improving performance over lossy links), Timestamps (for RTT measurement and PAWS — Protection Against Wrapped Sequence numbers), and TFO (TCP Fast Open — allows data in the SYN, reducing connection overhead for repeat connections).
TCP is defined by RFC 793 as an explicit state machine. Every TCP socket is always in exactly one of eleven defined states. Understanding these states is critical for debugging — when you see a socket stuck in CLOSE_WAIT or a flood of TIME_WAIT sockets on your server, you need to know what that means and why.
TCP STATE MACHINE ───────────────────────────────────────────────────────────── CLOSED ↓ ↑ passive close open ↓ LISTEN ← Server waiting for connection ↓ (rcv SYN, send SYN-ACK) SYN_RECEIVED ↓ (rcv ACK) ESTABLISHED ← Data flows here ↙ ↘ FIN_WAIT_1 CLOSE_WAIT ← Passive closer ↓ (rcv FIN) ↓ (app calls close) FIN_WAIT_2 LAST_ACK ↓ (rcv FIN) ↓ (rcv ACK) TIME_WAIT CLOSED ↓ (2*MSL timer) CLOSED ─────────────────────────────────────────────────────────────
A few states deserve special attention in practice:
CLOSE_WAIT is the state a server enters when the client sends a FIN but the server application hasn't called close() on the socket yet. A server with thousands of sockets stuck in CLOSE_WAIT almost always has a bug: the application is not closing connections it's done with. This leaks file descriptors and will eventually crash the process.
TIME_WAIT, as discussed, is the 2-MSL wait after the active closer sends the final ACK. In high-traffic servers handling many short-lived connections (like load balancers), TIME_WAIT sockets can number in the hundreds of thousands and exhaust the port space. Solutions include SO_REUSEADDR, tcp_tw_reuse, and the shift toward connection pooling and persistent connections.
SYN_RECEIVED is where SYN flood attacks land. An attacker sends thousands of SYN packets with spoofed source IPs. The server allocates state for each one and waits for the final ACK that will never come. SYN cookies solve this by encoding connection state into the sequence number itself, allowing the server to remain stateless until the ACK arrives and is verified.
The User Datagram Protocol (UDP), defined in RFC 768 by Jon Postel in 1980, is six pages long. The RFC 793 defining TCP is 85 pages. The difference in length is a difference in philosophy. UDP is, deliberately and proudly, a minimal protocol. It provides exactly one service: sending a datagram from one host to another. That's it.
UDP has no connection establishment. No sequence numbers. No acknowledgments. No flow control. No congestion control. No ordering guarantees. No reliability guarantees. You fire a packet at an IP address and port, and you have absolutely no idea whether it arrived.
This sounds like a step backward. It is not. UDP's minimal overhead makes it dramatically faster for specific use cases, and it hands control to the application, which can implement exactly as much reliability as it needs — no more, no less.
0 1 2 3 ┌───────────────────────────────┬───────────────────────────────┐ │ Source Port │ Destination Port │ ├───────────────────────────────┼───────────────────────────────┤ │ Length │ Checksum │ ├───────────────────────────────┴───────────────────────────────┤ │ Data Payload │ └───────────────────────────────────────────────────────────────┘ Total header: 8 bytes. TCP header: minimum 20 bytes.
Real-time audio/video (VoIP, video calls, live streaming): A retransmitted audio packet that arrives 300ms late is useless. It's better to skip the damaged audio and continue. Zoom, Skype, FaceTime, and WebRTC all use UDP (with their own application-level quality management).
Online gaming: In a first-person shooter, your current position update is immediately obsolete the moment a newer one is available. Retransmitting old position data will actually make the game play worse by causing lag. Games send position/state updates over UDP at 30–120 times per second and simply accept that some will be lost.
DNS queries: A DNS query is a single question expecting a single short answer. The overhead of establishing a TCP connection (1 RTT minimum) before sending a 40-byte query and receiving a 100-byte response is absurd. UDP sends both in single packets with zero setup cost. (DNS over TCP is used for large responses and zone transfers.)
IoT and embedded devices: Low-power devices transmitting small sensor readings cannot afford the CPU cycles and memory for TCP state management.
Multicast and broadcast: TCP is inherently unicast — it connects one sender to one receiver. UDP supports sending a single packet to multiple receivers simultaneously (multicast), which is fundamental to IPTV, financial market data feeds, and certain distributed systems.
The TCP/UDP binary is not the whole story. Decades of engineering have produced a rich ecosystem of transport protocols, each designed to address specific limitations or serve specific use cases. Most are not widely deployed — the internet's installed base strongly resists change — but understanding them reveals the design space TCP was always navigating.
Defined in RFC 4960 (2007), SCTP was originally designed for telephony signaling — carrying SS7 protocol messages over IP. It combines the best of TCP and UDP in interesting ways. Like TCP, it provides reliable, ordered delivery and connection setup. Like UDP, it supports multiple independent streams within a single connection — meaning head-of-line blocking in one stream doesn't affect others. It also supports multi-homing: a single SCTP association can span multiple IP addresses on both endpoints, providing automatic failover if one network interface fails.
SCTP never achieved mainstream adoption largely because it cannot traverse NATs easily (NAT devices don't understand SCTP associations) and because TCP is so deeply embedded in middleware, firewalls, and operating systems. It is used heavily in telecom core networks but almost invisible to web developers.
RFC 4340 (2006). DCCP is essentially UDP with congestion control added. It provides unreliable, unordered delivery (like UDP) but negotiates and applies a congestion control algorithm (like TCP). This was designed for streaming media: you want to transmit data at a rate the network can handle, but you don't need to retransmit old data. DCCP is almost invisible in deployment today — the use cases it targets are served by application-level logic built on top of UDP, or by QUIC.
RFC 8684 (2020). Multipath TCP is a TCP extension that allows a single logical TCP connection to use multiple physical network paths simultaneously. Your phone, connected to both WiFi and LTE, could split a large download across both interfaces, getting combined throughput. Or a data center server could load-balance traffic across multiple network cards at the TCP level. Apple uses MPTCP in iOS for Siri (allowing seamless WiFi-to-LTE handoff mid-conversation) and in certain app-to-server connections. It is increasingly supported in Linux kernels and is genuinely promising for mobile networking where path diversity is common.
WebSockets (RFC 6455, 2011) are not a transport protocol in the same sense — they run over TCP. They were created to solve a specific limitation of HTTP: HTTP is a request-response protocol, where the client always initiates. If a server wants to push data to a client (live notifications, stock prices, chat messages), HTTP required ugly workarounds like long-polling. WebSockets establish a persistent, bidirectional channel over a single TCP connection, with a lightweight framing layer. The initial handshake is an HTTP Upgrade request, meaning WebSockets work transparently through HTTP-aware proxies and firewalls. Almost every real-time web application — Slack, Discord, Google Docs collaborative editing — relies on WebSockets.
HTTP/1.1 (the protocol your browser used from 1997 to roughly 2015) has a fundamental inefficiency: each request-response pair must complete before the next one begins on a given TCP connection (without pipelining, which was poorly supported). This means loading a web page with 80 resources requires either 80 sequential requests (very slow) or 80 parallel TCP connections (wasteful, and TCP slow start penalizes each one).
HTTP/2 (RFC 7540, 2015) solves this with multiplexing: multiple request-response pairs interleaved on a single TCP connection simultaneously. It also introduces header compression (HPACK), server push, and stream prioritization. HTTP/2 is a massive improvement. But it still runs over TCP, which means it inherits TCP's fundamental limitation: one lost packet blocks all multiplexed streams until retransmission completes. This is the infamous head-of-line blocking problem — the Achilles' heel of TCP-based multiplexing.
This deserves its own chapter (see Chapter 14), but in context: QUIC (originally developed by Google, standardized in RFC 9000, 2021) is a new transport protocol implemented entirely in user space, running over UDP. It reimplements TCP's reliability guarantees with several crucial improvements:
HTTP/3 is HTTP semantics running over QUIC instead of TCP. As of 2024, HTTP/3 is supported by about 30% of the top websites globally, including every major Google property, Cloudflare-fronted sites, and a growing portion of the web. The transport revolution is not coming — it is already happening.
| Protocol | Layer | Reliable | Ordered | Congestion Ctrl | Encrypted | Multiplexed Streams | Primary Use |
|---|---|---|---|---|---|---|---|
| TCP | Transport | Yes | Yes | Yes | No | No | General purpose |
| UDP | Transport | No | No | No | No | No | Real-time, gaming, DNS |
| SCTP | Transport | Yes | Yes | Yes | No | Yes | Telecom signaling |
| DCCP | Transport | No | No | Yes | No | No | Streaming (niche) |
| QUIC | Transport* | Yes | Per-stream | Yes | Always | Yes | Web (HTTP/3) |
| WebSockets | App (over TCP) | Yes | Yes | Via TCP | Via TLS | No | Real-time web |
| HTTP/2 | App (over TCP) | Yes | Yes | Via TCP | De facto | Yes (w/ HoL) | Web |
| HTTP/3 | App (over QUIC) | Yes | Per-stream | Yes | Always | Yes (no HoL) | Web (current) |
TCP is not an abstract concept. It is the reason you can read this document, check your email, pull code from GitHub, query a database, and talk to a Kubernetes cluster. Every one of those actions involves TCP connections, TCP state machines, TCP handshakes, and TCP congestion windows. Let's be concrete.
Every time you visit a website, your browser opens a TCP connection to port 443 (HTTPS) on the web server. Before HTTP/2, a browser might open 6–12 parallel TCP connections per domain to work around HTTP/1.1's serialization limitations. With HTTP/2, a single TCP connection per origin carries all resources. With HTTP/3, that single connection is QUIC. The TLS handshake that follows the TCP handshake adds another round trip (TLS 1.2) or zero additional round trips if session resumption is available (TLS 1.3). This is why TCP connection latency matters so much — every millisecond of connection establishment is directly user-visible in page load time.
Email uses TCP at every step. Your email client (Gmail, Outlook, Thunderbird) connects via IMAP (port 993) or POP3 (port 995) over TCP to download messages. When sending, SMTP (port 587 with STARTTLS, or 465 with implicit TLS) carries the message over TCP from your client to your mail server, and then from mail server to recipient mail server. The SMTP conversation is an explicit back-and-forth dialog: EHLO, MAIL FROM, RCPT TO, DATA — each step acknowledged before the next proceeds. This is TCP's reliability guarantee at the application protocol level made visible.
FTP (File Transfer Protocol) is historically interesting: it uses two TCP connections. The control connection (port 21) carries commands and responses. For each file transfer, a separate data connection is opened (port 20 in active mode, or a negotiated port in passive mode). This two-channel design causes problems with NAT and firewalls (which must track both connections), which is why SFTP (SSH File Transfer Protocol) over a single SSH TCP connection, and SCP (Secure Copy, also over SSH), became preferred in practice.
MySQL (port 3306), PostgreSQL (port 5432), MongoDB (port 27017), Redis (port 6379) — all communicate over TCP. The database connection pools that web frameworks maintain are, at the infrastructure level, pools of established TCP connections that are reused across requests to avoid paying the handshake cost repeatedly. When your connection pool is exhausted, new requests wait — and what they're waiting for is the availability of a TCP socket, not the database itself.
Every time you ssh user@server, a TCP connection is established to port 22. Over this single, persistent, encrypted TCP stream, SSH multiplexes an interactive terminal session, file transfers (scp, sftp), and port forwarding tunnels. SSH's ability to tunnel other TCP connections through itself (local forwarding, remote forwarding, dynamic SOCKS proxy) is a direct consequence of TCP's reliable, ordered byte-stream model.
When you git push to GitHub, a TCP connection is established (either over HTTPS to port 443, or over SSH to port 22, or over the Git protocol to port 9418). The entire pack object transfer — delta-compressed, binary, potentially gigabytes — happens over this single TCP connection. TCP's reliability ensures that every byte of your repository data arrives intact and in order; without this, silent corruption of commit objects would be catastrophic.
Modern distributed systems are TCP connections all the way down. gRPC (Google's RPC framework used by virtually every microservices architecture) runs over HTTP/2, which runs over TCP (or QUIC for gRPC-Web). Apache Kafka's protocol is a custom binary protocol over TCP — producers, consumers, and brokers communicate via persistent TCP connections with their own application-level reliability semantics layered on top. Kubernetes API communication, etcd's Raft consensus protocol, kubelet-to-apiserver heartbeats — all TCP, all the time.
The FIX Protocol (Financial Information eXchange), used for electronic trading, runs over TCP. A TCP connection between a trading firm and an exchange carries orders, order acknowledgments, fills, and cancellations. TCP's reliability guarantee here is not just convenient — it's legally required. A lost order acknowledgment is a compliance problem. The ordering guarantee ensures that cancel messages are always processed after the orders they cancel.
Market data feeds, interestingly, often use UDP multicast — because reliability is sacrificed for the lower latency and broadcast efficiency of UDP. A missed market data tick is annoying; a delayed one is sometimes worse than missing it.
TCP was designed in an era of trusted academic networks. Its security assumptions were largely wrong by the time the internet became a public, adversarial place. Understanding TCP's attack surface is essential for any engineer building or securing networked systems.
An attacker sends thousands of SYN packets per second with spoofed source IP addresses. The server allocates a connection control block (roughly 280 bytes of kernel memory per connection) for each one and sends SYN-ACKs to addresses that don't exist. The connection sits in SYN_RECEIVED state until a timeout (typically 75 seconds). With enough forged SYNs, the server exhausts its connection table and cannot accept legitimate connections.
The standard defense is SYN cookies: the server encodes the connection state (source/destination addresses, ports, timestamp, a secret hash) into the initial sequence number of the SYN-ACK. No state is allocated. When the ACK arrives, the server reconstructs the state from the sequence number and validates it. If the source was spoofed, no ACK ever arrives, and no resources were consumed.
If an attacker can observe a TCP connection (e.g., on the same network segment), they can inject forged packets with the correct sequence number and take over the session. This was a serious vulnerability before TLS became standard. With TLS, injected packets fail the cryptographic integrity check and are discarded, making session hijacking effectively impossible on encrypted connections.
A forged RST packet with a valid sequence number causes a TCP receiver to immediately tear down the connection. This requires knowing (or guessing) the current sequence number, which is trivial for an on-path attacker (ISP, router, man-in-the-middle) and difficult but not impossible for an off-path attacker. The Great Firewall of China uses RST injection to terminate connections to blocked content — your connection to a blocked host receives RST packets injected by a router, and your OS politely closes the connection.
TCP's connection-oriented nature makes it ideal for port scanning. A TCP connect scan completes the full three-way handshake — if the target responds with SYN-ACK, the port is open; if with RST, closed. A SYN scan (half-open) sends SYN and analyzes the response without completing the handshake, making it stealthier (no entry in the application's accept queue). Tools like nmap are built on these primitives.
A connection in TIME_WAIT state can be prematurely terminated by a forged RST with a sequence number within the receiver's window. RFC 1337 describes this but it is rarely mitigated in practice. On modern kernels, time_wait_assassination_holes are plugged by proper handling of RST segments during TIME_WAIT.
In the early internet, initial sequence numbers were predictable (incremented globally). An off-path attacker could predict the ISN, forge a SYN-ACK to a spoofed connection, and complete a three-way handshake on behalf of a victim. This attack was used in the famous Kevin Mitnick attack on Tsutomu Shimomura in 1994. Modern TCP implementations use cryptographically random ISNs (typically derived from a secret + time + 4-tuple hash), making prediction infeasible.
Understanding TCP's internals has immediate, measurable impact on the performance of real systems. This chapter covers the practical tuning knobs that every backend engineer should know.
The bandwidth-delay product (BDP) = link bandwidth × round-trip time. This is the amount of data "in flight" when a pipe is fully utilized. A 1 Gbps link with 100ms RTT has a BDP of 12.5 MB. Your TCP send and receive buffers must be at least this large to fully utilize the link. Default Linux socket buffers are often 128KB, which would throttle that same link to 128KB / 0.1s = 10 Mbps — a 100x limitation. Modern kernels auto-tune buffers (tcp_moderate_rcvbuf), but knowing this math tells you why buffer sizes matter enormously on high-latency intercontinental links.
Defined by John Nagle in 1984, Nagle's algorithm buffers small TCP writes and sends them as a single larger segment when either the buffer fills to MSS size or all outstanding data has been acknowledged. This prevents "tinygram" flooding — a Telnet client sending one packet per keystroke. However, it introduces latency for interactive applications. Most high-performance web servers disable it with TCP_NODELAY. The pathological interaction of Nagle's algorithm with delayed ACKs (where the receiver waits up to 200ms before ACKing to allow piggybacking) can cause 200ms artificial delays in request-response protocols — a famous foot-gun.
Standardized in RFC 7413 (2014), TFO allows application data to be sent in the SYN packet on repeated connections, saving one full round trip. The first connection generates a server-issued cookie stored by the client. Subsequent connections include the cookie in the SYN, the server validates it, and can respond with data before the handshake completes. Google measured a 10–40% reduction in HTTP response times with TFO. TFO is supported on Linux and macOS but deployment is limited due to middlebox interference.
# Increase TCP socket buffer sizes (for high-BDP links) net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 # Enable selective acknowledgments (critical for lossy links) net.ipv4.tcp_sack = 1 # Enable timestamps (for RTT measurement + PAWS) net.ipv4.tcp_timestamps = 1 # Congestion control algorithm net.ipv4.tcp_congestion_control = bbr # SYN flood defense net.ipv4.tcp_syncookies = 1 # Reuse TIME_WAIT sockets for new connections (servers only) net.ipv4.tcp_tw_reuse = 1 # Number of SYN retransmits before giving up net.ipv4.tcp_syn_retries = 3
HTTP Keep-Alive (persistent connections) was one of the most impactful performance improvements in web history — simply reusing an existing TCP connection across multiple HTTP requests instead of creating a new one each time. The handshake cost (1 RTT at minimum, plus TCP slow start's initial conservatism) is paid only once, amortized across all requests on that connection. Modern reverse proxies (nginx, HAProxy), API gateways, and database drivers all maintain connection pools for precisely this reason.
The naive rule is "TCP for reliability, UDP for speed." The real answer is more nuanced. The question to ask is: what does my application need, and what does it want to pay for it?
| Dimension | Choose TCP | Choose UDP |
|---|---|---|
| Data integrity | Every byte must arrive correctly (files, databases, email) | Some loss acceptable (audio, video, game state) |
| Ordering | Order matters (HTTP, SQL, SSH) | Latest state supersedes old (position updates, telemetry) |
| Latency | Latency tolerance exists (bulk transfer) | Sub-millisecond latency required (trading, gaming) |
| Retransmission | Old data is still useful when it arrives late | Old data is useless when it arrives late |
| Broadcast/Multicast | One-to-one communication | One-to-many communication |
| Custom reliability | Standard TCP reliability sufficient | Need non-standard reliability (partial ordering, selectively reliable) |
| Network congestion | Must be a "good citizen" of the network (public internet) | Controlled environment where you manage bandwidth (internal LAN) |
The most important insight: UDP does not mean "unreliable." It means "you are responsible for whatever reliability properties you need." Games, WebRTC, QUIC, and DNS all use UDP with varying degrees of application-level reliability logic on top.
As of 2024, TCP carries the overwhelming majority of internet traffic by volume. HTTP/2 adoption stands at over 60% of websites. TLS 1.3 is now the dominant handshake protocol, reducing connection establishment to 1-RTT and enabling 0-RTT session resumption. TCP BBR is deployed on Google, Cloudflare, and a significant fraction of high-traffic servers, improving throughput on high-BDP links by 2–25x compared to CUBIC in controlled measurements.
Several significant changes have occurred in TCP's recent history. RFC 9293 (2022) replaced the original RFC 793 as the canonical TCP specification, consolidating decades of errata and clarifications into a single authoritative document — a sign that TCP is mature enough to warrant a full specification cleanup rather than patchwork amendments.
eBPF (extended Berkeley Packet Filter) has opened a new era of TCP observability and programmability. Engineers at Netflix, Facebook, and Cloudflare can now insert custom logic into the Linux TCP stack at runtime, without modifying the kernel — monitoring per-connection metrics, implementing custom congestion algorithms, and even routing decisions, all in sandboxed kernel-space programs that are provably safe.
The rise of cloud computing has subtly changed TCP's operational profile. In data center environments — where most internet traffic either originates or terminates — round-trip times are sub-millisecond rather than the hundreds of milliseconds of transcontinental links. TCP slow start, designed to be conservative on long-distance links, is unnecessarily cautious in these environments. Google's work on TCP initial congestion window size (increasing it from 3 segments to 10 in RFC 6928) shaved measurable latency from every short TCP connection initiated from Google's servers.
The death of TCP has been proclaimed many times and been wrong every time. This time, though, the challenge is categorically different: QUIC is not a competing standard seeking adoption — it is already deployed at internet scale. Approximately 12–15% of all internet traffic globally is currently QUIC. Every Google service, every Cloudflare-fronted website, every Meta property uses QUIC. YouTube uses QUIC for video delivery. Chrome uses QUIC for most connections to Google's infrastructure. This is not a promise; it is a present reality.
Previous attempts to replace TCP (SCTP, DCCP, MPTCP as a replacement) all suffered the same problem: they required support from every network device between sender and receiver. Firewalls, NAT devices, and load balancers are programmed to handle TCP and UDP. They may drop, mangle, or block novel transport protocols. QUIC sidesteps this entirely by running as payload inside UDP datagrams. To every middlebox in the network, QUIC looks like normal UDP traffic. All the QUIC logic — reliability, flow control, congestion control, multiplexing — happens in user space on the endpoints, with no network cooperation required.
This is the end-to-end principle Cerf and Kahn articulated in 1974, taken to its logical extreme: the network is maximally dumb, the intelligence is maximally at the endpoints. QUIC is, in a sense, the fullest realization of the original TCP design philosophy.
The reason QUIC had to run over UDP is a phenomenon called TCP ossification: the accumulated assumptions about TCP baked into every piece of network equipment on the internet. TCP header fields that RFC 793 marked "must be zero" are dropped by middleboxes that enforce it. TCP options that didn't exist in 1981 are stripped by firewalls. ECN bits that should pass through are cleared by load balancers. This accumulated rigidity makes it effectively impossible to extend or modify TCP in any meaningful way — any new TCP feature must survive traversal through millions of devices, most of which will never be updated. QUIC's encryption of transport-layer headers (including sequence numbers, connection IDs, and congestion control signals) is a deliberate strategy to prevent middleboxes from interfering with future evolution. A middlebox cannot selectively zero a field it cannot read.
TCP will not disappear. The installed base is too vast, the use cases too numerous, and the protocol too well-understood. Embedded systems, IoT devices, legacy enterprise software, and much of the inter-system communication within operating systems will remain TCP for decades. But the TCP share of high-visibility, user-facing web traffic will continue declining in favor of QUIC/HTTP/3. The trajectory is clear: TCP will become increasingly a "background" protocol — reliable, invisible, unglamorous — while QUIC becomes the protocol of choice for anything latency-sensitive, mobile, or actively developed.
Within QUIC, the frontier of active research includes: multipath QUIC (routing streams across multiple network paths, as MPTCP does for TCP), unreliable datagrams over QUIC (RFC 9221, allowing UDP-like unreliable delivery within a QUIC connection, useful for WebRTC media), and WebTransport (a browser API for low-latency bidirectional communication over QUIC, intended to eventually replace WebSockets for applications where latency matters most).
After fourteen chapters, the right way to crystallize this knowledge is to reduce it to intuitions — the immediate, pattern-matching understanding that separates engineers who understand TCP from those who have merely read about it.
When you open a TCP connection, both sides agree to a contract: "Everything I send will arrive at you, in order, without corruption, or I will keep trying until it does or until the connection dies." This contract has a cost: latency, overhead, and complexity. Every TCP feature exists to enforce some aspect of this contract in the face of an unreliable network.
Slow start is conservative by design — TCP knows nothing about the network and starts gently. As it receives acknowledgments, it accelerates. Congestion avoidance then probes carefully for the ceiling. Connection teardown has intentional delays (TIME_WAIT). If your application is making many short connections, you are constantly paying the "slow at the start" cost. This is why persistent connections, connection pooling, and HTTP/2 multiplexing are not nice-to-haves but fundamental performance requirements.
TCP is a single ordered stream. If you multiplex multiple independent logical channels (as HTTP/2 does) over a single TCP connection, you pay a hidden tax: one lost packet blocks all channels. This is not a bug — it is an inherent property of TCP's ordering guarantee applied to multiplexed streams. QUIC exists precisely because this tax became unacceptable for HTTP/2.
UDP doesn't fail to provide reliability. It deliberately abstains from providing it, handing the responsibility to the application. When an application team builds a reliable UDP transport for their game server, they are not "reinventing TCP badly." They are implementing precisely the reliability semantics their use case requires — which may be "retransmit only critical game events, drop stale position updates." TCP cannot express that. UDP can.
QUIC provides everything TCP cared about — reliability, ordering, flow control, congestion control — but implements it in user space over UDP, adding encryption, eliminating head-of-line blocking, and enabling connection migration. If TCP is the old master craftsman who built the foundation of the internet, QUIC is the apprentice who kept the craftsmanship but updated the tools. The principles are continuous; the implementation is modern.
The internet runs because people in the 1970s decided that complexity belonged at the edge, not the middle. TCP is the software expression of that decision. Every protocol that came after it — UDP, SCTP, QUIC — is a different answer to the same question: given an unreliable network, how much reliability should the transport provide, to whom, and at what cost?
That question does not have one correct answer. TCP's answer — complete, ordered, reliable delivery for every byte — was the right answer for the web of 1993, the dominant answer for the web of 2024, and will be a major answer for the web of 2034. But it will not be the only answer, and understanding why is the beginning of understanding the whole field.