Zero Copy. One Of Reason Behind Why Kafka So Fast.

ANKIT SHEORAN
3 min readApr 15, 2022

--

Many time we have listened and noticed that why kafka is so fast , one of many reason is kafka use zero copy. Consider the scenario of reading from a file and transferring the data to another program over the network. Internally, the copy operation requires four context switches between user mode and kernel mode, and the data is copied four times before the operation is complete.

  1. From disk data is copy in kernel read buffer . The first copy is performed by the direct memory access (DMA) engine, which reads file contents from the disk and stores them into a kernel address space buffer. As Before this we were in user mode and to do this one context switch required.
  2. From kernel read buffer it copy to application read buffer(context switch , switch to user mode).
  3. From application buffer it will copy to kernel socket buffer and put the data into a kernel address space buffer again(switched to kernel mode).
  4. From socket buffer it will copy to network buffer

After all four operation done then it will again switch to user mode.

If you re-examine the traditional scenario, you’ll notice that the second and third data copies are not actually required.The Java class libraries support zero copy on Linux and UNIX systems through the transferTo() method in java.nio.channels.FileChannel. Applications that use zero copy request that the kernel copy the data directly from the disk file to the socket, without going through the application. Zero copy greatly improves application performance and reduces the number of context switches between kernel and user mode.The transferTo() method lets you do exactly this.Lets us see how transferTo() copy the data.

  1. The transferTo() method causes the file contents to be copied into a read buffer by the DMA(Direct memory access) engine.
  2. Then the data is copied by the kernel into the kernel buffer associated with the output socket.
  3. From socket buffer it will copy to network buffer

Here we also need 3 copy and 2 context switches .

But this does not yet get us to our goal of zero copy. e can further reduce the data duplication done by the kernel if the underlying network interface card supports gather operations. In Linux kernels 2.4 and later, the socket buffer descriptor was modified to accommodate this requirement.

  1. The transferTo() method causes the file contents to be copied into a kernel buffer by the DMA engine.
  2. No data is copied into the socket buffer. Instead, only descriptors with information about the location and length of the data are appended to the socket buffer. The DMA engine passes data directly from the kernel buffer to the Network buffer, thus eliminating the remaining final CPU copy.

This is how zero copy help us in optimizing to copy data from disk to other network or disk.

Thanks for reading it, Hope you have learnt something new.

--

--