User Datagram Protocol#
UDP is quite different from TCP. The end goal is the same: get data from the client to the server and back again (if desired) but the way in which UDP operates is simple compared to TCP. That might seem like an advantage but as we'll see shortly UDP isn't ideal for all types of connections and workloads.
When we looked at TCP we learned that there is a three-way handshake, a connection is established, the connection has a state, data is sent and the receiving party confirms it got the data, and so on. We compared this process with a postal system analogy with the sending of a letter. The application writes the letter (packet) and the postal system (TCP) sends it and verifies it got delivered. Let's recycle that analogy.
With UDP the delivery of the letter looks like this:
- Write the letter (data)
- Give it to the post office (UDP)
- They put in an envelope (datagram)
- They sign it with the address (IP) of the recipient
- They also include the port number in the address
- ... and then they fire it out of a cannon and forget about it
No really: UDP is basically a "fire and forget" protocol. It does nothing to confirm the datagram (not packet - that's TCP) was received by the remote endpoint. With UDP you send the datagram and you forget about it. It's all about the application sending and receiving the UDP datagrams doing their own work to check everything was received or not.
This might seem a bit useless but it is actually very useful.
UDP a very fast protocol. Sending datagrams is a simple process because no connection needs to be established ahead of time. Instead you only need the IP address and port of the UDP endpoint and then you start sending datagrams to it. The remote endpoint that's listening for connections on the UDP port then receives the datagrams and decides what to do - reply, not reply, etc.
TCP differs in that you have to setup the connection, via the handshake, before sending data. UDP doesn't require this setup and as such it can be seen as far more efficient and a lot faster when you're dealing with a lot of clients.