← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

TCP vs UDP

networking Intermediate

Also Known As

TCP UDP connectionless reliable delivery

TL;DR

TCP provides reliable ordered delivery with connection setup overhead. UDP is connectionless and faster but unreliable — choose based on whether loss or latency is more acceptable.

Explanation

TCP (Transmission Control Protocol): connection-oriented (three-way handshake), guaranteed delivery (retransmission on loss), ordered packets, congestion control. Use for: HTTP/HTTPS, database connections, email, file transfers. UDP (User Datagram Protocol): connectionless, fire-and-forget, no guaranteed delivery, no ordering. Use for: DNS, video streaming, online games, VoIP, QUIC/HTTP3. The trade-off: TCP's reliability costs latency; UDP's speed costs reliability. Applications that can handle loss (real-time audio) prefer UDP; applications that cannot (banking) require TCP.

Common Misconception

UDP is always faster than TCP — UDP has less overhead per packet, but TCP's congestion control and flow control often make it more efficient on real networks; raw speed depends on the network conditions.

Why It Matters

Choosing TCP vs UDP matters for PHP socket programming, DNS resolvers, real-time features (WebRTC), and understanding why QUIC (HTTP/3) uses UDP but reimplements reliability at the application layer.

Common Mistakes

  • Using TCP for DNS queries — UDP is standard (less overhead for small queries).
  • Using UDP for reliable data transfer without implementing your own ACK/retry — data loss is silent.
  • Not understanding that QUIC is UDP with reliability — HTTP/3's UDP doesn't mean unreliable.
  • PHP stream sockets default to TCP — explicitly specify udp:// for UDP socket programming.

Code Examples

✗ Vulnerable
// UDP for order confirmation — data can be silently lost:
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_sendto($socket, $orderConfirmation, strlen($orderConfirmation), 0,
    '10.0.0.1', 9000);
// Fire and forget — confirmation may never arrive
// Customer never gets their order update
✓ Fixed
// TCP for reliable delivery of business data:
$socket = stream_socket_client('tcp://10.0.0.1:9000');
fwrite($socket, $orderConfirmation);
// TCP guarantees delivery — retransmits on loss

// UDP appropriate for real-time metrics:
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
socket_sendto($socket, $metric, strlen($metric), 0, $statsd_host, 8125);
// StatsD: dropped metrics are fine — stream of data not individual records

Added 16 Mar 2026
Edited 22 Mar 2026
Views 19
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 1 ping M 0 pings T 0 pings W 0 pings T 0 pings F 1 ping S 1 ping S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 2 pings S 0 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 0 pings T
No pings yet today
No pings yesterday
Amazonbot 7 Perplexity 4 Ahrefs 2 Google 2
crawler 14 crawler_json 1
DEV INTEL Tools & Severity
🔵 Info ⚙ Fix effort: Low
⚡ Quick Fix
PHP web apps use TCP (HTTP) — understanding TCP vs UDP explains why HTTP/3 uses UDP (QUIC) for performance and why WebSockets are TCP while some real-time games use UDP
📦 Applies To
any web cli
🔗 Prerequisites
🔍 Detection Hints
Confusion about why HTTP/3 uses UDP; misunderstanding of connection state in PHP-FPM
Auto-detectable: ✗ No
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: High ✗ Manual fix Fix: Low Context: File

✓ schema.org compliant