1. Library
  2. Computer Networks
  3. Tcp and Udp
  4. Udp

Updated 9 hours ago

Here's a counterintuitive truth about networks: TCP's reliability can make things worse.

When you're in a video call and a packet containing 20 milliseconds of audio gets lost, TCP will retransmit it. That packet might arrive 200 milliseconds later. By then, you've already said three more words. Playing that old audio now would be jarring, confusing, nonsensical. The "reliable" delivery made the conversation worse than if the packet had simply vanished.

In real-time, old data isn't late data—it's wrong data. Your position 200 milliseconds ago is a lie about where you are now.

This is why gaming, VoIP, and video streaming all reject TCP's guarantees. They choose UDP—a protocol that promises almost nothing—because they need the freedom to be smart about what to lose.

TCP's Three Sins Against Real-time

TCP commits three offenses that break real-time experiences:

Retransmission delay. When a packet is lost, TCP resends it. The retransmitted data arrives too late to matter, but TCP sent it anyway, wasting time and bandwidth on information that's already obsolete.

Head-of-line blocking. TCP delivers data in order. If packet 47 is lost, TCP holds packets 48, 49, and 50 hostage until 47 can be retransmitted—even when the application desperately needs that newer data right now. The lost packet blocks the line.

Aggressive congestion control. When TCP detects packet loss, it assumes congestion and slashes its sending rate. For real-time applications, this causes quality to collapse exactly when the network is stressed. The application can't maintain smooth degradation; TCP forces it off a cliff.

UDP commits none of these sins. It provides port numbers for multiplexing, a checksum for error detection, and nothing else. Packets arrive in whatever order the network delivers them. Lost packets stay lost. This apparent emptiness is UDP's gift: complete control returned to the application.

Gaming: Prediction Over Perfection

In a competitive first-person shooter, 50 milliseconds can determine whether your shot registers before or after your opponent's. Games send position updates 20, 30, even 60 times per second. If one update is lost, the next one is already on its way. Retransmitting position update 47 is pointless when updates 48, 49, and 50 have already arrived with newer information.

Game engines solve packet loss through prediction rather than retransmission.

Client-side prediction lets you see immediate responses to your inputs. When you press forward, your character moves instantly on your screen—the client predicts what the server will confirm. When the server's authoritative update arrives, the game reconciles any differences, snapping your character to the true position or smoothly correcting the trajectory.

Interpolation hides missing data. When a position update is lost, the game doesn't freeze or stutter. It smoothly animates the character between known positions, gliding from where they were 100 milliseconds ago to where they are now. The missing packet becomes invisible.

Lag compensation makes hit detection feel fair. When you shoot at a moving target, the server doesn't check where they are now—it rewinds to where they were from your perspective, accounting for your network latency. What you saw and shot at is what the server validates.

Games also prioritize ruthlessly. Critical events—shots fired, player deaths, objective captures—get sent multiple times or use application-layer acknowledgments. Cosmetic updates like particle effects are sent once and forgotten. If they arrive, great. If not, the game continues. Not all data deserves equal treatment.

VoIP: The Jitter Buffer Paradox

Voice calls face a different challenge. Brief gaps in audio are acceptable—human brains fill in missing syllables remarkably well. But delay destroys conversation. When round-trip latency exceeds 150 milliseconds, conversations become exhausting. You start talking over each other, pausing awkwardly, losing the natural rhythm of speech.

VoIP systems encode audio into packets representing 10-40 milliseconds of speech. Each packet is independent. If packet 47 is lost, packet 48 still contains perfectly good audio for the next time slice. TCP would hold packet 48 until 47 was retransmitted. UDP delivers 48 immediately.

But there's another problem: jitter. Packets sent 20 milliseconds apart might arrive 5 milliseconds apart, then 50 milliseconds apart, then 15. This variation would cause constant stuttering—audio speeding up and slowing down randomly.

The solution is beautifully paradoxical: the jitter buffer deliberately adds delay to remove delay.

