Datagram

On this page 7

A datagram is a self-contained, independent unit of data that carries enough addressing information to be routed from source to destination without a prior connection between the endpoints. Among transport protocols in networking, the datagram is the basic unit of connectionless delivery, most familiar as the UDP datagram that travels inside an IP packet.

Datagram service is deliberately minimal: the network makes a best-effort attempt to deliver each message, but it promises nothing about arrival, order or duplication. Applications that need those guarantees must add them themselves or choose a connection-oriented protocol such as TCP.

How datagram delivery works

Each datagram is handled independently by every router along the path. The router reads the destination address, consults its forwarding table and sends the datagram onward, keeping no per-flow state about earlier or later messages from the same sender.

Because there is no setup phase, the first byte of application data can leave the sender immediately. Two datagrams sent back to back may take different routes, arrive in reverse order, arrive twice or not arrive at all, and the network layer will not report any of these outcomes to the sender.

This design follows the end-to-end principle: keep the network core simple and place reliability in the hosts that actually need it. It is also why the Internet scaled so well, since routers never track millions of conversations.

At the receiving host, the operating system matches each arriving datagram to a socket by its destination port and address, then places it in that socket’s receive buffer. If the buffer is full or no application listens on the port, the datagram is dropped; in the second case the host may return an ICMP port unreachable message, which is often the only signal a sender ever gets.

Checksums offer limited protection. The 16-bit checksum detects most accidental corruption, but it cannot detect every error pattern and offers no security against tampering, so applications that need integrity use cryptographic protocols such as DTLS or QUIC on top.

Structure of a UDP datagram

The UDP datagram, defined in RFC 768, has one of the smallest transport headers in common use: 8 bytes in four fields. Everything else is payload handed directly to the application through a socket bound to the destination port number.

The table below lists the header fields and their roles.

FieldSizeFunction
Source port16 bitsIdentifies the sending socket; optional and may be zero
Destination port16 bitsSelects the receiving application socket
Length16 bitsHeader plus payload, minimum 8 bytes
Checksum16 bitsDetects corruption; mandatory over IPv6
UDP datagram header fields

The length field allows up to 65,535 bytes, but in practice a datagram should fit within the path MTU. On standard Ethernet that leaves about 1,472 bytes of payload over IPv4 and 1,452 bytes over IPv6 before fragmentation is required.

Datagram compared with segment, packet and frame

Networking uses several names for a protocol data unit, and the correct one depends on the layer. A datagram usually refers to a connectionless unit at the transport or network layer, while TCP data is called a segment because it belongs to an ordered byte stream.

NameTypical layerConnection model
FrameData link, such as EthernetLocal delivery on one link
Packet or IP datagramNetwork layerConnectionless, routed hop by hop
DatagramTransport layer with UDPConnectionless, message oriented
SegmentTransport layer with TCPConnection oriented byte stream
Protocol data unit names by layer

A second important difference is message boundaries. A datagram arrives either whole or not at all, so one send call matches one receive call. TCP, by contrast, may merge or split writes, which forces applications to add their own framing.

Where datagram transport is the right choice

Datagram transport fits workloads where low latency matters more than perfect delivery, or where the application already implements its own recovery. It is also the only practical option for broadcast and multicast, since a connection cannot be negotiated with an unknown group of receivers.

The following decision table connects common use cases to the reason datagrams suit them.

Use caseWhy datagrams fitTypical protocol
DNS queriesOne short request and reply, no setup costDNS over UDP port 53
Voice and video callsLate data is useless, retransmission adds delayRTP over UDP
Online gamesFresh state replaces lost updatesCustom UDP protocols
Modern web transportReliability built in user spaceQUIC over UDP
Network discoveryBroadcast or multicast to unknown hostsDHCP, mDNS, SSDP
Typical datagram use cases

QUIC illustrates a modern pattern: it uses UDP datagrams as a thin carrier and rebuilds reliability, ordering and congestion control above them, avoiding head-of-line blocking across independent streams.

Limitations and common mistakes

The freedom of datagram transport shifts responsibility to the developer. Because UDP has no built-in congestion control, a careless sender can flood a link and harm other traffic, so guidelines in RFC 8085 ask applications to pace their transmissions.

The table below lists frequent problems, their causes and practical fixes.

SymptomLikely causeRemedy
Large messages vanishIP fragmentation and lost fragmentsKeep datagrams under the path MTU
Out-of-order dataIndependent routing of each datagramAdd sequence numbers in the application
Silent loss under loadFull socket receive buffersIncrease buffers and add acknowledgments
Traffic blocked by middleboxesFirewalls and NAT timeouts for UDPSend keepalives and allow fallback
Amplification abuseSpoofed sources with large repliesValidate clients and limit response size
Datagram pitfalls and remedies

A frequent misconception is that datagrams are inherently unreliable. On a healthy network most arrive intact; the point is simply that delivery is not guaranteed, and robust applications must be designed for the cases where it fails.

The idea in brief

A datagram is like a postcard sent through the mail. Each card carries its own address and travels on its own. There is no phone call first to check that someone is home.

Most postcards arrive, but a few may be late, arrive out of order or get lost. Nobody tells you when that happens. This makes datagrams fast and simple, and the sender must decide whether lost cards matter.

A practical scenario

A company runs an internal video conferencing service for 400 remote employees. Early tests over TCP showed frozen frames and growing delay whenever Wi-Fi links dropped a few packets, because TCP paused the stream to retransmit data that was already too old to display.

The engineering team moved media to RTP over UDP datagrams, sizing each one at about 1,200 bytes to stay safely below typical path MTUs. The codec conceals the occasional missing datagram, and a lightweight feedback channel lowers the bitrate when receivers report loss above about 2 percent.

After the change, calls tolerated brief packet loss with only minor visual artifacts, and end-to-end delay stayed under 150 milliseconds on most connections, which kept conversations natural.