Kafka · mid
Kafka consumers: offsets, commits and lag
A consumer saves its position by committing offsets. When you commit decides what you lose or repeat. Lag shows how far behind a group is, but you must read it correctly.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- The committed offset is the next offset to read. Lag is the log-end offset minus the committed offset.
- Auto-commit commits what poll() has returned, not what is finished. If work continues in other threads, a restart skips unfinished messages. That is at-most-once.
- At-least-once: turn off auto-commit and commit only after the work is done. Commit in order: never commit an offset higher than an unfinished earlier message. At-least-once means duplicates, so make the work idempotent (a table keyed by event id, or the provider's idempotency key).
- Do not swallow errors. Retry temporary ones with back-off without committing. Send permanent ones (bad JSON) to a dead-letter topic with the error in headers, then commit and move on.
- auto.offset.reset applies when a group has no valid committed offset. latest silently skips the backlog. Use earliest for work that must not be skipped, or none to fail loudly.
- Keep each batch small enough (max.poll.records) to finish inside max.poll.interval.ms. Commit finished work when partitions are revoked. On shutdown call close() so the group rebalances at once.
- Read kafka-consumer-groups --describe: a partition whose CURRENT-OFFSET does not move while LOG-END-OFFSET grows is stuck. More consumers than partitions leaves some consumers idle. Resetting offsets works only while the group has no active members, and it skips messages, so copy them first.
The red pen
Common traps: mistake → fix
Mistake: Turn on exactly-once to fix lost emails
Fix: the loss comes from committing early.
Mistake: Add more consumers to fix lag
Fix: they cannot exceed the partition count, and a stuck message blocks one partition.
Mistake: Reset offsets while consumers run
Fix: it does not work, and it skips data.
Mistake: Commit in the order work finishes in a thread pool
Fix: a later offset can commit past an unfinished earlier one.
Easy to hard
Questions interviewers ask, easy to hard
- A pod restart caused lost emails and no errors in the log. Why?
- What is consumer lag?
- How do you commit safely with a thread pool?
- Read this consumer-groups output: one partition is 8.9 lakh behind. What next?
- Write a consumer that saves events to a database safely.
Practise
Practise with these questions
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.