TCP
A reliable transport protocol that guarantees data arrives in order and without errors. It uses a three-way handshake to establish connections.
What is TCP?
In short
TCP (Transmission Control Protocol) is a transport-layer protocol that gives applications a reliable, ordered, error-checked stream of bytes over an unreliable network. It opens a connection with a three-way handshake, numbers every byte so lost or out-of-order packets can be retransmitted and reassembled, and slows itself down when the network is congested. Most internet traffic, including web pages, email, and database queries, rides on TCP.
What TCP actually is
TCP sits between your application and the IP layer. IP alone just throws packets at an address and hopes they arrive. Packets can be dropped, duplicated, delayed, or show up in the wrong order. TCP wraps IP and turns that mess into something an application can trust: a connection where the bytes you send come out the other end in the same order, exactly once, with nothing missing.
It is a byte-stream protocol, not a message protocol. When you write 1000 bytes then 500 bytes, the receiver might read it back as one chunk of 1500, or as 200 then 1300. TCP does not preserve your message boundaries; that is your job, which is why protocols built on top of it (HTTP, for example) add their own framing like Content-Length headers.
TCP is also connection-oriented. Before any data flows, both sides agree to talk and set up shared state: starting sequence numbers, window sizes, and options. That setup is the famous three-way handshake.
How the connection works
The handshake is three packets. The client sends a SYN with a random initial sequence number. The server replies SYN-ACK, acknowledging the client and sending its own sequence number. The client sends a final ACK. Now both sides know the other is reachable and agree on where the byte counting starts. This costs one full round trip before any real data moves, which is why latency matters: a client 200ms away pays 200ms just to open the connection.
Reliability comes from sequence numbers and acknowledgements. Every byte has a number. The receiver acknowledges the highest contiguous byte it has received. If the sender does not get an ACK before a timer expires, or it sees three duplicate ACKs, it retransmits. Out-of-order packets are buffered and reordered before being handed to the application.
Flow control stops a fast sender from drowning a slow receiver. The receiver advertises a window: how many more bytes it can accept right now. The sender never sends more than that window allows. Congestion control is separate and protects the network itself. Algorithms like CUBIC (Linux default) and BBR (built by Google) start slow, ramp up, and back off when they detect loss or rising delay, so thousands of connections can share a link without collapsing it.
Closing is a four-way exchange of FIN and ACK packets, and the side that closes first sits in a TIME_WAIT state for a couple of minutes to make sure stray packets are gone before the port is reused.
When to use it and the trade-offs
Use TCP when correctness matters more than the last few milliseconds: web pages, APIs, file transfers, database connections, email. If a missing byte would corrupt the result, TCP is the default and correct choice.
The cost is latency and head-of-line blocking. The handshake adds a round trip, and because TCP delivers bytes strictly in order, one lost packet stalls everything behind it even if those later packets already arrived. On a lossy mobile link this hurts. It is also heavier than the alternative: per-connection state, buffers, and timers on both ends.
UDP is the alternative when you would rather drop data than wait for it: live video, voice calls, gaming, and DNS lookups. QUIC, which now carries HTTP/3, was built specifically to fix TCP's weaknesses. It runs reliability and ordering over UDP, folds the TLS handshake into the connection setup so a new connection costs zero or one round trip, and avoids head-of-line blocking across independent streams.
A practical gotcha: opening a fresh TCP connection per request is expensive. That is why HTTP keep-alive, connection pooling in database drivers, and reverse proxies all reuse connections instead of paying the handshake cost repeatedly.
A concrete example
Load a page on amazon.com and TCP is doing the heavy lifting underneath. Your browser resolves the IP, opens a TCP connection to port 443, and the three-way handshake completes in one round trip. The TLS handshake then runs on top of that reliable channel, and finally the HTTP request goes out.
Amazon, like most large sites, keeps these connections alive and reuses them for many requests so it does not pay the handshake tax on every image and script. Their load balancers terminate millions of concurrent TCP connections, and the TIME_WAIT state at that scale is a real tuning concern, which is why operators adjust kernel parameters like net.ipv4.tcp_tw_reuse and ephemeral port ranges.
On the data side, congestion control decides how fast bytes flow. Netflix serves video over TCP from its Open Connect appliances and tunes the congestion algorithm so a single stream can fill a fat pipe quickly without starving other users sharing the same link.
Where it is used in production
Linux kernel
Implements the most widely deployed TCP stack on Earth; its default congestion control is CUBIC and it ships BBR as an option.
Google / QUIC
Built BBR congestion control and then designed QUIC over UDP to replace TCP for HTTP/3, fixing handshake cost and head-of-line blocking.
Nginx
Reverse proxy and web server that terminates and pools huge numbers of TCP connections, reusing them via keep-alive to avoid repeated handshakes.
Netflix Open Connect
Serves video over TCP from edge appliances and tunes congestion control to saturate links fast without starving other traffic.
Frequently asked questions
- What is the difference between TCP and UDP?
- TCP is reliable, ordered, and connection-oriented: it guarantees every byte arrives in order, retransmitting losses, at the cost of a handshake and possible delay. UDP is fire-and-forget with no connection, no ordering, and no retransmission, which makes it faster and lower latency but lossy. Use TCP for web and files, UDP for live video, voice, gaming, and DNS.
- Why does TCP need a three-way handshake?
- The three packets let both sides confirm they can send and receive, and agree on the starting sequence numbers used to track every byte. Two packets would not prove the client can receive the server's data, and skipping it would let stale or spoofed packets open bogus connections. The cost is one round trip before any real data moves.
- What is head-of-line blocking in TCP?
- Because TCP delivers bytes strictly in order, a single lost packet forces the receiver to hold every packet that arrived after it until the missing one is retransmitted. The whole stream stalls behind one gap. QUIC and HTTP/3 fix this by keeping independent streams separate so a loss in one does not block the others.
- Is HTTP built on top of TCP?
- HTTP/1.1 and HTTP/2 run over TCP, usually with TLS on top for HTTPS. HTTP/3 is different: it runs over QUIC, which uses UDP instead of TCP to cut handshake latency and avoid head-of-line blocking, while still providing reliability and ordering itself.
- What is the TIME_WAIT state and why does it matter?
- After a connection closes, the side that closed first holds the socket in TIME_WAIT for roughly two minutes so any stray delayed packets cannot be misread as part of a new connection on the same port. On servers handling millions of short connections, accumulated TIME_WAIT sockets can exhaust ports, which is why operators tune kernel settings like tcp_tw_reuse and the ephemeral port range.
Learn TCP hands-on
This page explains the idea. The full lesson lets you step through the ring as servers join and leave, read the implementation, and check yourself with a quiz. It is one of 760+ lessons in the System Design Masterclass, from your first API call to distributed consensus. Eleven Foundation lessons are free, no signup. Lifetime access is ₹499 in India or $7.99 worldwide, one payment, no subscription.
Related lessons
Lessons that touch on TCP as part of a larger topic.
TCP Optimization
Tuning TCP parameters and techniques to maximize throughput and minimize latency for cloud workloads
intermediate · cloud infrastructure
Keep-Alive Connections
Reuse TCP connections across multiple requests to eliminate handshake overhead
intermediate · api design protocols
Connection Reuse
Pool and reuse connections across your application to maximize throughput and minimize overhead
intermediate · api design protocols
TCP/IP Stack
The four-layer model that powers every internet connection, from electrical signals to web pages
intermediate · cloud infrastructure
QUIC
The protocol replacing TCP for the modern web, built on UDP with encryption, multiplexing, and zero-RTT built in
intermediate · cloud infrastructure
See also
Related glossary terms you might want to look up next.
HTTP
The protocol powering the web. A request-response model where clients ask for resources and servers respond. Stateless by design.
WebSocket
A protocol for full-duplex communication over a single TCP connection. Unlike HTTP, the server can push data to the client without being asked.
SSL/TLS
Cryptographic protocols that encrypt data in transit between client and server. TLS is the modern successor to SSL. The 'S' in HTTPS.
Latency
The time delay between sending a request and getting a response. Amazon found every 100ms of extra latency costs 1% in sales.
Throughput
The number of operations a system can handle per unit of time. Think of it as how many cars a highway can move per hour.
Bandwidth
The maximum amount of data that can be transferred over a network in a given time. It's the width of the pipe, not how fast the water flows.