Kafka Topics and Partitions
The durable, partitioned append-only log
A Kafka topic is an append-only, partitioned log. Partition count decides parallelism and ordering scope; replication guarantees durability. Designing topics (keys, count, retention) is a core data-engineering skill.
What you will learn
- Explain topic → partition → replica structure
- Design partition counts and keys for parallelism + ordering
- Set retention and replication to match SLAs and cost
What is this?
A Kafka topic is a named, immutable, append-only log split into partitions, each partition being an ordered sequence of records. Producers choose a partition (via a key or round-robin), each partition is replicated to multiple brokers for durability, and consumers divide partitions among them. Topic design — keying, partition count, retention, replication — determines throughput, ordering scope and recovery capability.
Why do you need it?
- Partitions are the unit of parallelism for both producers and consumers.
- Ordering is guaranteed within a partition — so keys that must stay ordered share a partition.
- Replication copies each partition across brokers so broker failure loses nothing committed.
- Retention governs replay depth vs storage cost — the core trade for event-driven systems.
Mental model
A filing cabinet with labeled drawers
Topic and partitions
The topic is the cabinet; each drawer a partition holding an ordered stack of records. If a duplicate subject must stay in order, it goes in the same drawer (same key). Each clerk (consumer) reads drawers in sequence without mixing them.
How it works
- Producers append records to topics; the broker assigns each to a partition.
- With a key, the record's partition = hash(key) % partitions — same key, same partition, ordering preserved.
- Without a key, round-robin spreads records for higher aggregate throughput.
- Each partition has replicas on different brokers; the leader serves reads/writes, followers stay in sync.
- Retention (time-large, size-large, log compaction) auto-deletes or compacts old records.
Topic layout across partitions and replicas.
In production
- Click events keyed by session_id preserve per-session order while parallelizing across sessions.
- A 48-partition topic matches the consumer group's parallelism on a 48-executor cluster.
- Ride-hailing orders replicated 3x survive the loss of any single broker.
Best practices
- Dimension partitions to your consumer parallelism AND peak throughput, not to 'some number'.
- Key related events with a stable, bounded-cardinality key (user_id, device_id) for ordered processing.
- Control retention deliberately: days of data you might actually replay, not 'forever'.
- Set short- to-medium retention with replication for the dual guarantee of durability + replay.
Common mistakes
- Hundreds of partitions but a consumer group with 4 instances leaves 96 idle.
- Choosing a huge-cardinality or unbounded key that produces a partition per garbage value.
- Turning up replication to survive a failure and paying 3x storage you never use.
- Putting 10MB blobs on a 'stream' topic, turning the broker into a file server.
Common issues
One partition always has huge lag, others zeroA hot key or non-uniform keying overloads one partition
Salt the hot key or widen the key space so partitions balance.
Committed data is lost when a broker failsReplication factor 1 or an unclean leader election losing un-replicated + acked data
Set replication factor ≥3 and wait for ISR acks; verify min.insync.replicas.
Replay window too short to rebuild stateRetention too small (time.minutes/size.max)
Grow retention to match your recovery SLA; use compacted topics for keyed state.
Debugging tips
- Check partition vs consumer-group size for idle partitions (underused parallelism).
- Lag per partition tells you the slowest consumer; balance hot partitions by keying.
- Message ordering issue? Verify the key is stable — a changing key breaks the same-partition path.
- Use kafka-topics to inspect partitions and kafka-consumer-groups for lag distribution.
Interview checkpoint
Hands-on lab
- Create topics with chosen partitions and replication
- Publish keyed events and inspect partition assignment
- Measure retention/replication settings and behavior
Design and Inspect Kafka Topics
40 min · intermediate
Quick quiz
Two events for the same user arrive at different partitions. What is lost?
Kafka topics delete records immediately once all consumers have read them.