System design concepts · Doing Things Later

Kafka for System Design Interviews: Partitions, Consumer Groups, Ordering

Kafka isn't really a queue. It's a distributed, replicated, append-only log that many readers can replay at their own pace. Once you see it that way, its design makes sense.

What you'll be able to explain
  • Explain topics, partitions, offsets and consumer groups
  • Choose partition keys and counts
  • Configure durability with replication, acks and ISR
  • Explain delivery semantics and exactly-once in Kafka
  • Use retention and compaction appropriately

The log

A topic is split into partitions. Each partition is an ordered, append-only log; every record gets an increasing offset. Records are not deleted when read: they stay for the retention period (e.g. 7 days) or forever with compaction.

SERVICEProducerskey = order_idQUEUE / LOGPartition 0offsets 0…9,412QUEUE / LOGPartition 1offsets 0…8,877QUEUE / LOGPartition 2offsets 0…9,105WORKERConsumer 1group: billingWORKERConsumer 2group: billingWORKERAnalytics groupseparate offsetshash(key)
A topic with three partitions consumed by one consumer group. Each partition goes to exactly one consumer in the group.

The four ideas that matter

  1. Partitions are the unit of parallelism and ordering. Order is guaranteed only within a partition. Records with the same key go to the same partition, so they stay ordered.
  2. Consumer groups share work. Within a group, each partition is read by exactly one consumer. More consumers than partitions means idle consumers.
  3. Each group tracks its own offsets. Billing can be up to date while analytics replays yesterday. Adding a new consumer group doesn't affect anyone else.
  4. Reading doesn't delete. You can rewind offsets to reprocess data after a bug fix, which queues can't do.
Analogy

A queue is a to-do list where you cross items off. A Kafka topic is a newspaper archive: anyone can read any issue, at their own pace, and the archive keeps the issues whether or not they've been read.

Producing and partitioning

  • Key choice: order_id keeps each order's events ordered. A skewed key (one giant merchant) creates a hot partition.
  • Partition count: target throughput ÷ per-partition throughput, plus room to grow (you can add partitions later, but that changes which partition a key maps to). Tens to a few hundred per topic is typical.
  • Batching and compression (linger.ms, compression.type=lz4|zstd) give big throughput wins.

Durability: replicas, ISR and acks

Each partition has a leader and followers on other brokers (replication factor 3 is standard). Followers that are caught up form the in-sync replica set (ISR).

Producer acks Meaning Durability
0 Don't wait Can lose data freely
1 Leader wrote it Lost if leader dies before followers copy
all All ISR wrote it Safe with min.insync.replicas=2

The standard durable setup: RF = 3, acks=all, min.insync.replicas=2: survives one broker failure with no data loss, and refuses writes rather than silently degrading if two are down.