Scrivio Interview library
ConceptFoundations7 min read · updated 13 November 2026

Kafka brokers often run with a 6 GB JVM heap on machines with 64 GB of RAM and still serve hundreds of MB per second.

Explain the design choices that make this work.

Kafka is fast with a small heap because it lets the operating system do the caching. Brokers append to the end of log files and read them in order, keep data in the OS page cache rather than on the JVM heap, send it to consumers with zero-copy, and move records in compressed batches. The rest of the machine's memory is left to the page cache on purpose.

Why this question is asked

A 6 GB heap on a 64 GB machine looks like a mistake the first time we see it, and many teams have 'fixed' it by giving the broker a 32 GB heap and watching throughput fall. Interviewers ask this question to see whether we understand where Kafka's speed actually comes from, because that understanding drives every sizing and tuning decision later. In this answer, we will recap what a Kafka log looks like on disk, then go through the four design choices one by one: sequential I/O, the page cache, zero-copy transfers and batching. We will finish with why the leftover memory is deliberately not given to the JVM.

1A Quick Recap of the Kafka Log on Disk

Each partition of a topic is a log, an ordered sequence of records that only grows at the end. On disk, a partition is a directory, and the log inside it is split into segment files of about 1 GB each (the segment.bytes setting). Only the newest segment, the active segment, receives writes. Older segments are closed and only read or eventually deleted.

Next to each segment sit two small index files: an offset index that maps record offsets to byte positions in the segment, and a time index that maps timestamps to offsets. A consumer asks for 'partition 3 from offset 81,200', the broker looks up the byte position in the index, and from there it reads forward through the file.

So the broker never updates a record in place and never jumps around inside a file to find something. That simple layout is what makes the rest of the design possible.

2Choice 1: Append and Read Sequentially

Writes always go to the end of the active segment, and reads usually start at some offset and continue forward. Both are sequential I/O. Disks love sequential access: a spinning disk that manages perhaps 150 random reads per second can stream well over 100 MB per second in order, because the head does not have to move. SSDs are also faster for large sequential operations than for many small random ones, and the operating system can read ahead, loading the next blocks before anyone asks for them.

Notice what Kafka avoids: no B-tree to update, no per-record index on the write path, no random seeks. The cost of storing a record is close to the cost of copying its bytes to the end of a file.

3Choice 2: Let the OS Page Cache Hold the Data

The page cache is memory the operating system uses to keep recently read or written file blocks. When a program writes to a file, Linux copies the bytes into the page cache and returns immediately; the data is flushed to disk in the background. When a program reads a file, Linux first looks in the page cache and only goes to disk on a miss.

Kafka writes every record through the page cache and does not keep its own copy of the data on the heap. A consumer that keeps up with producers asks for data that was written a few seconds ago, and those pages are still in memory, so the read never touches the disk. In a healthy cluster, disk read IOPS are close to zero even under heavy consumer load.

This choice has a second benefit. A JVM with a 30 GB heap full of cached messages would spend a lot of effort in garbage collection, the process that finds and frees unused objects, and long pauses would stall requests. With a 6 GB heap that holds only metadata, request objects and buffers, GC pauses stay short and predictable. The page cache also survives a broker restart, because it belongs to the kernel, so a restarted broker is warm immediately.

Step throughWhere a Kafka broker's memory goes on a 64 GB machine
  1. JVM heap (about 6 GB)Requests, metadata, buffers; small so GC pauses stay short
  2. OS and other processes (a few GB)Kernel, agents, network buffers
  3. Page cache (about 50 GB)Recent log segments; consumers that keep up read from here
  4. DisksAll segments; read only when data is no longer cached

4 layers, from the top down. Step through them one at a time.

The broker uses a fraction of the RAM directly. Most of it works for Kafka through the kernel.

4Choice 3: Zero-Copy From Page Cache to Socket

A normal program that serves a file reads it into its own memory and then writes it to a socket. That is four copies of the data and several switches between user space and the kernel. For plaintext connections, Kafka uses Java's FileChannel.transferTo(), which on Linux becomes the sendfile system call. The kernel moves bytes from the page cache straight to the network socket, and the data never enters the JVM at all.

