Understanding Zero‑Copy Architecture: Concepts, Techniques, and Applications
Zero‑copy eliminates CPU‑mediated data copies between user and kernel spaces by using DMA and memory‑mapping, dramatically improving I/O performance, reducing context switches, and enabling high‑throughput applications such as file servers, Kafka brokers, and Java networking frameworks.
1. Introduction to Zero‑Copy Architecture
Zero‑copy eliminates CPU‑mediated data copies between user space and kernel space during I/O, improving throughput and reducing latency. It relies on DMA and memory‑mapping techniques to transfer data directly from source to destination.
2. Zero‑Copy Techniques in Linux
2.1 mmap
Memory‑mapped files share the same physical pages between user and kernel, reducing one CPU copy but still requiring a kernel‑side copy.
2.2 sendfile
sendfile transfers data from a file descriptor to a socket with only two context switches, one kernel CPU copy and two DMA copies. Linux 2.4 adds DMA gather to achieve true zero‑copy (0 CPU copies, 2 DMA copies).
2.3 splice / tee
splice creates a pipe between a file and a socket, avoiding CPU copies; tee works similarly but both ends must be pipes.
3. Zero‑Copy in Java and Netty
Netty uses direct ByteBuffers, composite buffers, and FileChannel.transferTo to avoid extra copies. Example Java code shows a traditional read/write loop versus zero‑copy alternatives.
Socket socket = new Socket(HOST, PORT);
InputStream inputStream = new FileInputStream(FILE_PATH);
OutputStream outputStream = new DataOutputStream(socket.getOutputStream());
byte[] buffer = new byte[4096];
while (inputStream.read(buffer) >= 0) {
outputStream.write(buffer);
}
outputStream.close();
socket.close();
inputStream.close();4. Applications
File‑download services, high‑speed network links, Kafka brokers, and data‑acquisition devices benefit from zero‑copy by reducing context switches and CPU load.
Kafka’s transport layer uses FileChannel.transferTo to move log data to a socket without user‑space copies:
/**
* Transfers bytes from `fileChannel` to this `TransportLayer`.
* This method delegates to FileChannel#transferTo but unwraps the
* destination channel to benefit from zero copy.
*/
long transferFrom(FileChannel fileChannel, long position, long count) throws IOException {
return fileChannel.transferTo(position, count, socketChannel);
}Deepin Linux
Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.