What it is
A Kafka topic is split into partitions. Each partition is its own append-only log with its own offsets, stored on one broker (the leader) and replicated to others. Producers append each record to exactly one partition; consumers read partitions independently. Kafka Streams, Redpanda, Amazon Kinesis (shards), Azure Event Hubs and Apache Pulsar (partitioned topics) use the same idea.
Why it exists
One log on one broker, read by one consumer, has a ceiling: the write speed of that broker and the processing speed of that consumer. Partitions let a topic grow past it. Different partitions can live on different brokers and be read by different consumers at the same time, while each partition stays strictly ordered. The topic gives up global order in exchange for horizontal scale.
How it works
Choosing a partition. The producer client decides. If the record has a key, Kafka’s default partitioner computes murmur2(keyBytes), makes it positive and takes it modulo the partition count. The same key therefore always lands on the same partition while the count stays the same. Records without a key are spread across partitions; current Java clients fill a batch for one partition before moving to the next, which gives larger batches than strict round robin. Producers can also supply a custom partitioner or name the partition explicitly.
Reading partitions. Within a consumer group, each partition is assigned to exactly one consumer. A consumer can own several partitions, but a partition is never split between two members of one group. So a group can use at most as many consumers as there are partitions; extras sit idle (see the consumer groups lesson).
Ordering. A partition is read in offset order by its one consumer, so records of one key are stored and delivered in the order they were written. Across partitions there is no order at all. Producer retries could reorder records within a partition, which is why idempotent producers (the default since Kafka 3.0) attach sequence numbers that the broker checks.
Hot partitions. Load per partition is the sum of the keys that hash to it. When keys are skewed (one big tenant, one viral product, a default value such as null or "unknown" used as the key), one partition gets much more than its share. Its consumer falls behind while others are idle. Adding partitions spreads the other keys more thinly but cannot split a single key.
Changing the partition count. You can add partitions to a topic but not remove them. Adding changes hash mod N, so most keys move: from 3 to 4 partitions about three quarters of keys land elsewhere. Old records stay where they were, so for a while a key has unprocessed records on two partitions, possibly read by two consumers in either order. Consumers that keep per-key state (Kafka Streams state stores, in-memory aggregates) find that state on the wrong instance.
When to use it
- Any topic whose throughput or consumer work exceeds what one consumer can do.
- When related events must stay in order: key by the entity (order id, account id, device id) so they share a partition.
- When consumers keep per-key state locally: partitioning routes every event of a key to the same consumer.
When not to
- Low-volume topics: one or a few partitions keep things simple and still allow a few consumers.
- When you need a global order of all events: that requires one partition, and therefore one consumer, or a sequencing step elsewhere.
- When work items are independent and short: a work queue with competing consumers scales per message, not per partition (Kafka 4.x share groups, KIP-932, also move in this direction).
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| More partitions | More consumers in parallel, more brokers used | Broker memory and file handles, replication traffic, slower recovery and rebalances |
| Fine-grained key (order id) | Even spread, per-entity order | No order across entities (for example across one customer’s orders) |
| Coarse key (tenant id) | Order across everything of a tenant | Hot partitions when tenants differ in size |
| No key | Best spread, biggest batches | No per-entity order at all |
Salting a hot key (key#0…#7) |
Spreads one key’s load | Order only within each salt; readers must merge |
Alternatives
- Work queues (SQS, RabbitMQ, Kafka share groups): any consumer takes any message, so parallelism is not tied to partitions; per-key order needs FIFO features such as SQS FIFO message groups.
- Separate topics per heavy tenant: isolates a hot key at the price of more topics to manage.
- Consistent hashing in your own routing layer: moves fewer keys when the number of buckets changes; Kafka’s default partitioner does not do this.
Failure modes
- Hot partition: one partition’s lag grows while total utilisation looks fine. Monitor lag and bytes in per partition, not just per topic.
- Null or constant keys: a missing key that defaults to the same value sends everything to one partition.
- Repartition surprise: adding partitions to fix lag reorders keys and breaks per-key state.
- Too few partitions: you cannot add consumers to catch up after an incident, because extras would be idle.
- Too many partitions: long leader elections and rebalances, and producers spread data so thinly that batches stay small.
In production
Estimate partitions as peak throughput ÷ what one consumer sustains (measured, not guessed), then add headroom for growth so you rarely change it. Watch per-partition lag (consumer lag exporters, Burrow, Cruise Control or your platform’s metrics) and per-partition bytes in. Keep the partitioner identical across all producers of a topic (different languages’ clients hash differently unless configured to match). If you must increase partitions on a keyed topic, plan it: pause or drain producers, or create a new topic and migrate.
Common misconceptions
- "More consumers always means more throughput." Not beyond the partition count.
- "Adding partitions fixes a hot partition." It spreads other keys; the hot key stays on one partition.
- "Kafka orders a topic." It orders each partition. Two keys on different partitions have no defined order.
- "Partitions are only about speed." They also define ordering and placement, which is why the count is hard to change.
Interview angle
When you put Kafka in a design, say what the key is and why ("key by order id so an order’s events stay in order"), estimate the partition count from throughput, and mention hot keys before the interviewer does. A strong answer notes that consumers in a group are capped by partitions, that order is per partition only, and that repartitioning moves keys, so you size with headroom.
Model assumptions
The simulation in this lesson is a simplified model, not a benchmark. Producers send records at random (Poisson), 75 per second (15 in the key-hashing step). Each partition is a queue with exactly one consumer; each consumer handles one record at a time, about 22 ms of work (lognormal, cv 0.3) plus a 3 ms fetch hop, so roughly 40 records per second. Keys come from 1,000 equally likely values, or from 100 Zipf-distributed values (exponent 2) in the hot-key steps, where the hottest key carries about 60% of records. The simulated partitioner is a hash ring with 64 points per partition: shares come out within about ±15% of even, and adding a partition moves only the keys it takes over. Kafka’s default murmur2 mod N moves most keys; that and the key-hashing and moved-key animations are scripted illustrations, not simulated. In the challenge each partition holds at most 100 unprocessed records and rejects the rest, standing in for a retention-size budget. Not modelled: broker limits, replication, batching and compression, consumer fetch sizes. Numbers show behaviour and orders of magnitude only.