TCP/IP Stack
The set of communications protocols used for the internet and similar computer networks.
Detailed Explanation
TCP (Transmission Control Protocol) handles the reliable delivery of data (checking for errors and reordering packets). IP (Internet Protocol) handles the addressing and routing of packets across the network. Together, they form the foundation of almost all internet traffic. IP tells the data where to go; TCP makes sure it gets there in one piece.
Quick Summary
TCP/IP is the layered protocol stack the internet runs on. IP routes packets between addresses; TCP guarantees ordered, reliable delivery on top. Almost every backend bug at the network layer comes back to these two.
Key Takeaways
- Four practical layers: Link (Ethernet/Wi-Fi), Internet (IP), Transport (TCP/UDP), Application (HTTP/DNS/SSH).
- TCP guarantees ordered, reliable byte stream. UDP doesn't, it's faster but "send and pray."
- TCP handshake: SYN → SYN-ACK → ACK. Adds at least one round trip before any data flows.
- Congestion control (BBR, CUBIC) decides how aggressively TCP sends data when the network is loaded.
- TIME_WAIT, MSS, MTU, Nagle's algorithm, corners of TCP that mostly don't matter until they suddenly do.
When to use it
- Diagnosing latency with `traceroute`, `mtr`, and `tcpdump` to find which hop is slow.
- Tuning kernel TCP parameters (buffer sizes, congestion control) for high-throughput servers.
- Choosing TCP vs. UDP for a new protocol, reliability vs. latency.
- Understanding why HTTP/3 over QUIC over UDP exists: TCP's head-of-line blocking became a bottleneck.
Common Mistakes
- Assuming "the network is reliable", fallacy #1 of distributed computing. Set timeouts.
- Connecting once and reusing forever without keep-alives; idle connections silently die at NATs and load balancers.
- Ignoring packet loss in error messages, "connection reset" often means something else dropped you, not your code.
- Not testing on a real lossy network; localhost has zero packet loss and 0ms latency, which lies.
TCP/IP Stack, Frequently Asked
TCP or UDP?
TCP for anything where lost data breaks the protocol (HTTP, SSH, DB connections). UDP for real-time data where latency matters more than reliability (gaming, voice, video, DNS lookups), or when you're building reliability on top yourself (QUIC).
Why does my connection time out before my application timeout?
Almost always a NAT or load balancer dropping idle TCP connections after 60-300s. Enable TCP keep-alives (or application-level pings) on long-lived connections.