UDP 并非不可靠,它是可转换的。
UDP isn't unreliable, it's a convertible (2024)

原始链接: https://www.proxylity.com/articles/udp-is-a-convertible.html

对UDP的常见描述为“不可靠”具有误导性。UDP并非*无法*送达,而是它不*保证*送达、顺序或错误检查——与TCP不同。可以将UDP想象成一辆敞篷车:快速且自由,但暴露在各种环境中。TCP是同一辆车,但车顶始终关闭,以牺牲速度和灵活性为代价提供可靠性。 这种“不可靠”源于UDP缺乏内置功能,例如确认和重传,而TCP则提供这些功能。然而,这并非一种限制,而是一种设计选择。开发者可以轻松地在UDP*之上*添加自定义的可靠性层,仅构建特定应用程序所需的功能。 这种灵活性使UDP成为游戏、流媒体和物联网等应用的首选,在这些应用中,速度和控制优先于绝对保证的送达。最终,UDP并非不可靠;它是可适应的,提供构建针对手头任务定制的可靠性的自由。

相关文章

原文

We've all heard the line: "UDP is unreliable."

It's so common it's almost an instinct. But that description doesn't quite hold up once you look closer. UDP isn't unreliable in the sense that it fails to do its job -- it's unreliable in the same way a convertible is "un-dry".

By default, it'll let you feel the open air, the sunshine -- and yes, maybe a little rain. But when you need cover, you can always put the top up.

Reliability in networking works in a similar way. UDP gives you the open-air version: minimal framing, no promises, pure freedom. TCP, on the other hand, is the same car with the "reliability top" welded shut.

Where "Unreliable" Really Comes From

The "UDP is unreliable" phrase emerges from protocol guarantees, not from the physical packets themselves. At the network level, every layer can fail occasionally -- even the electrical signals across an Ethernet cable aren't immune to noise or interference. TCP packets can be lost in transit the same as UDP, ICMP or any other.

What makes UDP different from TCP is that it doesn't hide these facts. It doesn't promise ordered delivery, retransmission, or flow control. It sends your data as-is and trusts you to handle the consequences.

So when people call UDP "unreliable," what they really mean is "UDP doesn't include the features that would make reliability automatic."

TCP is like UDP with more metal attached -- sequence numbers, checksums, acknowledgments, congestion windows, retransmit timers, and connection handshakes. It's a reliability kit welded onto the roof.

That's why it's fair to think of TCP as a superset of UDP. UDP handles basic messaging; TCP wraps that same process with an assurance layer. The analogy is simple:

  • UDP: Drive with the top down. You feel the wind; sometimes you get rained on.
  • TCP: The same car, but the top's permanently up -- drier, but heavier and slower.

There's no right or wrong choice, only context.

Your Own Roof, When You Need It

Here's where UDP gets fun, and the convertible gets interesting -- you don't need to pick one or the other. This convertible lets you build your own roof.

It doesn't take much, a tiny layer can give you the best of both worlds. For instance, consider appending this 4-byte header:

Bit(s) Field Description
0 ACK_REQ Whether the sender wants an acknowledgment
1–31 SEQ_NUM Count of "critical" packets sent

Every packet includes a snapshot of how far delivery has progressed. If the receiver spots a skipped number, it sends a NACK asking for a resend. If a packet's flag requests confirmation, it replies with an ACK.

This kind of approach can make UDP as reliable as you need it to be -- without adding TCP's full complexity. You can drive fast with the top down and only raise it when the weather demands.

Calling UDP unreliable is like saying a convertible isn't weatherproof. Technically true, but also missing the point. The freedom to choose is the feature.

UDP doesn't restrict you -- it gives you room to design reliability on your own terms. For games, voice calls, telemetry streams, and IoT control channels, that flexibility isn't just handy; it's essential.

So the next time someone repeats "UDP is unreliable," maybe remind them: it's just a convertible with the top down -- and you're the one holding the controls.

--Lee

联系我们 contact @ memedata.com