TCP vs UDP: What's the Difference and When to Use Each
TCP makes sure every byte arrives in order. UDP gets data there fast and lets the app decide what to do about loss. Here's how they differ and why real-time apps often use both.
TCP vs UDP at a glance
|
TCP |
UDP | |
|---|---|---|
|
Connection |
Connection-oriented; three-way handshake first |
Connectionless; send immediately |
|
Delivery |
Guaranteed; lost segments are resent |
Best effort; no resend |
|
Ordering |
Bytes arrive in the order sent |
Datagrams can arrive in any order |
|
Header size |
20 to 60 bytes |
8 bytes |
|
Speed |
Extra round trips; stalls while waiting on lost data |
Lower latency; no stall on loss |
|
Congestion control |
Built in |
Left to the application |
|
Data model |
Continuous byte stream |
Separate messages (datagrams) |
|
Broadcast and multicast |
No |
Yes |
|
Typical uses |
Web, APIs, email, file transfer, chat, WebSockets |
Voice and video calls, gaming, DNS, live streaming, QUIC |
What is TCP?
TCP (Transmission Control Protocol) gives two programs a reliable, ordered stream of bytes. Before any data moves, the client and server open a connection with a three-way handshake: SYN, SYN-ACK, ACK. After that, every chunk of data (a segment) carries a sequence number, and the receiver acknowledges what it got.
If an acknowledgement doesn't come back in time, the sender retransmits. The receiver puts segments back in order before handing them to the application, so your code never sees a gap or a jumbled byte. TCP also slows down when the receiver or the network is overloaded, through flow control and congestion control.
Most of the web runs on TCP: HTTP/1.1 and HTTP/2, TLS, email (SMTP, IMAP), SSH, database connections and WebSockets.
What is UDP?
UDP (User Datagram Protocol) does almost nothing beyond addressing. It wraps your data in an 8-byte header with source port, destination port, length and checksum, and sends it. There's no connection, no acknowledgement, no retransmission and no ordering. Each message is a self-contained datagram, delivered once, out of order, or not at all.
That sounds like a weakness, but it's the point. With no handshake and no waiting for acknowledgements, UDP adds almost no delay, and a lost packet doesn't hold up the ones behind it. Applications that care more about now than everything build exactly the reliability they need on top.
You'll find UDP under DNS lookups, voice and video calls, online games, live streaming and QUIC, the transport behind HTTP/3.
What TCP and UDP have in common
Both are transport-layer protocols in the internet protocol suite, and both sit on top of IP, which moves packets between machines but promises nothing about delivery. Both use port numbers so one machine can run many services at once: port 443 for HTTPS over TCP, port 53 for DNS over UDP, and so on. Both include a checksum, so a corrupted packet is detected and dropped rather than handed to your app.
The split is in what happens next. TCP adds connections, acknowledgements, retransmission, ordering and congestion control on top of IP. UDP adds almost nothing, which leaves those choices to the application. That's why the question is rarely "which is better" and more often "how much of TCP's work does this application actually need, and what does that work cost in delay?"
Key differences between TCP and UDP
Reliability
TCP guarantees delivery or tells you the connection failed. UDP makes no promise; the application has to notice and handle loss if it cares.
Ordering
TCP delivers bytes in the order they were sent. UDP datagrams can arrive in any order, and the receiver sorts them out (or doesn't).
Speed and latency
TCP needs a round trip to set up a connection, plus TLS on top for encryption. When a segment is lost, everything behind it waits for the resend. This is called head-of-line blocking. UDP starts sending immediately and never stalls on a missing packet.
Overhead
A TCP header is 20 to 60 bytes, and the protocol keeps state for every connection. A UDP header is 8 bytes and the protocol keeps no state, which makes it cheaper for servers handling huge numbers of short exchanges.
Network friendliness
TCP backs off when the network is congested. UDP doesn't, so UDP applications are expected to manage their own sending rate. Some corporate firewalls block UDP entirely, which is why real-time apps keep a TCP fallback.
TCP vs UDP in code
The difference shows up in Node.js. UDP sends a datagram with no connection; TCP connects first, then writes to a stream.
const dgram = require('dgram');
const net = require('net');
// UDP: no connection, no delivery guarantee
const udp = dgram.createSocket('udp4');
udp.send('ping', 41234, '127.0.0.1', (err) => {
if (err) console.error(err);
udp.close();
});
// TCP: handshake first, then a reliable, ordered stream
const tcp = net.connect(8080, '127.0.0.1', () => {
tcp.write('ping');
tcp.end();
});
tcp.on('error', console.error);
The UDP callback fires when the datagram leaves your machine, not when it arrives. TCP's connect callback only runs once the handshake succeeds.
When to use TCP or UDP
Use TCP when every byte matters:
Chat messages, API calls and file transfers
Payments, logins and anything transactional
Web pages, documents and database queries
Use UDP when late data is useless data:
Voice and video calls, where a 200 ms-old audio packet is worth less than no packet
Multiplayer games that send player positions many times a second
Live streaming with low-latency targets
DNS lookups and other single request-and-reply exchanges
Many real-time apps use both. A chat app can send text over a TCP-based WebSocket so no message is ever lost, while its voice and video calls run over WebRTC, which normally carries media over UDP and only falls back to TCP when the network blocks UDP.
Rule of thumb: if a late message is still useful, use TCP. If a late packet is worse than a missing one, use UDP.
Where QUIC and HTTP/3 fit
QUIC is a newer transport that runs on top of UDP and adds what TCP offers: reliable delivery, ordering and congestion control, with TLS 1.3 built in. Because it handles each stream separately, one lost packet only delays its own stream instead of the whole connection. It also sets up connections faster than TCP plus TLS. HTTP/3 runs on QUIC, so a lot of web traffic that used to be TCP now travels over UDP.
Limits and pitfalls
"UDP is faster" isn't always true. On a clean network TCP is fast too. UDP only wins when loss, latency or connection setup cost really hurts the experience.
Rebuilding TCP on UDP is hard. If you need every message delivered in order, use TCP (or QUIC) rather than writing your own retransmission logic.
UDP can be blocked. Hotel, school and corporate networks often block UDP. Plan a TCP or TLS fallback, such as a TURN relay for WebRTC.
Packet size limits apply. Keep UDP payloads around 1,200 bytes or less to avoid fragmentation on typical networks.
TCP connections cost memory. Each open connection holds state on the server. Holding millions of idle connections needs tuning, whichever protocol sits on top.
How this applies to in-app chat and calls
Chat and calling put different demands on the network, which is why most real-time platforms use more than one transport.
Reliable messages, real-time calls
The CometChat SDKs keep a WebSocket connection open to CometChat's servers while your app is in the foreground, so messages, typing indicators and receipts arrive over a TCP-based connection that doesn't drop data. The SDK reconnects automatically when the network comes back. Voice and video calls are a different job, and that's where WebRTC and UDP come in. See what WebSockets are and what WebRTC is for the detail.
Frequently asked questions
Is UDP faster than TCP?
UDP has less overhead and no handshake, so it starts sending sooner and never stalls waiting for a lost packet. On a good network the difference is small. It matters most for live audio, video and games.
Why does video calling use UDP?
In a live call, audio that arrives late is useless. UDP lets the app skip a lost packet and keep playing instead of freezing while TCP resends it. WebRTC normally sends media over UDP for this reason.
Do WebSockets use TCP or UDP?
TCP. A WebSocket starts as an HTTP request and upgrades to a long-lived TCP connection, which is why it suits chat messages that must all arrive in order.
Is HTTP TCP or UDP?
HTTP/1.1 and HTTP/2 run over TCP. HTTP/3 runs over QUIC, which is built on UDP.
Which is better for gaming, TCP or UDP?
Most fast-paced multiplayer games use UDP for real-time state like player positions and keep TCP (or a reliable channel on top of UDP) for things like chat, purchases and match results.