Skip to content
Kafka Is Fun

BeginnerCore Concepts

What is a Kafka producer and how does key-based partitioning work?

Reference answer

A Kafka producer is a client that publishes records to topics. The partition is chosen in this order: an explicit partition set on the record wins; then a custom partitioner if partitioner.class is configured; then, if the record has a key (and partitioner.ignore.keys is false), the built-in logic hashes the serialized key with murmur2 and takes it modulo the number of partitions. So records with the same key go to the same partition (as long as the partition count does not change), and because a partition is ordered, they are read in the order they were written. Without a key, the default logic uses a sticky partition: it sticks to one partition for a while and then switches, and with partitioner.adaptive.partitioning.enable (default true) it favors partitions on faster brokers. RoundRobinPartitioner is available if you want strict round-robin. Keying by an entity id, such as a customer id, keeps that entity's events together and in order.

Expected key concepts: producer, key, murmur2, hash, same partition, in the order, sticky partition, partitioner.class, partitioner.ignore.keys, RoundRobinPartitioner

How did that feel?

Your own verdict, not the score. It shows up next to this question in the list, so you can come back to the shaky ones.