Understanding Kafka: Topics, Partitions, Offsets and Consumer Groups

Introduction

I have watched a lot of smart engineers bounce off Kafka. Not because it is hard, but because they arrive holding the wrong mental model and every fact they learn gets filed in the wrong drawer.

The wrong model is usually “it’s a message queue”. The right one is closer to a paper ledger in a warehouse. Let me start there, because once you have the picture, every piece of Kafka terminology drops into place without effort.

The mental model: a goods-in ledger

A clerk sits at the warehouse door with a bound ledger. Every time a delivery arrives she writes one line: what came in, the reference number, the time. She only ever writes on the next free line. She never goes back and edits, and she never tears a page out.

Three departments need those entries. Accounts reads them to raise invoices. Stock control reads them to update inventory. Dispatch reads them to plan outbound loads. Each department works through the ledger at its own speed, and each keeps a slip of paper marking the last line it dealt with.

A ledger with numbered lines and three departments each holding a bookmark at a different line

Here is the part that matters most, and the part a queue gets wrong:

Reading a line does not remove it. Accounts reading line 412 does not stop stock control reading line 412 an hour later. If dispatch falls two hundred lines behind, that is dispatch’s problem alone. Nothing about the ledger changes.

That is Kafka. Genuinely, that is the whole model. Here is the translation:

Everything below just puts proper names on that picture.

Why it is not a queue, and not a database

In a message queue, taking a message removes it, and each message goes to one recipient. That is a fundamentally different shape. Our ledger does not work that way, and neither does Kafka.

It is not a database either. You cannot look up “all deliveries for reference X” by searching. You read forwards from a position, like turning pages. There is no update-in-place and no delete-a-row.

And that first difference is why offsets exist at all. If reading removed messages, “where did I get to” would be a meaningless question. Because nothing is removed, something has to remember the bookmark, and every question in this article flows from that one consequence.

Messages

One line in the ledger. Kafka says message or record, interchangeably. Four parts, only one of which is your payload:

The four parts of a Kafka record: key, value, timestamp and headers

The key is the part people skip past, and it matters most. It is not a unique id. It is a grouping label, and it decides which partition the message lands in. Think of it as which regional ledger the clerk writes this delivery into. Same key, same ledger, every time.

The value is your payload. To a broker, both key and value are just bytes, and Kafka never looks inside.

Topics and partitions

A topic is a named ledger. It is how you say which stream you mean.

One ledger and one clerk is a bottleneck, so you split it: a ledger per region, several clerks writing at once. Each is a partition, numbered from zero. This is how Kafka goes faster, because several readers can work in parallel.

The cost is ordering, and this trips people up constantly:

Kafka guarantees order within a partition and nowhere else.

If a topic has four partitions, a message written before another may well be read after it. There is no global sequence. What you get instead is the key. Kafka hashes it to pick a partition, so the same key always lands in the same partition.

Records with two keys hashed into two partitions, each key staying in order

Key every event by entity id and all events for that entity stay in order relative to each other, which is what you actually needed. Whole-topic ordering is almost never the real requirement, and asking for it means one partition, which means one consumer, which means no scale.

Offsets

The line number. Every message gets one as it is appended. They start at zero, increase by one, and are per partition line 500 of the north ledger has nothing to do with line 500 of the south one.

An offset is permanent, more like a page number than a position in a list. Message 2,772,099 is that message forever. When old entries are archived off the front, the numbering does not restart or slide backwards.

Two offsets describe a partition at any moment:

A partition showing the earliest retained offset and the latest offset

Latest is the offset the next message will take, so it is always one past the newest existing message. That off-by-one catches people out when they compare numbers.

Retention and compaction

Ledgers do not pile up forever. Each topic has a cleanup policy, and the two options behave so differently that confusing them causes real damage.

delete is by age. Anything older than the retention period is archived off the front, oldest first. The topic is a record of what happened recently.

compact is by key. Kafka keeps the newest entry for each key and discards superseded ones. The ledger stops being a diary and becomes a current-state sheet: one line per entity, showing its latest state.

The same log under delete and compact cleanup policies

Remember this one before you ever skip a consumer forwards. On a compacted topic the log is the current state. Skip to the end and your consumer never sees the current value of any key that is not written again, possibly ever.

Consumer groups

The department, not the individual. A consumer group is a set of application instances that agree to share the reading, by all setting the same group.id.

Kafka gives each partition to exactly one member, so two instances never process the same line. When someone joins or leaves, Kafka redistributes: a rebalance.

Which makes the partition count a hard ceiling on parallelism:

Two partitions assigned to two of three consumers, the third left idle

Three clerks and two ledgers means one clerk has nothing to do. Scaling a consumer beyond the partition count buys you exactly nothing, and I have seen teams add instances for weeks before someone checked.

A group also has a state, and it is the first thing to look at when something seems wrong: Stable means members are assigned and working, Empty means nobody is connected, PreparingRebalance means membership is changing right now.

The committed offset

The slip of paper, and the single most important object in this article.

As a consumer finishes messages it periodically tells the broker “done up to offset N”. That saved number is the committed offset. Three properties are worth internalising:

It is kept by the broker, not your application. Kafka stores it in an internal topic. That is why a consumer restarts and resumes where it left off, and why an operator can move it without touching your code.

It belongs to the group, not the instance. Which is why two applications with different group.ids read the same topic entirely independently, at their own positions. Adding a second reader costs the first one nothing.

It is not the data. Moving the bookmark changes where reading starts and nothing else. No message is created, deleted or altered by it.

A partition with the committed offset marked and the lag region ahead of it

auto.offset.reset, the most misleading name in Kafka

