Core Java

Zero-Copy and the Page Cache: Why Kafka Is Fast, Using FileChannel.transferTo

Kafka moves enormous amounts of data, yet its brokers do surprisingly little work per byte. The secret is not clever Java code. It is the opposite: Kafka avoids touching the data at all and lets the operating system do the heavy lifting.

In this article, we follow a message from disk to network, explain the page cache and zero-copy, and use FileChannel.transferTo in Java. Then we measure what it really changes, including a result that surprised me.

Where the Time Goes in a Normal Send

Imagine a broker sending a log file to a consumer the usual way. First, the kernel reads the file into its page cache. Next, an application reads that data into its own buffer, which is one copy. Then the application writes it into a socket buffer, which is another copy. Finally, the network card sends it.

Arpit Bhayani’s short explanation counts four copies for this path and two with sendfile. In plain words, the middle copies move bytes that the program never needed to look at.

The Page Cache: A Free Memory Layer

The page cache is the part of RAM the operating system uses to keep recently used file data. Kafka relies on it instead of building its own cache. Confluent’s Kafka design notes on efficiency explain that combining the page cache with sendfile lets consumers that keep up be served from memory, so the disks see little or no read activity.

To see the effect, I read a 1 GiB file after dropping the page cache, and then again right away.

Figure 1. Time to read a 1 GiB file, in seconds. Data note: my own runs in a one-CPU sandbox with a virtual disk that the host probably caches too, so real disks will make the cold reads look worse.

The first cold read took 0.93 s, and the next two took about 0.42 s. In contrast, the warm reads took 0.18 s, 0.18 s and 0.26 s. As a result, the cached read was roughly twice as fast, without a single line of caching code.

Zero-Copy With transferTo

Kafka asks the operating system to move data straight from the page cache to the socket, using the sendfile system call. In Java, the same idea is exposed through FileChannel.transferTo. The FileChannel reference says many operating systems can move bytes directly from the filesystem cache to the target channel without actually copying them.

One detail matters in practice. The same documentation warns that a call may or may not transfer all requested bytes. Therefore, always call it in a loop. Here is a small, complete program that copies a file this way. It needs only Java 17 or newer, and it prints copied 8388608 bytes when it works.

cat > ZeroCopy.java <<'JAVA'
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.file.*;

public class ZeroCopy {
    public static void main(String[] args) throws IOException {
        Path src = Files.createTempFile("src", ".bin");
        Path dst = Files.createTempFile("dst", ".bin");
        Files.write(src, new byte[8 * 1024 * 1024]);

        try (FileChannel in = FileChannel.open(src);
             FileChannel out = FileChannel.open(dst,
                     StandardOpenOption.WRITE)) {
            long pos = 0, size = in.size();
            while (pos < size) {
                pos += in.transferTo(pos, size - pos, out);
            }
        }
        System.out.println("copied " + Files.size(dst) + " bytes");
        Files.delete(src);
        Files.delete(dst);
    }
}
JAVA
java ZeroCopy.java

I ran it on JDK 17, 21 and 25, and each printed the same line. Keep in mind one limit: Bhayani’s note adds that sendfile does not work with TLS, because encryption must happen in user space.

What the Numbers Say

Next, I compared three ways of sending the same 1 GiB cached file over a loopback socket: a plain stream with a heap array, a channel with a direct buffer, and transferTo. Each figure is the average of two rounds of ten transfers.

MethodWall time per GiBUser CPUKernel CPU
Stream + heap array0.60 s0.093 s0.50 s
Channel + direct buffer0.60 s0.030 s0.56 s
transferTo0.67 s0.016 s0.64 s
Figure 2. CPU seconds per GiB sent, split into user and kernel time. Data note: my own runs, one CPU, loopback; the receiving thread runs in the same process, so a small constant share of user time belongs to it in every row.

The CPU split matches the theory. With transferTo, user-space CPU time fell to about 0.016 s per GiB, roughly six times less than the heap-array version, because the Java program no longer handles the bytes. However, the total time did not improve here. In fact, transferTo was about 11% slower on this loopback test, because the kernel did more work.

This is an honest result, not a failure of the idea. Loopback has no network card and no real DMA, and the sender and receiver shared one CPU. I did not profile the kernel, so I cannot say exactly why. The lesson is that zero-copy removes user-space work, but the end-to-end win depends on your hardware and workload. Therefore, measure on a real network before you promise a speedup.

When Zero-Copy Pays Off

SituationHelps?Why
Streaming large files unchangedYesThe program never needs to touch the bytes
Many consumers reading recent dataYesThe page cache serves them from memory
TLS-encrypted connectionsNoEncryption happens in user space
Data changed per requestNoThe program must read it anyway
Loopback or tiny testsMaybe notNo NIC offload, so gains can vanish

Quick checklist

  • Leave caching to the page cache and give it free RAM.
  • Call transferTo in a loop until all bytes move.
  • Remember that TLS turns the zero-copy path off.
  • Measure user and kernel CPU, not just wall time.
  • Test on real network hardware before claiming a gain.

What We Have Learned

Kafka is fast partly because it avoids work. It leans on the operating system’s page cache, so caught-up consumers are served from memory, and it uses sendfile so data moves from the cache to the socket without passing through the application. In Java, FileChannel.transferTo exposes the same mechanism, and it should always be called in a loop. In our measurements, a cached read was about twice as fast as a cold one, and transferTo cut user-space CPU roughly sixfold, although loopback wall time did not improve. So, use zero-copy for large, unchanged data, keep TLS in mind, and always measure on your own hardware.

Eleftheria Drosopoulou

Eleftheria is an Experienced Business Analyst with a robust background in the computer software industry. Proficient in Computer Software Training, Digital Marketing, HTML Scripting, and Microsoft Office, they bring a wealth of technical skills to the table. Additionally, she has a love for writing articles on various tech subjects, showcasing a talent for translating complex concepts into accessible content.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Back to top button