DEV Community

Dakshin G
Dakshin G

Posted on

How Kafka and Nginx Move Data Faster: The Magic of Zero Copy

Ever wonder how tools like Kafka, Nginx, Netty manage to saturate a 10Gbps network link while barely breaking a sweat on CPU usage?

It’s not just efficient indexing. It’s because they understand a fundamental truth: The fastest way to move data is to never touch it.

The Slow Way: Traditional Copy

In a standard Java application, moving a file to a socket looks like this:

  1. Disk → Kernel Buffer: The OS reads data from the disk.
  2. Kernel Buffer → User Buffer: The OS copies data into your application's memory (the byte[] array).
  3. User Buffer → Socket Buffer: Your app writes that array back to the OS.
  4. Socket Buffer → NIC: The OS moves it to the network card.

4 context switches

Each time data crosses the boundary between Kernel Space and User Space, the CPU has to stop what it's doing and perform a "context switch." For a high-volume streaming tool, these switches are death by a thousand cuts.

The Fast Way: Zero Copy

Streaming tools bypass the "User Buffer" entirely. Using Java NIO's FileChannel.transferTo(), the application simply tells the OS: "Take the data from this file descriptor and send it directly to this socket descriptor."

The data stays in Kernel Space the entire time. This is fast because -

  • Zero Redundancy: Data is never duplicated in the application's heap.
  • Reduced Context Switching: We go from 4 switches down to 2.
  • DMA (Direct Memory Access): The hardware (NIC) can often pull data directly from the kernel buffer, leaving the CPU completely free to handle other requests.

Zero Copy Flow

Top comments (0)