Part 3 of Learning Kafka
Consumers: groups, lag and delivery guarantees
Module 3 · 6 min read
Consumers: groups, lag and delivery guarantees
TL;DR
- Consumers pull at their own pace. In a group each partition has one reader, so partitions cap parallelism.
- If lag lasts longer than retention, unread events are deleted for good.
- Commit after the work and de-duplicate by notification ID. Kafka transactions can’t un-send an SMS.
Kafka never pushes
I assumed Kafka called each subscriber when an event arrived, like the observer pattern. It’s the reverse. Each consumer asks “anything after offset 48,213 in P0?”. If nothing is new, the broker holds the request open until something arrives (a long poll), so pulling doesn’t hammer it.
Pull lets each consumer set its own speed. The SMS gateway might allow 200 messages a second while push takes thousands. The SMS sender just asks for less, and the backlog waits on the broker’s disk instead of the sender’s memory. Back pressure for free.
Consumer groups: one group per job
I run three copies of the SMS sender. If two read Maya’s “you received $5” event, she gets two texts. A consumer group makes them split the work.
Inside a group, each partition is read by exactly one consumer. Different groups read the same partitions completely independently.
One group is one service, its consumers are that service’s instances. Push, SMS, inbox and analytics are four groups on the same payments topic.
That answers Module 1’s “queue or log?”. One group makes Kafka act like a queue, several make it a log with many readers. Same storage, read differently.
Lag, and why more instances don’t always help
The number I watch most is consumer lag, the latest offset minus the group’s position, summed over partitions. The simulated topic has 6 partitions and each SMS-sender instance sends 250 a second.
With 6 partitions, a 7th or 8th instance gets nothing. It’s just a hot spare. Scaling isn’t free either. Each join or leave triggers a rebalance that reassigns partitions and briefly pauses reading. With cooperative rebalancing only the moved partitions pause, but a crash-looping instance still causes churn.
Back pressure has a limit too. Stay slower than the producers for longer than retention and the oldest unread events are deleted. For notifications that’s silent, permanent loss, so I alert on lag.
Committing: the bookmark
Each group keeps one number per partition, the offset of the next event to read. Saving it is committing. Kafka keeps commits in the internal topic __consumer_offsets, basically a key–value table.
(group=sms-sender, topic=payments, partition=0) → 47,980 (group=sms-sender, topic=payments, partition=1) → 51,204 (group=inbox-writer, topic=payments, partition=0) → 48,213
After a crash, whoever takes over resumes from the committed offset. So when you commit relative to the work decides what users see. This SMS sender handles four notifications per batch.
| Guarantee | How | On a crash | For notifications |
|---|---|---|---|
| At most once | commit, then send | the rest of the batch is never sent | someone never gets their OTP. Bad. |
| At least once | send, then commit | part of the batch is sent twice | someone gets “you received $5” twice. Annoying, and scary for a money message. |
| Effectively once | at least once + skip IDs already sent | retried, but duplicates are blocked | what you actually want |
I default to at-least-once. A duplicate can be caught, a lost message can’t come back. Then I make the send idempotent (safe to repeat). Each notification gets an ID at creation, which the consumer checks before sending and records after. A crash between sending and recording can still let one duplicate through, so this shrinks the window without closing it. My notification series covers how I handle that.
One trap. enable.auto.commit=true, the default, commits during poll() whatever the previous poll returned. Finish each batch before polling and that’s at-least-once. Hand events to a thread pool and poll straight away, and it can commit unfinished work, i.e. at-most-once without you realising.
“Exactly once” has a boundary
Kafka’s exactly-once transactions make the output writes back to Kafka and the offset commit atomic, and read_committed consumers never see half a transaction. Great for stream processing (read one topic, write another). It can’t reach outside Kafka, though. No transaction recalls an SMS already sent to the gateway. For real-world side effects, exactly-once means at-least-once plus idempotency.
What if a bad event gets in?
You can’t edit or delete one event. The log records what happened; current state (queued, sent, delivered) lives in a database. A schema (Avro, Protobuf or JSON Schema with a schema registry) stops malformed events before they’re produced. If a bad one gets in anyway, publish a correction that cancels or overrides it, like an accountant’s correcting entry.
What you can’t process goes to a dead-letter topic so the consumer doesn’t get stuck. Kafka doesn’t build this in, you or your framework do (my reliability part shows my version). Old events leave once past retention (7 days by default, or a size limit), read or not.
Quick check
Your SMS sender sends a batch, then crashes before committing. No de-duplication. What do users see?
The committed offset still points at the start of the batch, so the replacement sends it again (at-least-once). Kafka doesn’t know an SMS went out. Only your record of sent IDs can stop the repeat.
Q&A
What triggers a rebalance?
A consumer joining, leaving or crashing (it stops sending heartbeats), taking too long between polls, or partitions being added. Reading can pause briefly while the group reshuffles.
Does committing an offset delete the message?
No, it only moves the group’s bookmark. The message stays until retention removes it, and other groups (or this one, after a reset) can still read it.