Mahir Shahriar Tamim

AI Researcher · Explainable & Responsible AI · Vision, Language & Agentic Systems

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) % partitions picks 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.

The payment service writes events to a Kafka cluster of three brokers. The payments topic has partitions P0, P1 and P2, one on each broker. The otp-requests topic has P0 and P1 on brokers 1 and 3. Payment service producer Kafka cluster Broker 1a server payments · P0 otp-requests · P0 Broker 2a server payments · P1 Broker 3a server payments · P2 otp-requests · P1
A broker holds partitions from many topics; a partition belongs to exactly one topic. (Each partition is also copied to other brokers. That’s Module 4, so the copies are left out here.)

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.

Key
Payment by

Each chip is one event (“Maya 2” is Maya’s second; the small number is its offset in that partition). The bars show how busy each partition is. Try random payments, the big merchant, “no key”, then adding a partition.

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?

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.