While we are here: auto.offset.reset applies only when a group has no committed offset at all for a partition. It answers “where does a brand new reader start?”, earliest or latest.

The moment anything is committed, it is ignored forever. It does not reset anything on an ongoing basis. Almost everyone reads that name and assumes otherwise, and it is precisely why manual offset resets need to exist.

Lag

How far behind a reader is. Per partition, it is one subtraction:

lag = LOG-END-OFFSET - CURRENT-OFFSET

It counts messages waiting, not minutes of delay. A lag of 22,008 says 22,008 records are unread. It says nothing about how long they sat there or how long they will take to clear.

Three things people consistently get wrong about it:

Lag zero does not mean healthy. It means the bookmark is at the end. A consumer that crashed an hour ago while fully caught up also reads zero. So does a topic nobody is writing to. Zero lag on a dead consumer looks identical to zero lag on a perfect one.

Rising lag is not automatically a fault. On a busy topic, lag reappears within seconds of any reset and oscillates as the consumer keeps pace. Lag that climbs and never comes back down is the real signal.

No bookmark means no lag figure at all. A partition with no committed offset does not appear in the output, rather than showing a huge number. Absence reads as silence, not as a problem.

Resetting offsets

Now the operational task is obvious: a reset moves the bookmark. Someone walks up to the department’s ledger and moves their paper slip to a different line. That is all it does.

You do it to skip a backlog, to replay data through a fixed consumer, or to start a test from a known point. The usual targets:

Where the bookmark lands for earliest, a specific offset, and latest

Two rules that are not negotiable:

The group must be inactive. You cannot move someone’s bookmark while they are reading, because their next commit overwrites it. Kafka refuses the operation outright while members are connected. Every consumer stops first, then starts again after.

Offsets are per partition. A four-partition topic needs four numbers. One value cannot describe a position, and a single --to-offset applies the same number to every partition, which will mean something different in each.

Running it

Every Kafka distribution ships kafka-consumer-groups.sh. Start by seeing where the group actually is:

kafka-consumer-groups.sh --bootstrap-server localhost:9092 \
  --group my-consumer-group --describe

That prints CURRENT-OFFSET, LOG-END-OFFSET and LAG per partition, plus a CONSUMER-ID column. A dash in that column means no member is assigned to the partition, which is worth noticing before you blame the offsets for anything.

Then preview the move. Always dry-run first:

kafka-consumer-groups.sh --bootstrap-server localhost:9092 \
  --group my-consumer-group \
  --reset-offsets --to-latest \
  --topic my-topic \
  --dry-run

Check the proposed offsets, then swap --dry-run for --execute to commit them. Repeat --topic for each topic, or use --all-topics.

On a managed cluster you will also need a properties file passed with --command-config, holding the SASL credentials:

bootstrap.servers=your-broker:9092
security.protocol=SASL_SSL
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule \
  required username="KEY" password="SECRET";

No local Kafka install? The same binary is in the Confluent image, so docker run --rm -v "$PWD":/cc:ro confluentinc/cp-kafka:7.7.1 kafka-consumer-groups --bootstrap-server ... --command-config /cc/client.properties ... works without installing anything.

Common pitfalls

Each of these cost me real time.

Pitfall 1: “Empty” can mean actively consuming

A consumer using assign() rather than subscribe() picks its partitions manually and never registers as a group member. Every monitoring UI I have used will show that group as Empty while it reads happily at full speed.

If you then “confirm the consumer is stopped” by looking at that badge and go ahead with a reset, the running consumer’s next commit quietly undoes your work, and you are left staring at an offset that refuses to change.

Trust the member list from the CLI, not the status badge.

Pitfall 2: consumer IDs prove whether a restart happened

Every consumer instance gets a fresh UUID when it starts. So if you check the group now and again in an hour and see identical consumer IDs, those processes never stopped, regardless of what anyone tells you.

I have used this twice to settle a “we definitely stopped it” conversation in about ten seconds. It is the closest thing to a lie detector in Kafka operations.

Pitfall 3: an out-of-range offset is silently clamped

Ask for an offset below the earliest retained and you get earliest. Ask for one above the latest and you get latest. No error, no warning, nothing in the output to tell you that the number you supplied was not the number applied.

Someone once handed me a target offset of 247,035 for a topic whose offsets ran from 12.27 million to 12.40 million. Applied as given, that quietly becomes a full replay of everything retained. It succeeds. It reports success. It does something completely different from what was asked.

The tell, in that case: 247,035 was suspiciously close to the topic’s message count, which the UI displays a few pixels away from the offsets. Someone had read the wrong field. Always check the actual earliest and latest per partition before accepting a requested number.

Pitfall 4: a dry run does not prove access

--dry-run needs only DESCRIBE permission, which most service accounts hold broadly, often on a wildcard. So it will happily succeed against topics you cannot actually write to.

Authorisation failures appear only on --execute. If you use a dry run to check “can I do this”, you will get a green light and a failure moments later.

Pitfall 5: lag zero afterwards proves nothing

Verify a reset by comparing the committed offset against the target your dry run proposed, not by checking that lag went to zero.

On an active topic, lag is non-zero again within seconds of a successful reset. On an idle one, lag reads zero whether the reset worked or not. In both cases the number tells you nothing about whether you achieved what you intended.

Key takeaways

If you remember nothing else: the data and your position in it are two separate things.

Messages sit in a log that grows at one end and ages off the other. Your position in that log is a bookmark, held elsewhere, owned by your consumer group, moved independently.

Almost every confusing Kafka behaviour is one of those two things doing something while you were watching the other. Lag is just the distance between them. A reset only ever moves one of them.


Glossary