saurav omar
2 min readJan 16, 2019

Zero-Copy in Linux:

Most of the people already heard of Zero-Copy in Linux but very fewer people understand how it works because underneath it requires some operating system concepts.

Lets first understand how sending of data to the network works.

As we can see from the diagram:

  1. Read system call causes a context switch from user mode to kernel mode. DMA engine reads the file contents from the disk and stores them into a kernel address space buffer.
  2. Data is copied from the kernel buffer into the user buffer, and the read system call caused a context switch from kernel back to user mode and return.
  3. To write the data to the socket, data is copied again from user context to kernel Context (Socket Buffer) and then sends to the network interface.

As we can see, it’s redundant to copy data between the Kernel Context and the Application Context. Using Zero Copy we can copy data directly from the Kernel Context to the Kernel Context.

So in Zero Copy we can bypass userspace entirely using the sendfile system call, which will copy the data directly from the to the Socket buffer. This turns out to be an important optimization which saves lots of CPU cycles, memory bandwidth.

Apache Kafka Uses Zero Copy for fast data transfers.

saurav omar
saurav omar

Written by saurav omar

Geek and Always ready to give more than 100%

No responses yet