Skip to the page

Kafka · Write code

A consumer loop that loses emails

The 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

  1. What does auto-commit commit, and when?
  2. Fix it but keep 20 emails sending in parallel.
  3. Which offset do you commit: the one you processed or the next one?
  4. A rebalance happens while 15 emails are still sending. What should happen?
  5. After your fix, a few customers get two emails. Is that acceptable, and how do you reduce it?
  6. 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 every auto.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=False and call store_offsets after 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 and pause() 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=latest makes a new group, or one whose offsets expired, start at the end and silently skip the backlog. Use earliest for work like this; Kafka 4.x also offers by_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 voice

Practise 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.

Start your free interview