notesonly.in

One notebook for every subject — open it anywhere.

Log in

TCP and UDP: Reliability and Congestion Control Guide.

TCP and UDP are the two main transport protocols that carry application data across networks. TCP (Transmission Control Protocol) is reliable and connection-oriented: it sets up a connection, numbers every byte, retransmits lost data, and delivers everything in order. UDP (User Datagram Protocol) is unreliable and connectionless: it fires off independent datagrams with no setup, no acknowledgments, and no retransmission.

This matters because every app chooses one. Web browsing, email, and file transfers need TCP's guarantee that nothing is lost or reordered. Live video calls, online games, and DNS lookups prefer UDP's speed — a late packet in a game is worse than a lost one. Picking the wrong one means either broken data or needless lag.

Key Concepts

  • Port: a 16-bit number identifying the application on a host (HTTP = 80, HTTPS = 443); IP address + port = socket.
  • Three-way handshake: SYN → SYN-ACK → ACK, which opens a TCP connection and synchronizes sequence numbers.
  • Sequence/acknowledgment numbers: every TCP byte is numbered; ACKs confirm the next expected byte.
  • Sliding window: flow control letting the sender transmit multiple segments before waiting for an ACK.
  • Congestion control: slow start (window grows exponentially), then congestion avoidance (grows linearly, halves on loss) — TCP probes for available bandwidth without overwhelming the network.
  • Checksum: error detection on headers and data in both protocols.
  • Datagram: UDP's independent, self-contained packet — no connection, no ordering, no guarantees.

How It Works

  1. Opening a TCP connection takes one round trip: the client sends SYN with its initial sequence number, the server replies SYN-ACK with its own, and the client ACKs.
  2. Data flows in numbered segments; the receiver's ACKs slide the sender's window forward, and any gap triggers retransmission after a timeout or three duplicate ACKs (fast retransmit).
  3. Congestion control starts the window small, doubles it each round trip (slow start), and on detecting loss halves it and grows additively — the AIMD (additive increase, multiplicative decrease) pattern that fairly shares bandwidth.
  4. Closing uses FIN segments in each direction. UDP skips all of this: the application builds a datagram, hands it to IP, and hopes — delivery, order, and duplicates are the application's problem, which is fine for DNS queries and game position updates where speed beats certainty.

Worked Example

A client sends a 3000-byte message over TCP with MSS (maximum segment size) 1000; the connection's initial sequence number is 1000.

  1. The data splits into 3 segments: bytes 1000–1999 (SEQ = 1000), 2000–2999 (SEQ = 2000), 3000–3999 (SEQ = 3000).
  2. Segments 1 and 2 arrive; the receiver sends ACK = 3000, meaning "I have everything below 3000 — send from 3000 next."
  3. Segment 3 is lost. After the timeout, the sender retransmits SEQ = 3000.
  4. It arrives; the receiver sends ACK = 4000, confirming all 3000 bytes.

Total bytes on the wire: 4000 (one retransmission) for 3000 bytes of data — the overhead that buys TCP's reliability. Over UDP, the same 3 datagrams would simply be sent once, and the lost one would never be noticed by the protocol.

Common Misconceptions

  • "TCP is always better than UDP." For real-time media, TCP's retransmission delays cause stutter; UDP with app-level handling wins.
  • "UDP has no error checking." UDP includes a checksum (optional in IPv4, mandatory in IPv6) — it detects corruption, it just doesn't fix it.
  • "The handshake makes TCP slow to start." It costs one round trip, after which the sliding window keeps the pipe full; bulk throughput is unaffected.

Real-World Applications

  • TCP — web (HTTP/HTTPS), email (SMTP/IMAP), file transfer (FTP/SFTP), database connections.
  • UDP — DNS lookups, VoIP and video calls, online gaming, live streaming, DHCP.

TCP numbers every byte, acknowledges receipt, and retransmits losses — reliable but with overhead, as the 3000-byte example shows. UDP sends datagrams and moves on — fast but bare. Match the protocol to the need: correctness for files, speed for live media.