Kafka is commonly used as message queue middleware, and one of the major contributors to its high throughput is zero copy. Data does not need to be copied many times, and the system performance is greatly improved. In fact, zero copy is used not only in Kafka, but also in Java NIO, Netty, rocketMQ and other frameworks. To understand what zero-copy is, you need to start with a few concepts of I/O.

The cache I/O

Cache I/O, also known as standard I/O, is the size of the file system default I/O. To reduce the number of disk reads and protect the system security, cache I/O separates the kernel space from the user space to some extent. However, data needs to be copied between user space and kernel space for many times in the process of transmission, and the CPU and memory cost brought by these data copying operations are very high.

The following example lists the steps required for a cache IO read and write:

For cache I/O, a read operation has three data copies, and a write operation has three data copies.

Read operations: Disk -> kernel cache -> user space cache -> Application memory.

Write operations: Application memory -> user space cache ->Socket cache -> Network.

Direct I/O

Direct IO means there is no user-level cache, but there is a kernel cache. This saves one copy of the data from the kernel buffer to the user program cache. As shown below:

Memory-mapped file

First, mapping means establishing a one-to-one correspondence between the location of a file on the hard disk and the logical address of a process. This correspondence is a purely logical concept and does not exist physically. In memory mapping, there is no actual copy of the data. The file is not loaded into memory, but is logically placed in memory, which is really just a logical address.

As shown in the figure above, the data no longer passes through the application memory and goes directly from the kernel cache to the socket buffer.

Zero copy

Zero copy also saves the kernel cache and socket cache. The underlying nic driver reads the kernel cache directly and sends the data to the network. During the entire process, only two copies of data took place. Once from disk to the kernel cache, and once from the kernel cache to the network. Since there is a data copy, why is it called “zero copy”, that is because the zero copy refers to no data copy in memory.