A jitter buffer holds incoming packets for 20-80 milliseconds before playing them. Packets that arrive early wait. Packets that arrive late (but within the buffer window) still make it. The buffer releases audio at a perfectly steady rate, smoothing the wrinkles in network time itself. You trade a small constant delay for the elimination of stuttering.

When packets are truly lost—arriving after even the jitter buffer's patience expires—VoIP uses packet loss concealment. Simple approaches repeat the last good packet or play silence. Sophisticated algorithms analyze preceding audio to synthesize the missing content, extrapolating pitch and tone to generate plausible sounds. Modern codecs like Opus do this so well that losses under 5% are often imperceptible.

VoIP systems also adapt continuously. They monitor conditions and adjust in real-time—shrinking the jitter buffer when the network is stable, expanding it when jitter increases, reducing bitrate from 64kbps to 32kbps if loss spikes. This constant adjustment is only possible because UDP lets the application control its own destiny.

Video Streaming: Selective Sacrifice

Live video streaming combines VoIP's latency sensitivity with massive bandwidth demands. Voice needs 20-100 kilobits per second. Video needs 500 kilobits to 10 megabits or more. And video has complex internal dependencies that voice lacks.

Video codecs compress by encoding differences between frames. A keyframe (I-frame) contains a complete image—expensive but self-sufficient. Predicted frames (P-frames and B-frames) encode only what changed since the previous frame—cheap but dependent. Lose a keyframe and every frame that references it becomes corrupt. Lose a predicted frame and the damage is contained.

This creates a hierarchy of importance. Streaming systems practice unequal error protection: keyframes get extra redundancy, more frequent transmission, sometimes even selective retransmission. Predicted frames are more expendable. When packets must be lost, the system chooses which losses hurt least.

Adaptive bitrate streaming extends this philosophy to quality levels. Applications monitor packet loss and available bandwidth, switching between encodings on the fly. If conditions degrade, the stream drops from 1080p at 5 megabits to 720p at 2 megabits, maybe down to 480p at 1 megabit. Users prefer reduced resolution over constant stalling. The application deliberately sacrifices quality to preserve continuity.

RTP (Real-time Transport Protocol) provides the scaffolding for all this. Sitting atop UDP, RTP adds sequence numbers and timestamps without imposing reliability. Its companion, RTCP, carries feedback about quality and synchronization. Together they give streaming applications the information they need to adapt intelligently.

Some streaming systems even implement selective retransmission—but smarter than TCP. If a keyframe packet goes missing and 50 milliseconds of latency budget remains, the application requests it specifically. For less critical packets, or when time is short, it moves on. The application decides what's worth waiting for.

The Network's Role

Applications handle loss and variation brilliantly, but reducing those problems at the network level still helps. Quality of Service (QoS) mechanisms can prioritize real-time traffic over bulk transfers.

DiffServ markings tell routers which packets matter most. VoIP packets marked with EF (Expedited Forwarding) get priority queuing, reducing the time they spend waiting behind large file transfers. When configured end-to-end, this dramatically improves real-time quality.

But here's the reality: end-to-end QoS requires cooperation from every network on the path. A perfectly configured home router doesn't help if the ISP treats all traffic equally. This is why applications must handle imperfect delivery—they cannot assume the network will be kind.

The Deeper Pattern

UDP gives applications a radical gift: the freedom to define what reliability means for their specific needs.

For gaming, reliability means the most recent state, not every state. For VoIP, reliability means continuous flow, not complete data. For streaming, reliability means adaptive quality, not fixed quality.

TCP imposes a single definition of reliability—every byte, in order, guaranteed. That definition serves file transfers and web pages perfectly. But when human perception drives requirements, when 50 milliseconds determines whether an experience feels good or broken, TCP's guarantees become constraints.

The applications that matter most to how we experience the Internet—the games we play, the calls we make, the streams we watch—all run on UDP. They accept that some data will be lost and choose to be intelligent about it. That choice, and the sophisticated systems built around it, is why real-time works at all.

Frequently Asked Questions About UDP in Real-time Applications

Was this page helpful?

😔
🤨
😃