The cost of copies
Serving a file the naive way copies the data several times. A read pulls it from the page cache into a kernel buffer, then into a user buffer. A write copies it back into a socket buffer in the kernel. Each copy burns CPU and memory bandwidth, and each transition crosses the user kernel boundary.
What zero copy removes
Zero copy techniques cut the redundant copies and context switches. The classic primitive is sendfile, which tells the kernel to move bytes from a file descriptor straight to a socket without ever surfacing them into user space. The application never sees the bytes, so its buffer is never touched.
- sendfile sends file data directly from the page cache toward the network
- With scatter gather support the kernel can hand buffers to the network card without even an internal copy
- splice moves data between descriptors through an in kernel pipe
Tradeoffs
- Zero copy shines for static content where the app does not need to inspect bytes
- It cannot help when you must transform data, such as encryption in user space
- It reduces CPU use and frees memory bandwidth for other work
Key idea
Zero copy primitives like sendfile push file data from the page cache to the socket without copying it through a user space buffer, saving CPU and memory bandwidth.