TailCache · Part 2 of 6

When is an off-heap Java cache worth the copy?

Caffeine keeps records in memory that Java manages. Chronicle Map stores their bytes outside it. Reading one record a thousand times helped me see the trade.

One detail of our hackathon design kept coming back to me. Sharing a record saved space. But every reader still had to turn the stored bytes into something a Java program could use. Who was paying for that, and how often?

Take a program that looks up the same record a thousand times. With Caffeine, a Java cache library, the record can live on the heap, the part of memory where Java keeps ordinary objects. In our example the record is an array of bytes. Caffeine can hand the program the same array on every read.

Chronicle Map, the library we used for the prototype, can store those bytes outside the heap. That’s what off-heap means. With its ordinary read method, Chronicle makes a fresh Java array and copies the bytes into it. A thousand reads of one unchanged record can mean a thousand copies.

So why pay for the copy?

Java has a garbage collector that reclaims memory when objects are no longer needed. A cache deliberately keeps objects around. As the rest of the program fills the heap, a cleanup can pause useful work. If your request arrives during that pause, you wait before anyone even looks up your record.

I could now see a reason to choose either design. I wanted to put a number on the trade. How much data does the program have to keep alive before shorter pauses pay for copying on each read? And if I make each record sixteen times bigger, do I still get the same answer?