Topics, partitions, offsets and consumer groups: how Kafka stores and delivers a huge stream of events.
Kafka is a system that stores a huge stream of events in a log: an ordered list where new events are only ever added at the end. Many programs can write to it, and many others can read it, each at their own speed.
A normal queue hands a message to one worker and then throws it away. Kafka works differently. It is a distributed, replicated commit log. Producers append messages to the end. Consumers read from any position they like. Reading does not delete anything.
One log on one machine would be too small and too slow, so a topic is split into partitions, spread over many machines (brokers). A partition is the unit of two things: parallelism (one partition is read by one consumer of a group at a time) and ordering (order is kept only inside one partition).
A message can carry a key (such as a user id or order id). Kafka runs a hash on the key to choose the partition, so the same key always lands in the same partition. That is how you keep all events of one order in the right order, while different orders spread over many partitions.
| Part | What it is | Why it matters |
|---|---|---|
| Broker | A server that stores partitions and serves reads and writes | A cluster has many brokers, so data and load are spread |
| Topic and partition | A named stream, cut into ordered logs | Partitions are the unit of order and parallelism |
| Offset | The position of a message in its partition | A consumer only has to remember one number per partition |
| Producer | Writes messages, choosing the topic and (often) a key | The key sets the partition and so the ordering |
| Consumer group | A set of consumers that share a topic's partitions | Gives scale. Each partition has one reader in the group |
| Replica: leader and followers | Every partition has one leader and some followers | Reads and writes go to the leader. Followers copy it |
| ISR (in-sync replicas) | Followers that are fully caught up with the leader | Only these may take over without losing confirmed data |
| Controller | The part of the cluster that tracks leaders and members | Newer versions run this inside Kafka (KRaft) instead of ZooKeeper |
Each partition has a leader and followers on other brokers. The producer setting acks decides when the leader may say "saved".
| acks | The producer waits for | Risk |
|---|---|---|
| 0 | Nothing | Fastest. A message can be lost with no error |
| 1 | The leader's own write | A leader crash right after OK can lose the message |
| all | Every in-sync replica (and at least min.insync.replicas of them) | Slowest, but a confirmed message survives a leader failure |
A simplified model with a simulated clock and no random numbers. Partition assignment here spreads partitions over live consumers in turn (real assignors differ), and the rebalance pause is shortened for teaching.
Illustrative numbers: when the leader dies, F1 has copied 3 messages and F2 has copied 2. Real clusters differ. The min.insync.replicas slider only matters with acks=all. Only an in-sync replica should take over (a clean election), which is the setting you want for data you cannot lose.
loop:
messages = poll(topic) # get a batch from my partitions
for each message:
do_the_work(message) # must be safe to repeat
commit(offsets) # then move my bookmark forward| Term | Meaning in easy words |
|---|---|
| Topic | A named stream of messages, split into partitions |
| Partition | One ordered, append-only log. The unit of order and parallelism |
| Offset | The position of a message inside a partition |
| Broker | A Kafka server that stores partitions |
| Producer / Consumer | A program that writes messages / a program that reads them |
| Consumer group | Consumers that share a topic's partitions, one reader per partition |
| Rebalance | Handing partitions to different consumers when one joins or leaves |
| Consumer lag | How many messages a group still has to read |
| Replication factor | How many copies of each partition exist |
| ISR | In-sync replicas: the followers that are fully caught up |
| Log compaction | Keeping only the latest message for each key, instead of deleting by age |
| Choice | We gain | We lose |
|---|---|---|
| More partitions | More parallel consumers and throughput | More files and overhead, longer rebalances, and key mapping changes when you add them |
| acks=all | No loss of confirmed messages | Higher write latency, and writes fail when too few replicas are in sync |
| acks=1 | Faster writes | A leader crash can lose confirmed messages |
| Long retention | Replay far back in time | More disk and cost |
| Key per entity | Order for each entity | A hot key overloads one partition |
| Commit after work | No lost messages | Duplicates after a crash, so the consumer must be idempotent |