Quick revision
The short answer
Before agreeing to a custom 30 GB off-heap cache, we ask what problem the numbers really describe: the hit rate, the size and shape of the values, how often they change, and whether 30 GB is the working set or just everything. We then weigh cheaper options: a low-pause collector such as generational ZGC with a large on-heap cache, an existing off-heap cache library, or an external cache. Off-heap means serialising every value, which costs CPU and code, and whatever we build needs monitoring, a memory budget and a plan for the cold start after every restart.
What to remember
In this answer, we saw that a 30 GB off-heap cache is a solution that must be traced back to its problem. We ask about the hit rate at different sizes, the size and number of values, the update pattern and the measured pauses. A low-pause collector such as generational ZGC often removes the pauses with a configuration change. Off-heap storage needs serialisation on every read and write, so its CPU and code cost belongs in the decision, and if it is still the right answer we reuse an existing off-heap or external cache rather than writing one. Monitoring, a full memory budget and a warm-up plan for restarts complete the design.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Before agreeing to a custom 30 GB off-heap cache, we ask what problem the numbers really describe: the hit rate, the size and shape of the values, how often they change, and whether 30 GB is the working set or just everything. We then weigh cheaper options: a low-pause collector such as generational ZGC with a large on-heap cache, an existing off-heap cache library, or an external cache. Off-heap means serialising every value, which costs CPU and code, and whatever we build needs monitoring, a memory budget and a plan for the cold start after every restart.
Why this question is asked
When a team says GC pauses are hurting latency and proposes a 30 GB off-heap cache, they are usually reacting to a real pain. But a custom off-heap cache is one of the more expensive things a team can build and own: it brings serialisation, manual memory management and new failure modes. Interviewers ask this design question to see whether a senior engineer can turn a proposed solution back into a problem statement, compare options with numbers, and still say yes when the case is strong. In this answer, we will go through the questions we ask first, the alternative of a modern low-pause collector, the hidden cost of serialisation, the build-or-reuse choice, and the operational plan for whatever we choose.
1Start With the Questions, Not the Design
The first question is the hit rate and how it changes with size. If a 3 GB cache already gives a 95 percent hit rate and 30 GB gives 97 percent, the extra 27 GB buys two percentage points of hits, and we should ask what those misses cost. Caffeine and most cache libraries report hit rates, so the team can measure this on today's traffic before building anything.
The second is the shape of the values. A million values of 30 KB each behave very differently from 300 million values of 100 bytes. Small values mean the per-entry overhead of keys, hash tables and object headers dominates, and many tiny objects are exactly what makes an on-heap cache expensive for the GC. Large values are cheap to track and mostly cost bandwidth to copy.
The third is the update pattern. A cache that is loaded once at startup and read all day is a very different design from one where 20 percent of entries change every minute. Frequent updates mean frequent serialisation and frequent freeing of old off-heap blocks, which leads to fragmentation if the cache manages its own memory.
The last is the latency target itself. We ask for the actual p99 and p999 numbers, and for GC logs that show the pauses are the cause. Sometimes the pause is a full GC caused by a too-small heap, or allocation from an unrelated part of the service.
- What is the hit rate at 3, 10 and 30 GB?measure with today's cache statistics
- How big are values, and how many are there?small values mean many objects
- How often do entries change?loaded once, or constant churn
- What do GC logs show for p99 and p999?pause length, cause, frequency
- Does a low-pause GC meet the target?YesKeep the cache on the heapNoConsider off-heap or external
5 steps in order. Play them, or step through them one at a time.
2The First Alternative: A Low-Pause Collector
The original reason for off-heap caches was that collectors such as CMS and the early G1 had pause times that grew with the heap. That has changed. ZGC, generational since Java 21 and generational only since Java 24, does marking and relocation concurrently with the application, and its pauses stay well under a millisecond regardless of heap size, even at hundreds of gigabytes. Shenandoah offers similar concurrent behaviour.
Notice that this is a configuration change, not a project. The cost is CPU: concurrent collectors use background threads, so the service might need some more cores, and the heap needs headroom so collection can keep up with allocation. But a few extra cores are usually much cheaper than months of engineering on a custom cache. If the canary meets the p999 target, the conversation can end here.
3The Hidden Cost: Serialisation
Off-heap memory holds bytes, not objects. Every put must turn a Java object into bytes, and every get must turn bytes back into a new Java object. So an off-heap cache does not remove allocation; it moves it from cache time to read time.
At 50,000 reads per second, an extra 3 microseconds of decoding is 0.15 CPU seconds every second, which is fine. At 2 million reads per second it is six full cores. The team also has to write and maintain a codec for every cached type, keep it compatible when the type changes, and handle partial or corrupt entries. These costs belong in the proposal next to the GC savings.
4Build, Reuse or Go External
If the numbers still favour off-heap, we would almost never write a custom one. Existing libraries already solve memory allocation, eviction and concurrency. Ehcache 3 has an off-heap tier, Chronicle Map stores entries in off-heap or memory-mapped storage and can survive a restart when backed by a file, and other libraries cover similar needs. An external cache such as Redis or Memcached takes the data out of the JVM entirely, at the price of a network hop of a few hundred microseconds and a separate system to run.
| Option | Read cost | Main cost | Lost on restart? |
|---|---|---|---|
| On-heap cache + ZGC | ~50 ns, no copy | extra CPU for concurrent GC | yes |
| Existing off-heap library | 1-5 us, decode | codecs, tuning | yes, unless file-backed |
| Custom off-heap cache | 1-5 us, decode | months of work, bugs | yes |
| External cache (Redis) | 200-1000 us, network | another system, network | no |
For a latency-sensitive service where every read crosses the network, an external cache can defeat the purpose. For a service with many instances, though, it saves memory: ten instances each holding 30 GB is 300 GB of RAM, while a shared cluster might hold it once with a couple of replicas.
5The Operational Plan
Whatever we choose, we plan its operation up front. Monitoring must show the cache's size, hit rate, eviction rate and, for off-heap, its native memory use, which does not appear in heap metrics. The memory budget must add up: a 30 GB off-heap cache plus the heap plus JVM overhead must fit the container limit with room to spare, or the kernel will kill the process.
Restarts need the most thought. An in-process cache, on or off the heap, is empty after a deploy. If the cache absorbs 95 percent of database reads, a cold instance sends twenty times the normal load to the database until it warms up. We would roll deploys slowly, warm the cache from a snapshot or from the most popular keys before taking traffic, and protect the database with request coalescing so that a thousand misses for the same key cause one load, not a thousand.
6Follow-up questions
When is a custom off-heap cache really justified?
When we have measured that a low-pause collector cannot meet the target, existing libraries do not fit the access pattern, and the gain is worth a team owning it for years. Trading systems with strict microsecond budgets and very predictable data shapes are the classic example, and they usually have the specialists to maintain it.
Does ZGC need more memory than G1?
Usually some more headroom, because it collects concurrently and the application keeps allocating during a cycle. A common starting point is a heap noticeably larger than the live data set, then tuning with the GC logs. It is still far simpler than moving data off-heap.
How would we warm a cache after a restart?
Either load a snapshot of the hottest keys written periodically by a running instance, or replay the most frequent keys from access logs before the instance joins the load balancer. A readiness check that waits for a minimum hit rate on a warm-up set keeps traffic away until it is ready.
7Conclusion
In this answer, we saw that a 30 GB off-heap cache is a solution that must be traced back to its problem. We ask about the hit rate at different sizes, the size and number of values, the update pattern and the measured pauses. A low-pause collector such as generational ZGC often removes the pauses with a configuration change. Off-heap storage needs serialisation on every read and write, so its CPU and code cost belongs in the decision, and if it is still the right answer we reuse an existing off-heap or external cache rather than writing one. Monitoring, a full memory budget and a warm-up plan for restarts complete the design.
Open with
First I would ask questions to make sure 30 GB is the real need. What is the hit rate at 3, 10 and 30 GB with today's traffic?
Then, if they want more
How large are the values, and how many entries are there, because many small objects are what hurt the GC? How often do they change? And what do the GC logs actually show for p99 and p999, so we know pauses are the cause. Then I would try the cheapest alternative: a modern low-pause collector.
Generational ZGC keeps pauses well under a millisecond even with very large heaps, so a canary with ZGC and a bigger heap might solve the problem with no code.
If we still need off-heap, I would point out that every read has to copy and decode bytes into a new object, which costs CPU and means writing codecs for every type. So I would prefer an existing library with an off-heap tier, or an external cache like Redis if the network hop is acceptable, over writing our own.
Finally, I would plan operations: native memory metrics, a container budget that fits heap plus 30 GB plus overhead, and a warm-up strategy because an in-process cache is empty after every restart.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21, Java 24
- 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
- The Java Language Specification, Java SE 21, Java SE 21 API 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 "A team wants to build an off-heap cache of 30 GB in a latency-sensitive service to avoid...": https://scrivio-interview-library.netlify.app/java/foreign-memory-and-native/36116c0f34/