Implementing Reliability on Top of UDP
Building reliability on top of the User Datagram Protocol (UDP) involves implementing custom transport-layer features at the application layer to guarantee packet delivery, ordering, and integrity. While standard UDP prioritizes speed over reliability by omitting handshakes and delivery guarantees, developers can combine sequence tracking, acknowledgment mechanisms, retransmission timers, and congestion control to construct a reliable UDP (RUDP) protocol tailored for specific use cases like gaming, live streaming, or real-time communications.
1. Packet Sequencing and Reordering
UDP does not guarantee that packets arrive in the order they were sent. To resolve this, sender applications attach a monotonically increasing sequence number to the header of every outgoing packet.
- Packet Identification: The receiver checks sequence numbers to detect missing, duplicate, or out-of-order packets.
- Jitter Buffers / Reorder Queues: When packets arrive out of sequence, they are placed in a memory buffer. The application sorts them and only processes packets once preceding sequence numbers have arrived.
2. Acknowledgments (ACKs and NACKs)
To confirm receipt, the receiver must communicate which packets have successfully arrived.
- Positive Acknowledgments (ACKs): The receiver sends an ACK packet back containing the sequence number of the received data. Implementations often use Cumulative ACKs (acknowledging all packets up to a sequence number) or Selective ACKs (SACK) (acknowledging specific ranges of received packets to minimize redundant retransmissions).
- Negative Acknowledgments (NACKs): The receiver explicitly notifies the sender when a gap in sequence numbers is detected, requesting an immediate retransmission of only the missing packets.
3. Retransmission Strategies (ARQ Mechanisms)
When packets are lost, Automatic Repeat reQuest (ARQ) strategies handle recovery:
- Stop-and-Wait ARQ: The sender transmits a packet and waits for an ACK before sending the next. Simple, but suffers from low throughput.
- Go-Back-N ARQ: The sender transmits multiple packets within a sliding window. If a packet is lost, the sender retransmits that packet and all subsequent packets.
- Selective Repeat ARQ: The sender retransmits only the specific packets that timed out or were flagged by SACK/NACK, optimizing bandwidth usage.
4. Dynamic Round-Trip Time (RTT) and Adaptive Timeouts
Static timeout intervals cause either unnecessary retransmissions or high latency. Reliable UDP systems measure the Round-Trip Time of ACK packets continuously.
- RTT Estimation: Algorithms (such as Jacobson’s algorithm used in TCP) calculate Smoothed RTT (SRTT) and RTT Variance (RTTVAR).
- Retransmission Timeout (RTO): The timeout threshold dynamically scales based on network conditions (\(RTO = SRTT + 4 \times RTTVAR\)), with exponential backoff applied during consecutive timeouts.
5. Flow and Congestion Control
To prevent overwhelming the receiver’s buffer or the underlying network path, flow and congestion management must be implemented:
- Sliding Window Flow Control: The receiver advertises its available buffer capacity in its ACK packets, preventing the sender from transmitting more data than the receiver can buffer.
- Congestion Avoidance: Algorithms monitor packet loss and latency shifts to throttle transmission rates. Implementations often use window-based methods (AIMD—Additive Increase/Multiplicative Decrease) or rate-based pacing (e.g., Token Bucket algorithms) to adapt to network capacity.
6. Forward Error Correction (FEC)
For ultra-low-latency applications where waiting for a retransmission round-trip is intolerable, FEC adds redundancy to the data stream:
- Parity Packets: Using algorithms like XOR parity or Reed-Solomon coding, the sender transmits extra recovery packets alongside the payload.
- Loss Recovery: The receiver can reconstruct a small number of lost packets mathematically on the fly without issuing retransmission requests.
7. Connection State and Session Management
Because UDP is connectionless, applications must track connection lifecycles independently:
- Virtual Handshakes: Custom SYN/ACK exchanges establish sessions and negotiate protocol parameters (such as MTU size and initial sequence numbers).
- Keep-Alives (Heartbeats): Small, periodic ping packets maintain NAT bindings and detect disconnected peers when no application data is flowing.
- Connection IDs: Protocols like QUIC assign unique Connection IDs to packets, allowing seamless session migration across IP or network changes.