# TCP vs UDP

> What are the differences between TCP and UDP? When would you choose one over the other?

Canonical: https://www.internhack.in/learn/interview/computer-networks-interview/cn-tcp-vs-udp
Last updated: 2026-11-28

Difficulty: Beginner

## Question

What are the differences between TCP and UDP? When would you choose one over the other?

## Answer

This is probably the single most asked networking question in Indian campus placements, so let's answer it with substance rather than a memorised table. The way I like to explain it: TCP is a phone call, UDP is a postcard. On a phone call you dial, the other side picks up, you confirm you can hear each other, you talk in order, and if a word is lost you say sorry, can you repeat that. With a postcard you just write and drop it in the box. It may arrive, it may arrive late, it may arrive out of order relative to another postcard, and nobody tells you.

TCP, Transmission Control Protocol, is connection-oriented. It establishes a connection with a three-way handshake before sending data, provides reliable delivery through sequence numbers, acknowledgements and retransmission, guarantees in-order delivery, detects corruption with a checksum, and includes flow control and congestion control. It treats data as a byte stream with no message boundaries, so two writes of 100 bytes may be read by the receiver as one read of 200 bytes. The header is at least 20 bytes.

UDP, User Datagram Protocol, is connectionless. There is no handshake, no acknowledgement, no retransmission, no ordering, no flow control and no congestion control. The header is just 8 bytes: source port, destination port, length and checksum. It preserves message boundaries: each send produces one datagram and one receive gets exactly that datagram. UDP is fast and light exactly because it does so little, and it supports broadcast and multicast, which TCP cannot.

Choosing between them comes down to what a late or lost packet means for your application. Use TCP when correctness matters more than latency: web browsing (HTTP/1.1 and HTTP/2), email (SMTP), file transfer (FTP, SFTP), SSH, database connections. Use UDP when fresh data matters more than complete data or when you need to control behaviour yourself: DNS queries (small request and response, retry at application level), live video and voice calls, online gaming, DHCP, NTP, SNMP, and streaming telemetry. In a video call, retransmitting a lost frame from 300 ms ago is pointless because the moment has passed, so it is better to skip it.

A common trap: people say UDP is unreliable so it is bad. Not true, it is simply minimal, and many modern protocols build exactly the reliability they need on top of it. QUIC, the transport for HTTP/3, runs over UDP and implements its own reliability, congestion control and encryption in user space, avoiding problems with TCP's head-of-line blocking and ossified middleboxes. Also TCP is not always slower: for bulk transfers it can saturate a link efficiently.

Interviewers may ask about the header differences and the port fields. Both use 16-bit ports and both use a checksum, though the UDP checksum is optional in IPv4. TCP adds sequence number, acknowledgement number, flags (SYN, ACK, FIN, RST, PSH, URG), window size and options. A last edge case: DNS uses UDP by default but switches to TCP when the response exceeds the size limit (traditionally 512 bytes, larger with EDNS0) and for zone transfers, showing that a single protocol can use both.

## Code examples

### UDP sender and receiver in Python

```
# receiver.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("0.0.0.0", 9999))
data, addr = s.recvfrom(1024)
print("got", data, "from", addr)

# sender.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(b"hello", ("127.0.0.1", 9999))
```

No listen, accept or connect. One sendto is one datagram.

## Key points

- TCP header is 20 bytes minimum; UDP header is 8 bytes.
- TCP is a byte stream; UDP preserves message boundaries.
- QUIC builds reliability on top of UDP.
- DNS uses UDP normally and TCP for large responses and zone transfers.

## Common follow-up questions

- Why does DNS use UDP?
- Can UDP ever be reliable?
- What is head-of-line blocking?
- Why does a video call prefer UDP?

## Interview tips

- Lead with the phone call versus postcard analogy, then give the table-style differences.
- Always give use cases with a reason, not just a list.

## Concepts covered

TCP, UDP, Transport Layer, Reliability, Use Cases

Previous: [ICMP, Ping and Traceroute](https://www.internhack.in/learn/interview/computer-networks-interview/cn-icmp-ping-traceroute) · Next: [TCP Three-Way Handshake and Four-Way Connection Teardown](https://www.internhack.in/learn/interview/computer-networks-interview/cn-tcp-handshake-teardown) · [All of Computer Networks](https://www.internhack.in/learn/interview/computer-networks-interview)

## More Computer Networks questions

- [TCP TIME_WAIT State: Why It Exists and How to Handle It](https://www.internhack.in/learn/interview/computer-networks-interview/cn-time-wait)
- [How TCP Achieves Reliable Delivery](https://www.internhack.in/learn/interview/computer-networks-interview/cn-tcp-reliability)
- [Flow Control vs Congestion Control in TCP](https://www.internhack.in/learn/interview/computer-networks-interview/cn-flow-vs-congestion)
- [DNS Resolution: Recursive vs Iterative Queries](https://www.internhack.in/learn/interview/computer-networks-interview/cn-dns-resolution)
- [DNS Record Types and TTL Caching](https://www.internhack.in/learn/interview/computer-networks-interview/cn-dns-records-ttl)
- [HTTP Methods and Status Codes](https://www.internhack.in/learn/interview/computer-networks-interview/cn-http-methods-status)

## Related

- [Learn](https://www.internhack.in/learn)
- [Interview Prep](https://www.internhack.in/learn/interview)
- [Computer Networks](https://www.internhack.in/learn/interview/computer-networks-interview)
