The 30 second answer
When CPU and the database look fine, the consumer is usually waiting rather than working. Common causes are a hot partition from a skewed key, rebalances because processing takes longer than the max poll interval, slow synchronous calls to another service, or tiny fetch batches. I check lag per partition with the consumer groups tool and the consumer lag metrics before changing anything.
If nothing is busy, the consumer is waiting, not working. Check: fewer partitions than consumers you need (one partition goes to only one consumer in a group); a hot partition from a skewed key; rebalances caused by processing slower than max.poll.interval.ms; slow synchronous calls to another service; or small fetch batches. I check lag per partition with kafka-consumer-groups.sh --describe and the consumer's lag metrics. Follow-up: How do you scale past the partition count? (Add partitions, which changes key placement, or process records in parallel inside the consumer with per-key ordering.) Editor's note: the list of causes comes from experience; the tools and config names come from the Kafka docs.
Alternate interview scenarios
"Kafka consumer lag keeps increasing. What would you investigate?"
This version does not say that the CPU and the database are fine, so I first find which side changed. Either producers are writing faster, or consumers are processing slower. I compare the producer's incoming rate with the consumer's records-consumed rate. If lag grows on every partition, it is a throughput problem. I check consumer CPU, database latency and downstream calls, then the waiting causes above. If lag grows on one partition, it is a skewed key or a stuck record, such as a message that fails and is retried forever. I check records-lag-max and the rebalance logs. A stuck record needs a dead-letter topic, not more consumers.
"Design a system for millions of events per hour with reliable processing and controlled backpressure."
A million events per hour is only about 280 per second, so throughput is rarely the hard part. The hard part is reliability and handling bursts. Kafka consumers pull, so backpressure comes for free: a slow consumer simply falls behind, and lag is the signal. I bound the work per poll with max.poll.records and keep processing within max.poll.interval.ms. When a downstream is saturated, pause() the partitions and resume() them later instead of buffering without limit in memory. Reliability comes from committing offsets after processing, idempotent handlers, a retry topic and a dead-letter topic for records that keep failing. I scale with partitions keyed by entity and alert on lag and its growth rate.
Sources (4)
- Kafka consumer configs (max.poll.interval.ms, fetch settings)kafka.apache.org
- Kafka: Basic operations (consumer groups)kafka.apache.org
- Kafka: Monitoring (consumer fetch metrics, records-lag-max)kafka.apache.org
- KafkaConsumer Javadoc (flow control with pause and resume)kafka.apache.org