Kafka · Write code
A consumer loop that loses emails
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voiceThe interviewer asks
This Python consumer sends order confirmation emails. After a pod restart, some customers never got their email, and the logs show no errors. Why?
from confluent_kafka import Consumer
from concurrent.futures import ThreadPoolExecutor
consumer = Consumer({
"bootstrap.servers": BROKERS,
"group.id": "order-emails",
"enable.auto.commit": True,
"auto.offset.reset": "latest",
})
consumer.subscribe(["order-events"])
pool = ThreadPoolExecutor(max_workers=20)
while True:
msg = consumer.poll(1.0)
if msg is None or msg.error():
continue
pool.submit(send_email, json.loads(msg.value())) # returns at once
Then
Follow-ups the interviewer asks next
- What does auto-commit commit, and when?
- Fix it but keep 20 emails sending in parallel.
- Which offset do you commit: the one you processed or the next one?
- A rebalance happens while 15 emails are still sending. What should happen?
- After your fix, a few customers get two emails. Is that acceptable, and how do you reduce it?
- A brand-new consumer group starts with
latest. What does it miss?
The answer
What a strong answer covers
- Auto-commit commits what
poll()has handed out, not what's finished: the client stores the offset when it returns the message, and a background commit sends it everyauto.commit.interval.ms(5 seconds by default). Emails still waiting in the pool are already committed, so a restart skips them. That's at-most-once. - Errors are swallowed: nobody checks the future, so a failed email is never retried or even logged. The pool's queue has no limit, so memory grows when emails are slow.
- Fix: turn off auto-commit (or set
enable.auto.offset.store=Falseand callstore_offsetsafter each email). Track, per partition, the highest offset below which every message is done, and commit that plus one (the committed offset is the next one to read). Cap the work in flight andpause()partitions when the pool is full. - On revoke (a rebalance), wait briefly for in-flight work and commit what's done; anything uncommitted is redelivered to the new owner.
- At-least-once means duplicates: make the email idempotent (a sent table keyed by event id, or the provider's idempotency key).
auto.offset.reset=latestmakes a new group, or one whose offsets expired, start at the end and silently skip the backlog. Useearliestfor work like this; Kafka 4.x also offersby_duration:<duration>.
The red pen
Common traps
Avoid:"turn on exactly-once"; committing every message synchronously at high volume without measuring; committing in completion order from the pool (a later offset commits past an unfinished earlier one).
Practise this out loud
Say your answer out loud to an AI interviewer that follows up, and get a score for it.
Practise by voicePractise questions like this out loud
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.