Quick revision
The short answer
I would first compare the evening input rate with the group's processing rate, per partition, to see whether the consumers simply cannot keep up at peak or whether one hot partition is lagging because of key skew. Then I would profile where the time per record goes, such as slow downstream calls, database contention or GC pauses, and check for rebalances during the peak. With 12 partitions and 12 consumers, adding consumers does not help; the fix is to speed up processing, batch the work, or add partitions carefully because that changes key ordering.
What to remember
In this answer, we diagnosed evening lag on a group with 12 partitions and 12 consumers. We compared input and processing rates per partition, separated a hot-partition skew from a whole-group capacity problem, profiled processing time to find slow downstream calls, database contention or GC pauses, and checked for rebalances at peak. Because each partition already has a consumer, more consumers cannot help; the fixes are faster or batched processing, key-level parallelism, or more partitions with care for keyed ordering.
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.
I would first compare the evening input rate with the group's processing rate, per partition, to see whether the consumers simply cannot keep up at peak or whether one hot partition is lagging because of key skew. Then I would profile where the time per record goes, such as slow downstream calls, database contention or GC pauses, and check for rebalances during the peak. With 12 partitions and 12 consumers, adding consumers does not help; the fix is to speed up processing, batch the work, or add partitions carefully because that changes key ordering.
Why this question is asked
A lag pattern that grows every evening and recovers overnight is almost always a capacity story: the consumers are fast enough on average but not at peak. Still, the fix depends entirely on where the time goes, and the obvious idea, more consumers, does not work here. Interviewers ask this debugging question to see a structured investigation rather than a guess. In this answer, we will compare input and processing rates, check for skew, profile the processing time, rule out rebalances, and then look at the fixes and why adding consumers alone cannot help.
1Compare the Input Rate With the Processing Rate
Lag grows when records arrive faster than the group processes them, and shrinks when the opposite is true. So the first step is to put two numbers side by side for the evening hours: the rate at which records are written to the topic, per partition, and the rate at which the group processes them, per partition.
The write rate comes from broker metrics, such as the bytes and messages in per topic, or from the growth of the log end offsets. The processing rate comes from the consumers: Spring Kafka's listener timers count records processed and their duration, and the client's records-consumed-rate metric shows fetch throughput. If at 19:00 the topic receives 2,400 orders per second across 12 partitions, 200 per partition, and each consumer handles about 150 per second, every partition falls behind by 50 records a second, about 180,000 per hour. Overnight, input falls to 50 per partition and the consumers catch up. That pattern matches the symptom exactly.
| Time | Input / s | Processed / s | Lag change / hour |
|---|---|---|---|
| 14:00 | 90 | 90 (keeps up) | 0 |
| 19:00 | 200 | 150 (at capacity) | +180,000 |
| 22:00 | 120 | 150 | -108,000 |
| 03:00 | 50 | 150 | -360,000 (catching up) |
2Look for Skew: One Partition or All of Them?
Before concluding that the group lacks capacity, we look at lag per partition. If all 12 partitions lag by similar amounts, the load is spread evenly and the group as a whole is too slow at peak. If partition 7 lags by 400,000 records while the others lag by a few thousand, the problem is key distribution: some keys, such as a big merchant or a bulk importer, produce much more traffic in the evening, and they all hash to one partition.
Skew changes the fix. Overall capacity problems are solved by faster processing or more partitions. A hot partition is solved by reducing work for that key, splitting the key, or processing that partition with key-level parallelism, while adding partitions may not help at all if one key dominates.
3Profile Where the Time Goes
Next we find out why each consumer processes only 150 records a second. At 150 per second on one thread, each record takes about 6.7 milliseconds. That is far more than the CPU work of handling an order, so most of the time is probably waiting. Tracing or timers around each step of the listener usually show it quickly.
Typical findings are a downstream HTTP call, such as a pricing or fraud service, whose latency rises from 2 ms to 8 ms in the evening because that service is busy too; database contention, where many consumers update the same rows or a table lock slows writes at peak; or GC pauses, visible in JVM metrics, when the evening batch sizes push the heap. Each points to a different fix, and only measurement can tell them apart.
In this example, the pricing call dominates. The consumers are not slow; they are waiting on another service, one record at a time.
4Check for Rebalances During the Peak
Rebalances pause consumption. While the group reassigns partitions, consumers stop fetching, and records keep arriving. A few rebalances an evening can add a lot of lag, and they are easy to miss if we only look at throughput.
The common cause at peak is slow processing itself: if a poll returns 500 records and each takes 6.7 ms, one batch takes over 3 seconds, fine on its own, but if a downstream call starts timing out at 5 seconds, a batch can exceed max.poll.interval.ms, 5 minutes by default, and the consumer is removed from the group. Deployments or autoscaling events scheduled in the evening are another cause. The consumer logs, the group coordinator's logs and the rebalance metrics, such as rebalance-rate-per-hour, show whether this is happening.
- Compare input and processing rate per partitionat peak and off-peak
- One partition far behind the rest?YesSkew: look at hot keysNoCapacity: whole group too slow
- Profile time per recorddownstream calls, DB, GC
- Rebalances during the peak?YesFix poll interval, batch size, deploy timingNoFocus on processing speed
- Apply the fixfaster processing, batching, or more partitions
5 steps in order. Play them, or step through them one at a time.
5Fixes, and Why More Consumers Do Not Help
With 12 partitions and 12 consumers, every partition already has its own consumer. Kafka assigns each partition to exactly one consumer in a group, so a 13th consumer would sit idle. More consumers only help when there are more partitions than consumers.
So the real fixes act on processing time. We can make the slow step faster or less frequent: cache the pricing quotes that repeat, call the pricing service in batches, or get its owners to scale it for the evening. We can batch our own work: a batch listener that writes 100 orders in one database round trip instead of 100. We can overlap waiting: process records with key-level parallelism inside each partition, so that many pricing calls are in flight at once while per-order ordering is kept. And we can increase the partition count, say from 12 to 24 with 24 consumers, which doubles parallelism, but because keys hash to partitions modulo the count, it remaps keys and can break per-key order during the change, so it needs planning.
Finally, if the evening peak is predictable and the lag recovers well before morning, it may even be acceptable. That is a business question: if orders must be processed within two minutes, it is a bug; if overnight is fine, it is a capacity plan.
6Follow-up questions
Would increasing max.poll.records help?
Only indirectly. Larger polls reduce per-poll overhead, which matters little when the time goes to downstream calls. It helps if combined with a batch listener that actually processes records in bulk, for example one database write per batch.
How do you make the consumer scale automatically in the evening?
Autoscaling on lag works only up to the partition count; beyond 12 instances there is nothing to assign. So autoscaling needs enough partitions planned in advance, and it should scale on time lag or a lag trend with some delay, otherwise the rebalances caused by scaling add to the lag.
What if the lag is caused by GC pauses?
Look at heap usage and pause times in the evening. Large batches of big records, or caches growing with traffic, can push the heap. Reducing max.poll.records, fixing the memory use, or moving to a low-pause collector such as ZGC or Generational Shenandoah usually helps.
7Conclusion
In this answer, we diagnosed evening lag on a group with 12 partitions and 12 consumers. We compared input and processing rates per partition, separated a hot-partition skew from a whole-group capacity problem, profiled processing time to find slow downstream calls, database contention or GC pauses, and checked for rebalances at peak. Because each partition already has a consumer, more consumers cannot help; the fixes are faster or batched processing, key-level parallelism, or more partitions with care for keyed ordering.
Open with
A pattern that grows every evening and recovers overnight tells me the consumers keep up on average but not at peak, so I'd confirm it with numbers. First I'd compare the input rate per partition, from broker metrics or log end offsets, with the processing rate per partition from Spring Kafka's listener timers.
Then, if they want more
Then I'd look at lag per partition: if all twelve lag similarly it's a capacity problem, but if one partition is far behind it's key skew, a hot key hashing to one partition, and the fix is different.
Next I'd profile where the time per record goes, because at peak it's usually waiting rather than CPU: a downstream call that gets slower in the evening, database contention, or GC pauses. I'd also check for rebalances during the peak, since slow batches can exceed max.poll.interval.ms and each rebalance pauses consumption.
As for fixes, adding consumers won't help, because there are already twelve consumers for twelve partitions, and an extra one would be idle. So I'd speed up processing, for example caching or batching the downstream calls, use a batch listener to write to the database in bulk, or add key-ordered parallelism inside a partition.
Increasing partitions is an option too, but it remaps keys and can break per-key ordering during the change, so I'd plan it as a migration.
About this answer
- Last updated
- 12 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, Spring for Apache Kafka reference
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 "Consumer lag for an order-processing group grows steadily every evening and recovers...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/3f89c30e58/