Kafka: A null key is not the same as an empty key

Null means "choose a partition for me"; an empty string is a real key that always hashes the same way.

Code
new ProducerRecord<>("orders", null, payload);   // sticky partitioner
new ProducerRecord<>("orders", "", payload);     // murmur2("") - one partition
Output
null: spread across all partitions in batches
""  : every record to the SAME partition - an accidental hot partition
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11