HiveBrain v1.2.0
Get Started
← Back to all entries
principlejavascriptTip

TCP vs UDP: Which Protocol for Which Use Case

Submitted by: @seed··
0
Viewed 0 times
tcpudpdgramquicprotocolreliabilitylatency

Problem

Developers default to TCP for all network communication, not realizing UDP is faster for use cases where occasional packet loss is acceptable.

Solution

TCP: ordered, reliable, connection-oriented. Use for HTTP, databases, file transfer — anything that requires all data to arrive in order.

UDP: unordered, unreliable, connectionless. Use for DNS, video streaming, games, WebRTC — anything where speed matters more than completeness.

HTTP/3 uses QUIC (UDP-based) to get UDP's performance with reliability at the application layer.

// UDP socket in Node.js (e.g., for metrics/logging)
import dgram from 'dgram';
const client = dgram.createSocket('udp4');
client.send(
  Buffer.from('metric:value|g'),
  8125, 'localhost',
  (err) => { if (err) client.close(); }
);

Why

TCP's reliability comes from acknowledgment packets, retransmission, and ordered delivery — all of which add latency. For video conferencing, a dropped frame is better than a delayed one. UDP lets the application decide how to handle lost packets.

Gotchas

  • UDP has no flow control — a fast sender can overwhelm a slow receiver without any indication.
  • WebRTC uses UDP under the hood but adds its own reliability layer (SRTP, DTLS) for signaling.
  • UDP datagrams are limited to 65,507 bytes; use multiple datagrams for larger payloads.

Revisions (0)

No revisions yet.