What does a UDP header contain, and how big is it?
A UDP header is 8 bytes with four 16-bit fields: source port, destination port, length, and checksum. A TCP header is at least 20 bytes. That small size is part of why UDP is light.
Explain it simply
A UDP datagram is a postcard: sender's room, receiver's room, how long the message is, and a check number to detect damage. TCP is a registered courier envelope with tracking and acknowledgement fields on top.
Example
0 16 32 bits
+---------+---------+
| src port| dst port|
+---------+---------+
| length | checksum|
+---------+---------+
| data ...- Source port: optional in UDP; can be 0 if no reply is expected.
- Length: header plus data in bytes, so the minimum is 8.
- Checksum: detects corruption; optional in IPv4, required in IPv6.
If the checksum fails, the receiver drops the datagram silently. There is no retransmission, no sequence number and no connection state. A datagram with 100 bytes of data has length 108.
What to say in the interview
"UDP has just four fields in 8 bytes: source port, destination port, length and checksum. There are no sequence numbers, acknowledgements or flow control, so reliability, if any, is the application's job."
Common mistakes
- Saying UDP has no checksum. It has one; it just does not retransmit on failure.