Zero-copy (sendfile / splice)
When to Use Zero-copy (sendfile / splice)
In Practice
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.