Part 2 of Learning Kafka
Topics, partitions and keys
Module 2 · 5 min read
Topics, partitions and keys
TL;DR
- A topic is just a name. Its partitions are the real logs, spread across brokers.
hash(key) % partitionspicks the partition, and order only holds inside one. Watch for hot keys.- The partition count caps parallel consumers and can never shrink. An event’s address is (topic, partition, offset).
The words, quickly
An event is one fact, e.g. {type: "PaymentCompleted", paymentId: 8812, from: "alex", to: "maya", amountCents: 500}. A producer writes events (the payment service) and a consumer reads them (the push sender, SMS sender, inbox writer).
A topic is a named stream of related events, like payments, otp-requests or user-profile-changes. Services subscribe only to what they care about. Related doesn’t mean one type. payments carries PaymentCompleted, PaymentFailed and PaymentReversed, which share a subject and usually a reader.
The partition is the real thing
I had this backwards. A topic isn’t one thing on disk, its partitions are. Each is an ordered, append-only log on a server (a broker). Brokers together make a cluster.
One log on one machine is capped by one disk and network card for writes, one disk for storage and one server for reads. Partitions on several brokers share that out. Writes to P0, P1 and P2 land on three machines at once, three SMS senders can each read one partition, and growing means moving whole partitions to new brokers.
Which partition does an event go to?
The producer decides. If the event has a key,
partition = hash(key) % number_of_partitions
Same key, same partition, every time, and Kafka only keeps order inside a partition. I key notifications by user ID, so Maya’s “payment pending” and “payment successful” stay in order and her phone never shows success first. Her events and Alex’s don’t need ordering against each other, luckily. Topic-wide order would need every broker to agree on every event (a network round trip per write) and kill the parallelism.
Keys buy order and cost balance. A very busy key (a big merchant, or a campaign keyed by campaign ID) piles onto one partition. Its consumer falls behind and nobody else may read that partition to help (Module 3). Use a better key like merchantId + bucket, or ask if those events need order at all.
No key means even load and no order. Not strictly round-robin though, the “sticky” partitioner fills a batch for one partition before moving on, which keeps batches big.
Adding partitions moves keys. The hash stays, % N changes. Old events stay put and new ones go elsewhere, so a user’s events can be read out of order around the switch.
Real Kafka hashes keys with murmur2. The demo’s hash is different but behaves the same.
How many partitions?
You set the count when you create the topic.
kafka-topics.sh --bootstrap-server localhost:9092 \
--create --topic payments --partitions 12 --replication-factor 3
In a consumer group each partition has one reader, so the count caps how many instances work on the topic in parallel. I start from throughput, e.g. 6,000 notifications a second at 500 per SMS-sender instance needs at least 12. Then leave room. Adding partitions later moves keys and you can never remove one, so plan for a year or two. 12 also splits evenly across 2, 3, 4 or 6 instances.
Don’t go wild though. Each partition costs open files and memory on the brokers, a buffer in every producer, and time in failover and rebalancing. Roughly, 3–6 for a quiet topic and 12–30 for normal service traffic. Beyond that, do the maths and load-test.
Offsets are positions
Each event appended to a partition gets the next number, 0, 1, 2…, its offset. Partitions count separately, so offset 2 is a different event in P0, P1 and P2. One exact event needs all three.
(topic, partition, offset) e.g. (payments, 1, 2)
Producers never pick offsets and offsets never change, so a consumer’s position is one number per partition. That’s the next module.
Quick check
You key the payments topic by merchant ID. One merchant makes 40% of all payments. What happens?
Different keys spread evenly, but one key always maps to one partition and Kafka never splits it. Merchant ID plus a small bucket number fixes it, if you can live with looser ordering.
Q&A
Can I reduce the number of partitions later?
No. You can only add them, and that changes where keys map, so a key’s new events land apart from its old ones. Pick a sensible number up front.
What happens if I send events without a key?
The producer spreads them across partitions to balance load. Fine when order doesn’t matter, but two events about one user can then be read in either order.