Video in progress
One JVM with a 128 GB heap or eight JVMs with 16 GB each: which is better, and what do you base the decision on?
The answer depends on the nature of the data: a big shared in-memory state (a cache, an index) may call for one big JVM, while for stateless services many smaller instances are more stable and easier to manage.
📖 Answer
In one big heap the in-memory data lives in one place and isn't duplicated; the cost of the JIT, metaspace and code cache is paid once; and no coordination between nodes is needed.
But there are many risks too:
- if one instance dies, a large part of the capacity disappears at once;
- restart and warm-up are long (refilling the cache takes hours);
- in G1, old gen collection and remembered sets are big; ZGC works well, but needs a CPU and memory reserve;
- the heap dump is too big to analyze;
- above 32 GB compressed oops are off, so references grow to 8 bytes.
With many small instances the blast radius is small, a rolling deploy is easy, and GC problems stay local. The cost is the overhead of each one and the problem of splitting state (sharding, consistent hashing, replication).
This is how I decide. For a stateless API it is almost always horizontal, with a 2-8 GB heap. If there is a big cache inside, I first ask: does this data have to be inside the JVM? Often the heap shrinks with Redis or a separate cache cluster. If in-memory computation is truly needed (for example real-time risk calculation, a search index), a big heap + ZGC + off-heap structures are justified. But a JVM like that must have its own runbook, its own monitoring and a recovery plan.
Sign up to mark studied questions and save your progress.