What does a cache hit cost?
A Java cache can find a record before the program can use it. Deciding when to stop the timer shaped my first Caffeine and Chronicle Map benchmark.
I started the first TailCache benchmark on Java 21 with a small question. Say I ask for record 42 and the cache already has it. That’s a cache hit. Where should I stop the timer? When the cache finds 42, or when my program has the bytes it asked for?
I chose the second. It’s the point at which I can actually do something with the answer. That choice matters for the two Java libraries I was testing. Caffeine can return an array it already holds in memory. Chronicle Map’s ordinary read finds the record outside Java’s heap and copies its contents into a new array. Stopping at “found it” would leave part of that read off the clock.
Which requests took longest?
Imagine 1,000 people making the same request. Almost everyone gets a quick answer. A few happen to arrive when Java pauses to reclaim memory. The average might look fine to me. It doesn’t tell those people much about their wait.
So I wanted to line up the times from fastest to slowest. The 99th percentile, or p99, is the time at or below which roughly 99 in 100 finish. At p99.9 you’re looking near the slowest one in a thousand. With very few requests, one unusually slow call can move that number a long way. I needed enough observations before I could use it to compare the caches.
Give record 42 a fair chance
I loaded both caches before timing and gave each enough room for every record. Caffeine can remove an entry when it reaches its size limit. Chronicle uses its expected entry count to reserve space. If one had already dropped record 42, we’d be comparing a successful lookup with a miss.
I also generated the list of record numbers in advance. Both libraries got the same list, and the time to pick the next number stayed out of the result. The first short run answered one modest question for me. Could I put a record in, read the right bytes back, and time it? Yes. How either cache would behave once people started changing those records was still wide open.