Java
// What the broker does for a plaintext fetch, simplified
FileChannel segment = FileChannel.open(path, StandardOpenOption.READ);
long sent = segment.transferTo(position, length, socketChannel); // sendfile: page cache -> socket
// The JVM never sees the record bytes, so no heap allocation and no GC for them

This only works because the broker stores records in exactly the format it sends them in. A batch written by a producer is stored as is and later sent to consumers as is, so the broker has nothing to change on the way out. There is one catch to remember: with TLS, the bytes must be encrypted in the JVM, so zero-copy no longer applies. That is a separate interview question of its own.

5Choice 4: Batches Everywhere, Compressed End to End

Producers group records for the same partition into batches before sending, the broker writes each batch as one unit, and consumers fetch many batches per request. A request has a fixed cost (a network round trip, a thread hand-off, a few system calls), and batching spreads that cost over hundreds of records instead of paying it for each one.

Compression works on whole batches too. A producer compresses a batch with lz4 or zstd, the broker stores the compressed batch without unpacking it (as long as the topic's compression.type is left at producer), and the consumer decompresses it. So compression saves network on the way in, disk space on the broker, page cache space, and network on the way out, all at once.

6Why the Spare Memory Is Left Alone

Put together, these choices mean the broker's real cache is the page cache, and its size is simply all the memory nobody else uses. On a 64 GB machine with a 6 GB heap, around 50 GB can hold recent segments. If we raise the heap to 32 GB, we shrink the page cache to around 25 GB, consumers that were served from memory start reading from disk, and GC pauses grow. Throughput usually gets worse, not better.

That is why the common recommendation is a heap of roughly 4 to 8 GB, and why memory upgrades on Kafka brokers help through the page cache, not through the JVM.

7Follow-up questions

Does writing through the page cache mean Kafka can lose acknowledged data?

A single broker can lose data that was in its page cache but not yet flushed if the whole machine loses power. Kafka does not rely on fsync for durability by default. It relies on replication instead: with acks=all, a write is acknowledged only after all in-sync replicas have it, so losing one machine's unflushed pages does not lose the record. Settings such as flush.messages exist, but forcing frequent fsync costs a lot of throughput and is rarely needed when replication factor 3 and min.insync.replicas=2 are used.

What happens when a consumer falls far behind?

Its data is no longer in the page cache, so the broker has to read old segments from disk. Those reads are still sequential and still use zero-copy, but they cost disk I/O and they pull old pages into the cache, which can push out recent pages that up-to-date consumers need. A large backfill can therefore slow down everyone on the broker, which is why heavy historical reads are often throttled with quotas or served from tiered storage.

8Conclusion

In this answer, we saw that Kafka's speed comes from a handful of choices that work together: an append-only log that is read sequentially, the OS page cache instead of a heap cache, zero-copy transfers from that cache to the network on plaintext connections, and batching with compression from producer to consumer. The small heap is not an oversight. It keeps GC pauses short and leaves most of the machine's memory for the page cache, where it serves consumers directly.

What to say in the interviewAbout 90 seconds aloud

Open with

Kafka is fast with a small heap because it pushes most of the work onto the operating system. Each partition is an append-only log split into segment files, so writes go to the end of a file and reads usually move forward through it.

Then, if they want more

That is sequential I/O, which disks and SSDs handle far better than random access. Kafka does not cache records on the JVM heap; it writes through the OS page cache, and consumers that keep up read data that is still in memory, so in a healthy cluster disk reads are close to zero.

Because the heap only holds requests and metadata, around 6 GB is enough and garbage collection pauses stay short. On plaintext connections the broker serves fetches with FileChannel.transferTo, which is sendfile on Linux, so bytes go from the page cache to the socket without passing through the JVM.

That works because the broker keeps batches in the same format the producer sent and the consumer receives. Batching spreads per-request costs, and compression is per batch, so a compressed batch stays compressed on disk, in the cache and on the wire. Finally, the remaining 50-odd GB is left for the page cache on purpose.

If someone raises the heap to 32 GB, the cache shrinks, consumers start hitting disk and GC gets worse, so throughput usually falls.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain the design choices that make this work.": https://scrivio-interview-library.netlify.app/spring-boot/kafka-internals/773dd87c1c/