Prompt
Kafka messages not being consumed fast enough, how do I troubleshoot this?
Latest observation
Here’s a practical way to troubleshoot Kafka consumers that are falling behind.
1) First identify where the lag is coming from
Check consumer lag by group, topic, and partition.
Useful commands:
kafka-consumer-groups.sh --bootstrap-server <broker:9092> \
--describe --group <group-name>
Look for:
- Large and growing lag on specific partitions
- Uneven lag where only a few partitions are behind
- One consumer instance owning too many partitions
If lag is only on some partitions, the problem may be key skew or hot partitions.
2) Check consumer health and throughput
Ask:
- Is the consumer actually running continuously?
- Is it restarting/crashing/rebalancing often?
- Is it processing messages slower than they arrive?
Check app logs for:
- Rebalance events
- Commit failures
- Deserialization errors
- Timeouts
- GC pauses / memory pressure
- Retries or downstream dependency failures
If the consumer is healthy but slow, the bottleneck is usually:
- message processing logic
- downstream database/API
- too little parallelism
- fetch/commit settings
- CPU/memory limits
3) Verify partition count vs consumer parallelism
Kafka parallelism is bounded by partition count.
Rules of thumb:
- Max active consumers in a group = number of partitions
- If you have 3 partitions and 10 consumers, only 3 are useful
- If you have 100 partitions but 1 consumer, that consumer may be overwhelmed
Check:
- Does the topic have enough partitions?
- Is the consumer group scaled to match the partition count?
If needed:
- Increase partitions for more parallelism
- Scale consumer instances up to partition count
4) Look for uneven partition distribution
One slow partition can make lag look bad even if others are fine.
Causes:
- Bad key choice causing hot partitions
- One downstream record taking much longer than others
- Large messages concentrated on one partition
Check partition lag individually and compare:
- Record count per partition
- Message size per partition
- Processing time per partition
If key skew is the issue:
- Use a better partitioning key
- Salt hot keys
- Revisit message distribution strategy
5) Measure processing time inside the consumer
Separate:
- poll time
- business logic time
- commit time
- retry time
If your consumer polls quickly but processes slowly, the issue is in your application logic.
Add metrics for:
- time from
poll()to completion - per-record processing latency
- batch processing latency
- external dependency latency
- commit latency
This helps answer whether the bottleneck is Kafka or your code.
6) Check consumer configuration
Important configs that affect throughput:
Fetching
max.poll.records
Increase if you want larger batches, but only if processing can keep up.fetch.min.bytes/fetch.max.wait.ms
Can help batch more data efficiently.
Session and polling
max.poll.interval.ms
If processing takes too long, consumer may be kicked out of the group.session.timeout.ms/heartbeat.interval.ms
Too aggressive values can cause rebalances.
Offsets
enable.auto.commit=falsefor manual control- Commit in batches rather than after every message if safe
Deserialization
- Expensive deserialization can limit throughput
- Large payloads may cause memory pressure
If you increase max.poll.records, make sure processing and commit strategy can handle it.
7) Check downstream dependencies
Often Kafka is not the bottleneck. The consumer is waiting on:
- database writes
- external HTTP APIs
- cache lookups
- file I/O
- rate limiting
- lock contention
Signs:
- High latency in one dependency
- Retries/backoff increasing
- Thread pool saturation
- Connection pool exhaustion
Fixes:
- batch writes
- async I/O
- caching
- increase pool sizes carefully
- circuit breakers / timeouts
- isolate slow dependencies
8) Inspect broker-side metrics too
On the broker, check:
- Under-replicated partitions
- Request latency
- Network saturation
- Disk I/O saturation
- CPU usage
- Log flush / I/O wait
If brokers are overloaded, consumers may also appear slow because fetch requests are delayed.
9) Check for rebalances
Frequent rebalances can destroy throughput.
Common causes:
- Consumers taking too long between polls
- Crashes
- Deployment rolling restarts
- Static membership not enabled
- Partition assignment churn
Symptoms:
- lag rises in bursts
- frequent “revoking partitions” logs
- consumers temporarily stop processing
Mitigations:
- reduce processing per poll
- tune
max.poll.interval.ms - use cooperative rebalancing
- use static membership if appropriate
10) Validate resource limits
Consumer pods/hosts may be constrained by:
- CPU limits
- memory limits
- GC pauses
- disk throughput
- network bandwidth
Check:
- CPU throttling
- memory OOMs
- GC time
- container restarts
- thread starvation
If running in Kubernetes, look at:
- CPU throttling metrics
- memory requests/limits
- pod restarts
- node pressure
11) Common quick fixes
Depending on what you find, these often help:
- Add more consumer instances
- Increase topic partitions
- Fix hot partitions / key skew
- Batch processing instead of per-message work
- Increase
max.poll.records - Reduce downstream latency
- Optimize serialization/deserialization
- Increase DB connection pool or API throughput
- Reduce rebalances
- Use async processing carefully
- Scale CPU/memory
12) A simple troubleshooting order
If you want a quick sequence:
- Check lag by partition
- Check consumer logs for rebalances/errors
- Measure processing time per message/batch
- Check downstream dependency latency
- Compare consumer count to partition count
- Inspect CPU/memory/GC
- Review Kafka consumer configs
- Check broker health
13) If you want, I can help you pinpoint it
If you share:
- number of partitions
- number of consumers
- current lag
- consumer config
- processing pattern (per message or batch)
- whether there’s a DB/API involved
…I can help you narrow down the likely bottleneck and suggest specific tuning changes.
Brands