Lesson 8 of 18
3. Client and Server

3.7 What's the difference between TCP and UDP?

3 min read+10 XP

Under HTTP

In the restaurant: One waiter writes your order down, reads it back, and confirms; if a slip gets lost, they come back and ask again. Another just shouts orders across a noisy room: fast, but some orders are never heard.

In the system: HTTP rides on top of a transport protocol that actually moves the bytes.

TCP is the careful waiter; UDP is shouting across the room.

Figure: TCP trades speed for guarantees. UDP trades guarantees for speed, and leaves the app to cope.

  • TCP first sets up a connection with a three-way handshake (SYN, SYN-ACK, ACK). After that it guarantees every byte arrives, in order, resending anything lost. The cost: the handshake takes a round trip, and if one piece is lost, everything behind it waits until it's resent (head-of-line blocking).
  • UDP just sends packets (datagrams). No handshake, no guarantee: packets can be lost, duplicated or arrive out of order. The app deals with it.

When each fits:

  • TCP: anything that must be complete and exact. Web pages, APIs, file downloads, database connections.
  • UDP: anything where late is as bad as lost. In a video call, a frame that arrives 2 seconds late is useless; better to skip it. Most DNS lookups use UDP because one tiny question and answer don't need a connection.

Memory code: "TCP confirms, UDP fires and forgets."