A server has a file in memory. A client wants that file exactly as it is. Why would the server copy every byte into the application, only for the application to hand those same bytes back to the operating system? Zero-copy networking starts with that question.
Think of a library that photocopies a book at the returns desk, then photocopies the copy at the checkout desk before shipping it. The customer gets the right book. The two desks have still done a surprising amount of work. For a server, that work competes for CPU time, cache space, and memory bandwidth.
What Is Zero-Copy Networking?
Zero-copy networking is a family of techniques that avoid duplicating payload bytes at a specific point in the network data path. Instead of making a new buffer full of the same bytes, cooperating components can refer to an existing buffer and agree on how long it must remain valid.
The scope matters. A system can avoid copying a file through user space while still copying it later in the kernel. A network interface can fetch bytes via direct memory access (DMA) even when the CPU has already copied them twice. Ask which copy disappears, and between which components?
Follow One File: Read and Send vs. Sendfile
Imagine a Linux server sending an unchanged file over TCP. Assume a regular file, a warm page cache (the file’s data is already cached in RAM), and no TLS for this first example.
| Step | Ordinary buffered read and send | File-to-socket sendfile path |
|---|---|---|
| Start | File bytes are in the kernel’s page cache. | Same starting point. |
| Get bytes to the socket | A read copies bytes into an application buffer. A conventional send copies the payload into kernel networking buffers. | The kernel transfers data between the file and socket descriptors without staging the payload in an application buffer. |
| Prepare transmission | The kernel manages TCP and queues data for the driver. | The kernel still manages TCP; on a compatible path it can reference the file pages rather than duplicate the payload. |
| Get bytes to the NIC | The NIC can read transmit buffers via DMA. | With suitable support, the NIC can fetch the referenced payload via DMA as well. |
Linux’s sendfile manual documents the key difference: no payload round trip through user space. It also documents partial transfers and the need to leave transferred file regions unchanged while a zero-copy recipient may still be using them. Serving immutable assets and publishing updates as new file versions fits that constraint better than rewriting a live file in place.
The socket still has a finite capacity. A nonblocking call can make partial progress or report that it would block. The application must keep its place and try again when it can write. The Python selectors guide explains the complementary job of tracking when sockets are ready.
DMA Moves the Bytes; Scatter-Gather Avoids Packing Them First
A driver can point a network interface card at buffers the device is allowed to access. The card’s DMA engine then transfers bytes without the CPU copying each byte itself. The CPU still sets up or manages the work, and the bytes still move.
With scatter-gather DMA, a capable NIC can fetch several regions as part of one transmission. Imagine headers in a small kernel buffer and payload fragments in separate memory pages. The driver describes the regions; the NIC can read them in sequence without first requiring the CPU to assemble one continuous buffer. The Linux DMA mapping guide describes the device mappings and scatterlists that make this possible.
There’s a deceptively similar user-space optimization: writev() can submit several buffers in one call. That may save an application from concatenating them, but the interface itself does not guarantee that the socket stack avoids copying their payload.
What Would Two Copies Cost at 100 Gb/s?
Use 100 Gb/s of hypothetical payload throughput as a round number. That is 12.5 GB/s in decimal units. A copy reads 12.5 GB/s from its source and writes 12.5 GB/s into its destination. Two full copies therefore add up to 50 GB/s of logical buffer reads and writes.
That’s an accounting exercise, not a measured DRAM rate or a claim that a 100 GbE link carries 100 Gb/s of application payload. Protocol overhead reduces payload throughput; caches and the CPU’s store behavior change physical memory traffic. The calculation also leaves out NIC reads, protocol processing, and storage I/O. It isolates one question: how much local buffer work do we ask for per byte delivered?
Three Linux Paths That Avoid Different Copies
| Interface | Payload starts in | Copy it can avoid | Practical condition |
|---|---|---|---|
| sendfile() | A file | The user-space round trip when sending an unchanged file | Compatible descriptors; later stages may still copy |
| MSG_ZEROCOPY | An application transmit buffer | The normal copy into the kernel’s networking buffers | Opt in, track completions, and account for copies on fallback |
| AF_XDP in zero-copy mode | Incoming packets on RX; shared UMEM frames on TX | The packet copy between the driver path and the application’s packet area | Compatible NIC and driver; manage buffer ownership through rings |
splice() offers another route for some in-kernel transfers; at least one descriptor must be a pipe. These tools solve different problems. A static content server, an application that generates its own response bytes, and a high-speed packet processor start with data in different places.
On Windows, Winsock’s TransmitFile provides a file-to-connected-socket interface that uses the operating system’s cache manager. It’s the same category of optimization, with its own API and guarantees.
The Trade: Copies Give Way to Buffer Ownership
With an ordinary copying send, the application can reuse the portion of the source buffer that the call accepted. With Linux MSG_ZEROCOPY, the application opts in with SO_ZEROCOPY, then requests copy avoidance on selected sends. The kernel may hold references to the original pages, so the application must wait for the corresponding completion notification before modifying or reusing the accepted region.
That completion means the kernel has released the buffer. It does not certify delivery to the peer or prove the payload was transmitted without a copy. Linux can fall back to copying and indicate that in the notification. The kernel documentation describes writes above roughly 10 KB as a general starting point for where this interface pays off; page pinning, accounting, and notifications can cost more than a small copy. Measure the crossover for your workload.
On the receive side, AF_XDP illustrates the ownership idea with rings. In zero-copy mode, the application makes empty frames in a shared memory region (UMEM) available to the system. The NIC can DMA incoming packets into them, and the application receives descriptors identifying the populated frames. It must finish reading a frame before returning it for reuse. If the driver cannot support zero-copy mode, AF_XDP can use copy mode by default; explicitly requesting XDP_ZEROCOPY makes the bind fail if that mode is unavailable.
Slow consumers and outstanding transmissions still tie up buffers. That can put pressure on a finite buffer pool even if copies are cheap or gone. Backpressure in networking explains how a system can keep producers from outrunning downstream capacity.
What Changes When You Add TLS?
If a user-space TLS library needs the plaintext to build encrypted records, the plain TCP file-to-socket route cannot simply bypass that library and remain encrypted. Compression, rewriting, and other payload transformations also change where bytes must be read or written.
Linux kernel TLS can move TLS record processing into the kernel or, with supported hardware offload, to the NIC. The documented TLS_TX_ZEROCOPY_RO option allows sendfile data to be handed to the NIC without the extra in-kernel copy on the device-offload path. The file data must stay unchanged while needed for transmission. It’s a specific combination of API, configuration, and hardware, not a property of all HTTPS servers.
How to Tell Whether It Helped
Start with a path that serves large, unchanged payloads and spends measurable CPU time copying data. Then compare ordinary buffered I/O with the candidate path:
- Keep the scenarios comparable. Use the same content, clients, concurrency, and link. Test warm and cold cache runs separately. If production uses TLS, compare with the production TLS path too.
- Measure CPU cost per gigabyte served, throughput, and tail latency. A link that was already full may deliver the same throughput while freeing CPU capacity for other work. Watch memory held by outstanding buffers as well as CPU.
- Verify the path you exercised. A system-call trace can show that the application called sendfile, but it does not prove that every later stage avoided copies. For MSG_ZEROCOPY examine the completion status; for AF_XDP check the selected mode. A TCP or UDP loopback test does not demonstrate MSG_ZEROCOPY savings on a real NIC: Linux documents a deferred copy on that local path.
For small messages or a server bottlenecked on disk, encryption, or the link itself, the simpler design may be just as good. The useful question is how much work the server does to move each gigabyte, and which of that work the workload can afford to give up.
References and Further Reading
- Linux man-pages — sendfile(2)
- Linux kernel documentation — Dynamic DMA Mapping Guide
- Linux man-pages — readv(2) and writev(2)
- Linux man-pages — splice(2)
- Microsoft Learn — Winsock TransmitFile
- Linux kernel documentation — MSG_ZEROCOPY
- Linux kernel documentation — AF_XDP
- Linux kernel documentation — Kernel TLS
