How it works
A message queue typically removes a job once a worker finishes it. A log keeps records, in order, until a retention limit passes, which lets many independent readers share one stream and replay it. In Kafka, a topic is split into partitions. Each partition is an append-only sequence, and each record gets an offset, its position in that partition. A producer’s key is hashed to choose a partition, so records with the same key land together.
A consumer group is a set of consumers sharing a topic’s work. Kafka assigns each partition to one member of the group at a time, and the group commits how far it has read. For example, an orders topic has 6 partitions and is keyed by order ID. Events for order 812 all go to partition 3, at offsets 1041, 1042, and 1043, and are read in that order. A billing group with 6 consumers gives each one partition, and a seventh consumer sits idle. A search-indexing group reads the same records with its own offsets, without affecting billing’s position.
Order holds within a partition, not across a topic, so orders 812 and 813 can interleave if they land on different partitions. Partition count caps a group’s parallelism, and one slow record delays everything behind it in that partition. Adding partitions changes where a key hashes, so an order’s new events can separate from its older ones. When a consumer dies, another member resumes from the last committed offset, so work done but not yet committed runs again.
An accounts topic has 8 partitions keyed by account ID, and consumers apply balance changes in order. Lag is growing, so a colleague proposes raising it to 16 partitions tonight and doubling the consumers. What do you check before agreeing?
New partitions change where accounts hash, so an account’s new events can be read before its older events finish on the old partition, which breaks per-account order. First check whether the lag comes from a hot key or slow per-record work, since more partitions would fix neither. If you do need more partitions, pause producers and drain the backlog first, or migrate to a new topic.
Use this as a guide for self review; written answers are not graded.Keep in mind
- Records stay after being read, so each group tracks its own offset and can replay.
- Ordering holds per partition, so choose the key whose records must stay in order.
- A group cannot use more active consumers than it has partitions.