Quick revision
The short answer
The memory goes to RocksDB, which lives outside the Java heap: every store instance on every task has its own memtables, block cache and index and filter blocks, and windowed stores are split into segments that are separate RocksDB instances. 48 tasks with 3 stores, two of them windowed, add up to hundreds of RocksDB instances whose default budgets reach gigabytes. We bound it with a RocksDBConfigSetter that shares one block cache and write buffer manager across the JVM, size containers as heap plus the RocksDB budget plus overhead, and make that a platform standard with dashboards and load tests.
What to remember
In this answer, we saw that the pods are killed for native memory, not heap: every task's store instances, and every segment of a windowed store, are separate RocksDB databases with their own caches and memtables, about 340 of them on one pod. A shared block cache and WriteBufferManager set through a RocksDBConfigSetter bound that memory, the container is sized as heap plus RocksDB plus overhead plus headroom, state per pod is controlled through instances, standbys and retention, and a platform template, dashboards and load tests make it a standard.
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.
The memory goes to RocksDB, which lives outside the Java heap: every store instance on every task has its own memtables, block cache and index and filter blocks, and windowed stores are split into segments that are separate RocksDB instances. 48 tasks with 3 stores, two of them windowed, add up to hundreds of RocksDB instances whose default budgets reach gigabytes. We bound it with a RocksDBConfigSetter that shares one block cache and write buffer manager across the JVM, size containers as heap plus the RocksDB budget plus overhead, and make that a platform standard with dashboards and load tests.
Why this question is asked
OOMKilled pods with a calm heap graph are one of the most confusing failures in Kafka Streams, and in a platform with 25 applications it will keep happening until there is a standard. Interviewers ask this to see whether we understand where a Java process uses memory beyond the heap and can turn one fix into a rule for many teams. In this answer, we will count where the memory goes, bound RocksDB with a shared configuration, size the container properly, control state per pod, and define the platform standard.
1Why the Heap Graph Lies
Kubernetes kills a pod when its resident memory (RSS), everything the process uses, passes the container limit, 4 GiB here. The heap is only one part: there is also metaspace, thread stacks, direct buffers and, for Kafka Streams, RocksDB. RocksDB is a C++ library used through JNI, so all its memory is native memory that no JVM heap metric shows. A heap under 1.5 GB with -Xmx2g tells us nothing about the other 2.5 GiB.
2Counting the RocksDB Instances
Each task has its own instance of each store. A windowed store keeps its data in segments by time so that old windows can be dropped by deleting a whole segment, and each segment is a separate RocksDB database; typically a few segments are live per store. So one pod with 48 tasks holds 48 plain stores plus 48 x 2 windowed stores x about 3 segments, roughly 340 RocksDB instances.
Kafka Streams' default RocksDB settings give each instance its own block cache of 50 MB and up to 3 memtables (write buffers) of 16 MB each. Not every instance fills its budget, but the ceiling is close to 100 MB each, and even a modest fill of 10 to 20 MB per instance means 3.4 to 6.8 GB. That is how a 4 GiB pod with a 1.5 GB heap gets killed.
| Part | Default behaviour | Bounded standard |
|---|---|---|
| JVM heap | Up to -Xmx (2 GB) | 1.5 GB, sized from measurement |
| RocksDB block caches | 50 MB per instance, about 340 instances | One shared cache, e.g. 1 GB total |
| RocksDB memtables | Up to 3 x 16 MB per instance | WriteBufferManager inside the cache |
| Index and filter blocks | Outside the cache, unbounded | Counted against the shared cache |
| Metaspace, stacks, direct buffers | Hundreds of MB | Budgeted, about 500 MB |
3Bounding RocksDB With a Shared Configuration
Kafka Streams lets us customise RocksDB through a class named in rocksdb.config.setter. The standard approach is to create one LRU block cache and one WriteBufferManager as static objects shared by every store in the JVM, so the total is fixed regardless of how many stores exist.
Notice the close() method. Shared objects must not be closed when one store closes, but any RocksDB object created per store inside setConfig must be closed there, or it leaks native memory. Getting that wrong produces a slow leak that looks just like the original problem.
4Sizing the Container
With RocksDB bounded, the container size becomes a formula rather than a guess: heap plus RocksDB budget plus JVM overhead plus headroom. For this pod that might be 1.5 GB heap, 1 GiB RocksDB, about 0.5 GB for metaspace, thread stacks and direct buffers, and 0.5 to 1 GB of headroom, which fits a 4 GiB limit with -Xmx lowered to match. We watch RSS and the container's working set, not the heap.
If RSS still creeps up while every budget is respected, memory fragmentation in the default glibc allocator may be the cause; teams often fix it by running with jemalloc or by limiting malloc arenas (MALLOC_ARENA_MAX).
5Controlling State per Pod
A shared cache bounds memory but cannot make 48 tasks of state fast if the cache is too small for them. The number of tasks per pod follows the partition count divided by the number of instances, so adding instances spreads the stores. Standby replicas add memory and disk, because a standby is a full RocksDB instance too. Where the design allows, fewer, larger stores are better than many tiny ones, and window retention should be no longer than the grace period and queries need, since longer retention means more segments.
6Making It a Platform Standard
For 25 applications we turn this into defaults. The platform ships a RocksDBConfigSetter template with a budget property, a memory formula per task and store in the onboarding guide, a dashboard with RocksDB metrics such as block cache usage and memtable size (some require metrics.recording.level=DEBUG), and an alert on RSS approaching the limit. Before launch, every state-heavy application runs a load test with production-sized state and the real partition count, because memory problems only appear at full size.
- Shared RocksDBConfigSetter templateOne block cache and
WriteBufferManagerper JVM - Container sizing formulaHeap + RocksDB budget + overhead + headroom
- Dashboards and alertsRSS, block cache usage, memtables
- Pre-launch load testProduction-sized state and partitions
4 layers, from the top down. Step through them one at a time.
7Follow-up questions
When would in-memory stores be the better choice?
When the state is small, for example a few tens of megabytes per instance, and restore on every start is cheap. In-memory stores live on the heap, so they show up in heap metrics and are bounded by -Xmx, which makes memory easier to reason about, but they always restore fully from the changelog on start and they make garbage collection work harder as they grow.
Is the Kafka Streams record cache part of this problem?
No. The record cache, statestore.cache.max.bytes, is on the heap and bounded, 10 MB by default. The unbounded part is RocksDB's native memory.
Why not simply raise the limit to 8 GiB?
It might stop the kills for a while, but without a bound RocksDB memory grows with the number of tasks, stores and segments, so the next partition increase or new store brings the kills back. A bound turns memory into a number we can plan.
8Conclusion
In this answer, we saw that the pods are killed for native memory, not heap: every task's store instances, and every segment of a windowed store, are separate RocksDB databases with their own caches and memtables, about 340 of them on one pod. A shared block cache and WriteBufferManager set through a RocksDBConfigSetter bound that memory, the container is sized as heap plus RocksDB plus overhead plus headroom, state per pod is controlled through instances, standbys and retention, and a platform template, dashboards and load tests make it a standard.
Open with
The heap is not the problem; RocksDB is, and it lives off-heap. Each task has its own instance of each store, and windowed stores are split into time segments that are separate RocksDB databases, so 48 tasks with three stores, two windowed, means something like 340 RocksDB instances on one pod.
Then, if they want more
By default each has its own 50 MB block cache and up to three 16 MB memtables, plus index and filter blocks outside the cache, so a few gigabytes of native memory is easy, and Kubernetes kills the pod on RSS.
The fix is a RocksDBConfigSetter that creates one shared LRU block cache and one WriteBufferManager for the whole JVM, counts index and filter blocks against the cache, limits write buffers, and does not close the shared objects in close.
Then the container is sized as heap plus the RocksDB budget plus JVM overhead plus headroom, and we watch RSS rather than heap, with jemalloc if fragmentation shows up. State per pod depends on tasks per instance, standbys and window retention.
As a platform standard I would ship the config setter as a template with a memory formula, dashboards for RocksDB metrics, some of which need DEBUG recording level, and a load test with production-sized state before launch.
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 "One such pod hosts 48 tasks with 3 stores each, two of them windowed. Explain where the...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-streams-connect/330a8db045/