Back to series
· 14 min read

UDP Protocol: Why Do Some Scenarios Not Need Reliable Transmission?


I. What Is UDP?

UDP stands for User Datagram Protocol. It is a transport layer protocol in the TCP/IP protocol suite.

In network communication, applications usually do not handle IP packets directly; instead, they communicate between processes through a transport layer protocol.

The transport layer primarily solves two problems:

  • Inter-process communication: Port numbers distinguish different applications running on the same host.
  • Data transmission: Hand application data to the network layer for sending, and deliver received data to the corresponding application.

TCP and UDP are the two most common transport layer protocols.

Their design goals differ:

  • TCP focuses more on reliable, ordered data transmission.
  • UDP focuses more on simple, fast datagram transmission.

1. Core Characteristics of UDP

UDP has the following characteristics:

Characteristic Description
Connectionless No connection needs to be established before sending data
Stateless The protocol itself does not maintain connection state
Unreliable transmission Does not guarantee that data will arrive
Unordered transmission Does not guarantee that data arrives in the order it was sent
No retransmission mechanism Does not automatically retransmit data because of loss
Datagram-oriented Preserves application layer message boundaries
Low overhead The header is a fixed 8 bytes
Supports broadcast and multicast Can send data to multiple receivers

Here, "unreliable" does not mean that UDP will necessarily drop packets.

It means the UDP protocol itself provides no guarantee of reliable delivery. Data may arrive normally, or it may be lost, duplicated, or arrive out of order, depending on the network conditions and how the application handles it.

2. Where UDP Sits in the Network Protocol Stack

UDP resides at the transport layer and runs on top of IP.

