advanced
Kafka partitions
Use partitions as the unit of parallelism and per-key ordering, choosing keys carefully because repartitioning affects scale and ordering.
Partitions are the unit of parallelism and per-key ordering in Kafka. Messages with the same key land in the same partition and are read in order there. More partitions increase throughput ceiling but also file handles, rebalance cost, and minimum consumer parallelism.
Trade-off: repartitioning is hard — choose count from expected throughput and consumer count early.
On interviews: pick a key for order events so all events for one order are ordered; effect of null keys.
Common pitfalls: hot partitions from skewed keys; too many partitions for small cluster; changing partition count breaking key order assumptions.
Checklist:
- Key by natural business identifier for ordering.
- Size partitions from peak throughput and consumer targets.
- Monitor per-partition lag for hot spots.
- Plan repartitioning as migration, not routine ops.