Quick revision
The short answer
Fetched data waits in the consumer's buffer until poll() returns it, and the consumer keeps fetching for every partition that has nothing buffered, so with a backlog the buffer can fill with up to one full fetch per partition: 400 partitions times 10 MB is about 4 GB of compressed data, before decompression and deserialised objects multiply it again, far beyond a 2 GB heap. The fix is to bring max.partition.fetch.bytes back to about 1 MB, keep fetch.max.bytes bounded, lower max.poll.records if records are large, spread the partitions over more consumers, and size the heap from measured numbers.
What to remember
In this answer, we saw that fetched data waits in the consumer's buffer until poll() returns it, and that during a backlog the consumer keeps fetching full responses for every partition with nothing buffered, so 400 partitions at 10 MB each can mean about 4 GB of compressed data. Decompression and deserialisation multiply it further, so a 2 GB heap runs out. Returning max.partition.fetch.bytes to about 1 MB, keeping fetch.max.bytes bounded, lowering max.poll.records for large records and spreading partitions over more consumers fix it, and the heap is sized from measurement under a real backlog.
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.
Fetched data waits in the consumer's buffer until poll() returns it, and the consumer keeps fetching for every partition that has nothing buffered, so with a backlog the buffer can fill with up to one full fetch per partition: 400 partitions times 10 MB is about 4 GB of compressed data, before decompression and deserialised objects multiply it again, far beyond a 2 GB heap. The fix is to bring max.partition.fetch.bytes back to about 1 MB, keep fetch.max.bytes bounded, lower max.poll.records if records are large, spread the partitions over more consumers, and size the heap from measured numbers.
Why this question is asked
An OutOfMemoryError that appears only after a backlog builds up is a strong hint that something scales with how much data is available rather than with our traffic. Interviewers ask this to see whether we understand that a consumer's memory is driven by its fetch settings and partition count, not by max.poll.records alone. In this answer, we will recap where fetched data lives, work out how much memory this consumer can hold, add the hidden multipliers, apply the fixes and finish with a sizing method.
1A Quick Recap: Where Fetched Data Lives
The consumer sends fetch requests to the brokers that lead its partitions and puts each response into an internal buffer. poll() takes up to max.poll.records records from that buffer and returns them. Until poll() takes them, the fetched bytes sit on the heap.
Two limits shape each fetch: max.partition.fetch.bytes caps the data per partition in a response (1 MB by default, 10 MB here), and fetch.max.bytes caps a whole response (50 MB by default). And a third rule matters most here: the consumer sends a new fetch for any of its partitions that has no data waiting in the buffer, one request at a time per broker.
2Why a Backlog Fills the Buffer
When the consumer is caught up, each fetch returns only the few new records written since the last one, so responses are small and the buffer stays nearly empty. With a backlog, every partition has megabytes of unread data, so every fetch comes back full: up to the per-partition limit for each partition in it, up to the response limit overall.
Now follow the consumer for a moment. Our code is slow, so the buffer drains slowly. Meanwhile, the consumer keeps sending fetches for the partitions that have nothing buffered yet. Response by response, it ends up holding a full fetch for almost every one of its 400 partitions. The 50 MB fetch.max.bytes limit applies to each response, not to the buffer as a whole, so it does not stop this.
7 messages in order. Play them, or step through them one at a time.
Each response obeys fetch.max.bytes, but the buffer keeps growing across responses until every partition holds one.
Notice that raising max.partition.fetch.bytes for throughput multiplied the worst case by ten, and the worst case only appears when there is a backlog, which is exactly when the consumer is under the most pressure. That is why the service ran fine for weeks and crashed during the incident.
3The Hidden Multipliers
The bytes in the buffer are the compressed batches as stored on the broker. When the consumer iterates over a batch, it decompresses it; with typical compression ratios of 3x to 5x for JSON-like data, each batch being read briefly takes several times its stored size.
Then deserialisation turns bytes into objects. A 1 KB JSON record can become a Java object graph of 3 KB to 5 KB, with strings, maps and boxed numbers. With max.poll.records=500 and large records, the batch our code is holding can itself be tens of megabytes. Add the application's own needs, such as caches, HTTP clients and the batch it is writing to its sink, and a 2 GB heap has very little room for a multi-gigabyte buffer.
4The Fixes
First, bring max.partition.fetch.bytes back to about 1 MB. With 400 partitions, 1 MB per partition already allows large, efficient fetches, and the worst-case buffer drops tenfold. Keep fetch.max.bytes at its default or lower, which bounds each response. If individual records are large, lower max.poll.records so the batch of deserialised objects stays small.
Second, spread the partitions. 400 partitions on one consumer is a lot of buffer to hold and a lot of work for one thread. Four consumers with 100 partitions each have a quarter of the worst case each, and also more processing capacity, which is what lets the backlog drain in the first place.
| Setup | Partitions per consumer | Per-partition limit | Worst-case buffer |
|---|---|---|---|
| Today | 400 | 10 MB | about 4 GB |
| Default limit | 400 | 1 MB | about 400 MB |
| Default limit, 4 consumers | 100 | 1 MB | about 100 MB |
5Sizing the Heap From Measurement
Rather than guess, we estimate and then measure under a realistic backlog. The estimate is the fetched data we may buffer (partitions times per-partition limit, or, for a consumer with few partitions per broker, the number of brokers times fetch.max.bytes), times the decompression ratio for the batch being read, plus max.poll.records times the size of one deserialised record, plus the application's own working set. Then we run the consumer in a test environment against a deliberately built backlog, watch heap usage after garbage collection, and set the heap with comfortable headroom above the peak.
The consumer's metrics help: bytes-consumed-rate and fetch-size-avg show how big fetches really are, and heap usage after collection shows the true peak, which is the number the heap must cover.
6Follow-up questions
Why did fetch.max.bytes not protect us?
Because it limits a single fetch response, not the buffer. The consumer can hold several completed responses at once, from different brokers and from successive fetches for different partitions, so the total buffered can be many times fetch.max.bytes.
Would a bigger heap be enough?
It would stop this crash, but the memory needed still grows with partitions times the per-partition limit, so the next increase in partitions brings it back, and a very large heap brings long garbage-collection pauses. Fixing the settings and spreading partitions addresses the cause.
7Conclusion
In this answer, we saw that fetched data waits in the consumer's buffer until poll() returns it, and that during a backlog the consumer keeps fetching full responses for every partition with nothing buffered, so 400 partitions at 10 MB each can mean about 4 GB of compressed data. Decompression and deserialisation multiply it further, so a 2 GB heap runs out. Returning max.partition.fetch.bytes to about 1 MB, keeping fetch.max.bytes bounded, lowering max.poll.records for large records and spreading partitions over more consumers fix it, and the heap is sized from measurement under a real backlog.
Open with
Fetched data sits in the consumer's buffer until poll hands it to my code, and the consumer keeps sending fetches for any partition that has nothing buffered. When the consumer is caught up, fetches are tiny, so nobody notices.
Then, if they want more
But with a backlog every fetch comes back full, and since our processing is slow, the buffer ends up holding a full fetch for nearly every partition. fetch.max.bytes, 50 megabytes, limits each response, not the buffer. So the worst case is roughly partitions times max.partition.fetch.bytes: 400 times 10 megabytes is about 4 gigabytes, and that is compressed data.
Decompressing a batch multiplies it by maybe three to five, and deserialised Java objects are several times bigger than their bytes. On a 2 gigabyte heap that is an OutOfMemoryError exactly when the backlog appears.
I would put max.partition.fetch.bytes back to about one megabyte, which still gives big efficient fetches with 400 partitions and cuts the worst case to about 400 megabytes, keep fetch.max.bytes bounded, lower max.poll.records if records are large, and run more consumers so each holds fewer partitions, say four with 100 each.
Then I would size the heap from measurement: buffered fetch data, plus decompression and object overhead, plus the application, tested against a real backlog.
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 "Explain how fetch settings drive consumer memory and how you would size them.": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/c295abdbd2/