flowchart TD
    A["Application Layer<br/>DNS, DHCP, Audio/Video, Games"] --> B["Transport Layer<br/>UDP"]
    B --> C["Network Layer<br/>IPv4 or IPv6"]
    C --> D["Data Link Layer<br/>Ethernet, Wi-Fi"]
    D --> E["Physical Medium<br/>Cable, Wireless Signal"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style C fill:#EEE8FA,stroke:#B9A7DF,color:#55427A
    style D fill:#FFF4D6,stroke:#E7C96A,color:#715B1F
    style E fill:#F1F5F9,stroke:#CBD5E1,color:#334155

The application hands data to UDP; UDP adds its own header and then passes the data to the IP layer.

The IP layer is responsible for delivering the datagram to the destination host.

After the destination host receives the IP packet, it uses the UDP destination port to deliver the data to the corresponding application.

Note that UDP is not responsible for confirming whether the destination application has successfully processed the data. Its main work is datagram encapsulation, port multiplexing and demultiplexing, and checksum verification.

II. UDP Message Structure

The structure of a UDP message is very simple.

A UDP datagram consists of two parts:

  1. The UDP header.
  2. The UDP data.

The UDP header is a fixed 8 bytes.

1. UDP Header Fields

Field Length Purpose
Source Port 16 bit Identifies the port of the sending application
Destination Port 16 bit Identifies the port of the receiving application
Length 16 bit Total length of the UDP header and data
Checksum 16 bit Detects errors during message transmission

UDP header structure:

  0                15 16               31
 +-------------------+-------------------+
 |    Source Port    | Destination Port  |
 +-------------------+-------------------+
 |      Length       |     Checksum      |
 +-------------------+-------------------+
 |                                       |
 |               UDP Data                |
 |                                       |
 +---------------------------------------+

2. Source Port

The source port is the port used by the application that sends the data.

For example:

Client IP: 192.168.1.10
Client port: 52000

The application sends UDP datagrams through this port.

The source port can also be 0, but real applications usually use a valid ephemeral port.

3. Destination Port

The destination port identifies the receiving application on the destination host.

For example:

Server IP: 192.168.1.20
Server port: 53

Port 53 is commonly used for DNS services.

After the destination host receives a UDP datagram, it delivers the data to the corresponding socket based on the destination port.

Common UDP service ports:

Port Protocol or Service Description
53 DNS Domain name resolution
67 DHCP Server DHCP server
68 DHCP Client DHCP client
69 TFTP Trivial file transfer
123 NTP Network time synchronization
161 SNMP Network device management
500 IKE IPsec key exchange
514 Syslog System logs; common UDP usage

A port number is merely a convention; it does not mean that only the designated protocol can run on a given port.

4. UDP Length Field

The UDP Length field indicates the length of the entire UDP datagram:

UDP Length = UDP Header + UDP Data

Since the UDP header is fixed at 8 bytes:

UDP Data Length = UDP Length - 8

For example:

UDP Length = 108
UDP Header = 8
UDP Data = 100

The UDP data portion is therefore 100 bytes.

The UDP Length field is 16 bits, so its range is 0 to 65535.

However, actual UDP datagrams are also limited by the IP layer, network MTU, operating system, and protocol implementation, so you cannot simply assume that all environments can send a UDP payload close to 64 KB.

5. UDP Checksum

The UDP checksum is used to detect whether data corruption occurs during transmission.

It covers not only the UDP header and data but is also calculated together with the pseudo-header provided by the IP layer.

The pseudo-header contains:

  • Source IP address.
  • Destination IP address.
  • Protocol number.
  • UDP length.

This helps detect whether a datagram has been incorrectly delivered to another address or protocol.

Different IP versions have different requirements for the UDP checksum:

Network Protocol UDP Checksum
IPv4 A zero value may be used to indicate that the checksum is not enabled
IPv6 The UDP checksum is generally required, with limited exceptions specified by the standard

The checksum can only help detect errors; it cannot repair them and does not provide data encryption or identity authentication.

III. Why Does UDP Not Need a Three-Way Handshake?

Before sending application data, TCP needs to establish a connection through a three-way handshake.

UDP does not.

This is because their design models differ.

TCP needs to maintain connection state, including:

  • Initial sequence number.
  • Send window.
  • Receive window.
  • Retransmission timer.
  • Congestion control state.
  • Connection state machine.

UDP does not maintain these connection-level states.

When an application calls sendto() to send data, UDP can directly encapsulate the datagram and pass it to the IP layer.

1. UDP Communication Process

sequenceDiagram
    participant A as Client application
    participant UA as Client UDP
    participant IP as IP network
    participant UB as Server UDP
    participant B as Server application

    A->>UA: Call sendto
    UA->>IP: Encapsulate UDP datagram
    IP->>UB: Transmit datagram
    UB->>B: Deliver data based on destination port

The entire process does not require:

  • SYN packet.
  • SYN + ACK packet.
  • The third ACK.
  • TCP connection state establishment.

This reduces the interaction overhead of connection establishment.

2. If UDP Does Not Need a Connection, Does That Mean It Has No State at All?

No.

UDP itself does not maintain connection state like TCP, but the operating system and applications may still maintain related state.

For example:

  • UDP socket.
  • Local bound port.
  • Remote address and port.
  • NAT mapping.
  • Firewall connection tracking table.
  • Application-layer session state.

For example, an application can call:

sock.connect(("127.0.0.1", 9000))

For a UDP socket, this usually does not perform a network handshake; instead, it sets the default communication target and may affect the kernel's data sending/receiving and filtering behavior for that socket.

Therefore, UDP's connectionlessness is a protocol-level concept; it does not mean that the operating system and applications maintain no state at all.

IV. Differences Between UDP and TCP

TCP and UDP both operate at the transport layer, but they solve problems in different ways.

1. Core Differences

Comparison Dimension TCP UDP
Connection mode Connection-oriented Connectionless
Data unit Byte stream Datagram
Reliability Provides reliable, ordered transmission Does not guarantee reliable delivery
Handshake Three-way handshake No protocol handshake
Connection release Has a connection closing process No protocol-level connection release
Data ordering Guarantees ordered delivery Does not guarantee order
Retransmission Built-in retransmission mechanism No protocol-level automatic retransmission
Flow control Supported Not provided
Congestion control Supported Not provided
Header length Usually at least 20 bytes Fixed 8 bytes
Message boundaries Not preserved Preserved
Broadcast and multicast Not directly provided Supports IP broadcast and multicast scenarios

2. TCP Is a Byte Stream, UDP Is a Datagram

This is a very important difference between the two protocols.

Suppose the application sends data twice:

First: Hello
Second: World

For TCP, the receiver may receive it through multiple recv() calls:

HelloWorld

It may also be split into:

Hel
loWor
ld

because TCP does not preserve application-layer message boundaries.

For UDP, send two datagrams:

Datagram 1: Hello
Datagram 2: World

The receiver will receive the two datagrams separately, rather than having them automatically merged into one datagram.

This means:

  • The TCP application layer needs to design message boundaries itself.
  • The UDP application layer can directly use datagrams as message units.

However, UDP datagrams may also be lost, duplicated, or arrive out of order, and the receiver must handle these situations according to business needs.

3. TCP and UDP Data Transmission Models

flowchart TD
    A["Application sends two messages"] --> B{"Which protocol is used"}
    B -->|TCP| C["Converted into a continuous byte stream"]
    C --> D["Network segmentation and transmission"]
    D --> E["Receiver reads as a byte stream"]
    E --> F["Application layer parses message boundaries itself"]
    B -->|UDP| G["Encapsulated into independent datagrams"]
    G --> H["Transmit datagrams separately"]
    H --> I["Receiver receives datagrams one by one"]
    I --> J["Application layer processes messages"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#FFF4D6,stroke:#E7C96A,color:#715B1F
    style C fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style D fill:#EEE8FA,stroke:#B9A7DF,color:#55427A
    style E fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style F fill:#F1F5F9,stroke:#CBD5E1,color:#334155
    style G fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style H fill:#EEE8FA,stroke:#B9A7DF,color:#55427A
    style I fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style J fill:#F1F5F9,stroke:#CBD5E1,color:#334155

4. Is UDP Always Faster Than TCP?

It cannot simply be assumed that UDP is always faster than TCP.

UDP's protocol mechanisms are simpler, reducing overhead such as connection establishment, acknowledgments, retransmission, and flow control.

However, actual communication speed also depends on:

  • Network bandwidth.
  • Network latency.
  • Packet loss rate.
  • Datagram size.
  • Application-layer protocol design.
  • CPU and memory overhead.
  • Network device processing capacity.

For example:

In a LAN with good network quality, UDP may have lower protocol processing overhead.

However, in a network with severe packet loss, if the application needs to implement reliable transmission, retransmission, congestion control, and flow control itself, the overall complexity and overhead may exceed those of TCP.

The correct understanding is:

UDP provides a simpler transport mechanism, and the application can decide whether to add reliability based on business requirements; it is not that UDP inherently guarantees faster performance.

V. Why Doesn't UDP Guarantee Reliable Transmission?

TCP uses multiple mechanisms to ensure reliable data delivery.

UDP does not implement these mechanisms at the transport layer.

1. UDP Does Not Provide an Acknowledgment Mechanism

After TCP sends data, the receiver acknowledges the data it has received via ACK.

UDP does not automatically return an ACK.

For example:

sequenceDiagram
    participant A as Sender
    participant B as Receiver

    A->>B: UDP datagram 1
    A->>B: UDP datagram 2
    Note over B: Datagram 2 is lost
    B-->>A: No protocol-level automatic acknowledgment
    Note over A: The sender does not know whether the datagram arrived

The sender cannot determine solely through the UDP protocol whether the receiver has received the data.

If the application requires an acknowledgment mechanism, it can design one at the application layer.

2. UDP Does Not Provide Automatic Retransmission

TCP determines whether data needs retransmission based on mechanisms such as ACKs, duplicate acknowledgments, and timeouts.

UDP does not automatically retransmit lost datagrams.

For example:

Sender:
Packet 1
Packet 2
Packet 3

Network:
Packet 1 arrives
Packet 2 is lost
Packet 3 arrives

Receiver:
Packet 1
Packet 3

The receiver will not automatically ask the UDP protocol to resend just because Packet 2 was lost.

If the application requires reliable delivery, it must implement acknowledgment and retransmission itself.

3. UDP Does Not Guarantee Data Order

Suppose the sender sends in sequence:

Packet 1
Packet 2
Packet 3

The network may experience path changes, queuing delays, or scheduling differences.

The receiver may receive them in the following order:

Packet 2
Packet 1
Packet 3

UDP is not responsible for reordering.

The application can add sequence numbers to datagrams and then reorder them at the receiving end.

4. UDP Does Not Provide Flow Control

TCP uses a receive window mechanism to control the sender's data transmission rate and prevent the receiver's buffer from being filled with excessive data.

UDP has no protocol-level receive window.

If the sender keeps sending at high speed while the receiver has insufficient processing capacity, the following may occur:

  • Receive buffer overflow.
  • The kernel drops datagrams.
  • The application does not read data in time.
  • The datagram loss rate increases.

Therefore, UDP applications usually need to design their own rate control and buffer management strategies.

5. UDP Does Not Provide Congestion Control

TCP uses congestion control algorithms to dynamically adjust the sending rate according to network conditions.

UDP itself does not provide congestion control.

If a large number of UDP applications send data without restraint, they may worsen network congestion and affect other communication traffic.

Especially in the Internet environment, UDP applications still need to follow reasonable congestion control principles.

For example:

  • Limit the sending rate.
  • Monitor the packet loss rate.
  • Dynamically adjust the sending frequency.
  • Control the datagram size.
  • Reduce the sending load when the network is congested.

VI. Why Is UDP Still Very Important?

Since UDP does not guarantee reliability, why do DNS, real-time audio and video, online games, and QUIC all use UDP?

The reason is that not all applications require a fully reliable byte-stream transmission.

Some applications care more about real-time performance, low latency, and message independence.

1. Real-Time Audio and Video

Real-time audio and video are very sensitive to latency.

Suppose a packet is lost during a video call.

If a strict retransmission mechanism is used, the sender may wait for retransmission to complete before continuing to process subsequent data.

However, for real-time audio and video, data that is already expired may have little value.

For example:

Video frame 1: should be displayed at 10:00:00
Video frame 2: should be displayed at 10:00:00.033
Video frame 3: should be displayed at 10:00:00.066

If video frame 1 is lost, by the time it is successfully retransmitted, video frames 2 and 3 may already have passed their optimal playback time.

In this case, appropriately discarding old data and continuing to play new data can instead provide a better user experience.

UDP's characteristics suit this scenario:

  • No need to establish a TCP connection.
  • Datagrams are independent of one another.
  • A lost datagram does not automatically block subsequent data.
  • The application can decide whether to discard or repair data based on its time value.

Common real-time audio and video technologies:

Technology Purpose
RTP Carries real-time audio and video data
RTCP Provides transport quality feedback
Jitter Buffer Buffers and smooths network jitter
FEC Forward error correction, reducing the need for retransmission
NACK Requests retransmission of lost data
PLC Packet loss concealment, restoring audio continuity as much as possible

Note that RTP is commonly carried over UDP, but RTP itself does not guarantee reliable delivery.

2. Online Games

Online games frequently need to transmit:

  • Player positions.
  • Character orientation.
  • Movement speed.
  • Skill states.
  • Real-time input commands.
  • Game state synchronization.

This data is usually highly time-sensitive.

For example:

Frame 100: player position X=10
Frame 101: player position X=11
Frame 102: player position X=12

If the position update in frame 100 is lost but frame 102 has already arrived, the server can simply use the latest position instead of waiting for the old position to be retransmitted.

If every position update were delivered strictly in TCP byte-stream order, lost data could block the reading of subsequent data, which would increase real-time interaction latency.

However, not all game data is suited to unreliable transmission.

For example:

  • Real-time position updates can tolerate some loss.
  • Important skill-cast events may require acknowledgment.
  • Trades and item changes must be guaranteed reliable.
  • Login and account data must be transmitted reliably.

Therefore, games usually design different reliability strategies depending on the type of data.

3. DNS Domain Name Resolution

DNS is one of the most typical use cases for UDP.

When a client queries a domain name, it can usually send the DNS request over UDP.

For example:

Query: www.example.com
Server: 8.8.8.8
Port: 53

A typical DNS query process:

sequenceDiagram
    participant C as DNS Client
    participant S as DNS Server

    C->>S: UDP DNS Query
    S->>C: UDP DNS Response
    Note over C,S: Query complete

Why DNS uses UDP:

  • Query messages are usually relatively small.
  • There is no need to establish a TCP connection for each query.
  • Requests and responses can usually be completed quickly.
  • The DNS application layer can match requests and responses via a transaction ID.
  • After a timeout, the query can simply be retried.

But DNS is not limited to UDP.

DNS may also use TCP in the following cases:

  • The response data is large.
  • The UDP response is truncated and must be retried over TCP.
  • Zone transfers.
  • Certain DNS services or implementations require TCP.

In addition, DNS can also run over other transport protocols via DoT, DoH, and DoQ.

4. DHCP Dynamic Address Allocation

DHCP is used to automatically assign IP addresses and network configuration to network devices.

In an IPv4 network, a client may not yet have its own IP address when it first boots.

In this case, DHCP interaction is usually carried out via UDP broadcast.

A typical DHCP process:

sequenceDiagram
    participant C as DHCP Client
    participant S as DHCP Server

    C->>S: DHCP Discover
    S->>C: DHCP Offer
    C->>S: DHCP Request
    S->>C: DHCP ACK

Common ports:

Role UDP Port
DHCP server 67
DHCP client 68

DHCP uses UDP because the client needs to send requests over the network before it has obtained an IP address.

The broadcast mechanism helps clients discover the DHCP server on the local network.

5. NTP Time Synchronization

NTP is used for time synchronization between computers.

Ordinary NTP clients usually communicate with time servers over UDP port 123.

Since time synchronization messages are usually small and the request/response exchange is fairly simple, UDP is very well suited to this scenario.

6. QUIC and HTTP/3

QUIC is a modern transport protocol built on UDP.

It is used by HTTP/3 as its underlying transport mechanism.

Pay particular attention:

Although QUIC uses UDP, it does not simply abandon reliable transmission.

QUIC implements the following on top of UDP:

  • Reliable data transmission.
  • Packet loss detection and retransmission.
  • Congestion control.
  • Flow control.
  • Multiplexing.
  • TLS 1.3 integration.
  • Connection migration.

The protocol layers of HTTP/3:

flowchart TD
    A["HTTP/3"] --> B["QUIC"]
    B --> C["UDP"]
    C --> D["IP"]
    D --> E["Link Layer"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style C fill:#EEE8FA,stroke:#B9A7DF,color:#55427A
    style D fill:#FFF4D6,stroke:#E7C96A,color:#715B1F
    style E fill:#F1F5F9,stroke:#CBD5E1,color:#334155

Why doesn't QUIC use TCP directly?

Because some of TCP's transmission mechanisms reside in the operating system kernel, so protocol evolution and deployment are subject to certain limitations.

QUIC implements a large amount of transport-layer capability in user space, allowing more flexible protocol upgrades and integrating encryption with transport connection management.

In addition, QUIC multiplexing can avoid TCP byte-stream-level head-of-line blocking between different application data streams.

7. Can UDP reliability be implemented at the application layer?

Yes.

UDP does not provide reliable transmission, but that does not mean applications based on UDP cannot implement reliable transmission.

The application layer can add various mechanisms on top of UDP.

For example:

  • Sequence numbers.
  • Acknowledgments.
  • Timeout retransmission.
  • Data ordering.
  • Deduplication.
  • Flow control.
  • Congestion control.
  • Data checksums.
  • Forward error correction.

1. Basic design of application-layer reliable transmission

Suppose the sender needs to send 3 datagrams:

Packet 1
Packet 2
Packet 3

The application layer assigns a sequence number to each datagram:

Packet 1:SEQ=1
Packet 2:SEQ=2
Packet 3:SEQ=3

After receiving the data, the receiver returns an ACK:

ACK=1
ACK=2
ACK=3

If the sender does not receive an ACK within the specified time, it resends the corresponding data.

2. Basic flow of reliable UDP

flowchart TD
    A["Application generates data"] --> B["Assign sequence number"]
    B --> C["Send UDP datagram"]
    C --> D["Start retransmission timer"]
    D --> E["Receiver receives data"]
    E --> F{"Is the sequence number duplicate?"}
    F -->|Yes| G["Discard duplicate data and send ACK"]
    F -->|No| H["Buffer or deliver data"]
    H --> I["Send ACK"]
    G --> J["Sender receives ACK"]
    I --> J
    J --> K["Stop corresponding timer"]
    K --> L["Send next batch of data"]
    D --> M{"Timeout?"}
    M -->|Yes| N["Resend data"]
    N --> D
    M -->|No| O["Continue waiting for ACK"]

    style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style B fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style C fill:#EEE8FA,stroke:#B9A7DF,color:#55427A
    style D fill:#FFF4D6,stroke:#E7C96A,color:#715B1F
    style E fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style F fill:#FFF4D6,stroke:#E7C96A,color:#715B1F
    style G fill:#F1F5F9,stroke:#CBD5E1,color:#334155
    style H fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style I fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style J fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style K fill:#DCEFE5,stroke:#8EBFA5,color:#24563F
    style L fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
    style M fill:#FFF4D6,stroke:#E7C96A,color:#715B1F
    style N fill:#EEE8FA,stroke:#B9A7DF,color:#55427A
    style O fill:#F1F5F9,stroke:#CBD5E1,color:#334155

This is only a simplified reliable transmission model.

Production-grade protocols also need to consider:

  • ACK loss.
  • Duplicate datagrams.
  • Out-of-order arrival.
  • Network congestion.
  • Retransmission backoff.
  • Receive window.
  • Connection identifiers.
  • Authentication.
  • Data encryption.

3. Why can't we implement only timeout retransmission?

If an application implements only timeout retransmission, some problems will arise.

For example:

The sender sends data once every 100 milliseconds.

When the network is already severely congested, the continuous retransmission of large numbers of datagrams may further increase the burden on the network.

Therefore, a reliable UDP protocol also needs to implement sensible congestion control.

At the same time, the retransmission interval should not be fixed either; it should be adjusted dynamically based on the network RTT and packet loss conditions.

4. Common Protocols and Technologies Built on UDP

Protocol or Technology Underlying Transport Primary Use
RTP Usually UDP Real-time audio and video
RTCP Usually UDP Real-time transport quality feedback
QUIC UDP Reliable, multiplexed, and encrypted transport
DNS Usually UDP, with TCP also supported Domain name resolution
DHCP UDP Dynamic address allocation
NTP UDP Time synchronization
TFTP UDP Simple file transfer
WireGuard UDP VPN tunnel transport

Among these, QUIC is a very typical example: it leverages UDP's simple datagram transport capability and reimplements complex transport control functions at the upper layer.

VIII. UDP Datagram Boundaries and MTU

UDP preserves datagram boundaries, but that does not mean a datagram can be arbitrarily large.

UDP datagrams ultimately have to be transmitted over an IP network, and IP packets are limited by the network MTU.

1. What Is MTU?

MTU stands for Maximum Transmission Unit, which is the maximum length of a network-layer packet that the link layer is allowed to carry.

A common Ethernet MTU is 1500 bytes.

For IPv4:

IP MTU = 1500
IPv4 Header = 20
UDP Header = 8

With no IP options, the maximum UDP data portion is:

1500 - 20 - 8 = 1472 bytes

For IPv6:

IP MTU = 1500
IPv6 Header = 40
UDP Header = 8

The maximum UDP data portion is:

1500 - 40 - 8 = 1452 bytes

The calculations here assume there are no additional extension headers or tunnel encapsulation overhead.

2. What If a UDP Datagram Is Too Large?

If a UDP datagram exceeds the path MTU, IP fragmentation may occur, or the datagram may be dropped because of the path MTU or the policies of intermediate devices.

IPv4 can perform IP fragmentation, whereas IPv6 routers will not fragment on behalf of the sender; typically the source host must adjust the packet size according to the path MTU.

IP fragmentation introduces additional risks:

  • If one fragment is lost, the entire original IP datagram may fail to be reassembled.
  • The receiving end must consume reassembly buffers.
  • Intermediate network devices may restrict or drop fragments.
  • Fragmentation increases processing complexity.

Therefore, UDP applications should generally avoid sending overly large datagrams.

3. Recommendations for UDP Datagram Size

For UDP applications that need to traverse the Internet, the datagram size should generally be kept as small as practical to avoid relying on IP fragmentation.

The actual size should be determined based on the path MTU, the network environment, and encapsulation overhead.

If the path MTU cannot be determined, a more conservative datagram size can be used and then adjusted based on error feedback.

Pay particular attention to the fact that VPNs, tunnels, and container networks may introduce additional encapsulation overhead, making the actual usable MTU smaller than the physical network MTU.

IX. UDP Socket Programming in Practice

Python's socket module provides UDP communication capabilities.

TCP typically uses SOCK_STREAM, while UDP uses SOCK_DGRAM.