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.
The four ideas that matter
- 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.
- Consumer groups share work. Within a group, each partition is read by exactly one consumer. More consumers than partitions means idle consumers.
- 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.
- Reading doesn't delete. You can rewind offsets to reprocess data after a bug fix, which queues can't do.
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_idkeeps 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.