Sweaty Imposter

Zero-copy (sendfile / splice)

TechniqueNetworking and OSSeniorStaffUpdated
Move bytes from disk to socket without copying through user space. The kernel hands page-cache pages directly to the NIC's DMA engine.

When to Use Zero-copy (sendfile / splice)

Static file servers, video CDN origin nodes, Kafka brokers shipping log segments. Removes 2 copies and 2 context switches per buffer; throughput goes up and CPU goes down.

In Practice

Kafka's broker uses sendfile(2) to push committed log segments straight from the page cache to consumer sockets, which is a big part of why a single broker can saturate a 10GbE NIC. Nginx uses it for static content via the sendfile on; directive.

How Zero-copy (sendfile / splice) Comes Up in Interviews

Scenario

A Kafka broker serves 500MB/s of consumer reads from disk on a single 10GbE NIC and barely uses CPU. What kernel feature is doing the heavy lifting?

sendfile(2) hands page-cache pages straight to the NIC without copying through user space. Without it, every byte would go disk -> page cache -> user buffer -> socket buffer -> NIC, burning CPU on memcpy.

TLS used to break this because encryption needed bytes in user space. Kernel TLS restores zero-copy for HTTPS by doing the AES inside the kernel, which is how Netflix's Open Connect serves 800Gb/s per box.

Scenario

You are writing a high-throughput static file server. The hot path reads from disk and writes to socket. How do you cut CPU per byte to near zero?

Use sendfile(2) / splice(2) so bytes never enter user space. Combine with the page cache so hot files are served from RAM. Nginx's sendfile on; is the canonical example.

Pair with tcp_nopush (Linux) / TCP_CORK so headers and file data are coalesced into full-sized segments, avoiding the small-write penalty.