Quick revision
The short answer
The Java client hashes keys with murmur2, while clients built on librdkafka, such as the common Python client, default to a different partitioner (consistent_random, based on CRC32), so the same key can map to a different partition. Even with the same algorithm, the key bytes must match exactly. We fix it by setting the librdkafka partitioner to murmur2_random and agreeing on one key serialiser, test that both clients pick the same partition for sample keys, repair already misplaced records by re-keying into a new topic or a careful replay, and make the partitioning rule part of the topic's contract.
What to remember
In this answer, we traced the split to two causes: librdkafka's default consistent_random partitioner hashes keys with CRC32 while Java uses murmur2, and different key serialisation produces different bytes even with the same hash. We fixed it with partitioner=murmur2_random and one agreed key format, proved it with a cross-client contract test, repaired existing data by re-keying into a new topic when order matters, and made partitioning part of the topic's contract.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
The Java client hashes keys with murmur2, while clients built on librdkafka, such as the common Python client, default to a different partitioner (consistent_random, based on CRC32), so the same key can map to a different partition. Even with the same algorithm, the key bytes must match exactly. We fix it by setting the librdkafka partitioner to murmur2_random and agreeing on one key serialiser, test that both clients pick the same partition for sample keys, repair already misplaced records by re-keying into a new topic or a careful replay, and make the partitioning rule part of the topic's contract.
Why this question is asked
This bug is common in polyglot companies and hard to spot, because each service on its own works perfectly. Only when a consumer expects all events for one account in one partition, or a Kafka Streams join expects matching keys in matching partitions, do things break, and they break silently: joins miss records and ordering is violated without any error. Interviewers ask it to see whether we know that partitioning is a client decision and can trace a cross-language difference to its cause. In this answer, we will recap why same key should mean same partition, find the two ways the services disagree, fix them, deal with data already written, and prevent a repeat.
1A Quick Recap: Same Key, Same Partition
Kafka keeps order only within a partition. Producers choose the partition, and for keyed records the usual rule is a hash of the key modulo the partition count. Per-account ordering works only if every producer sends all events for an account to the same partition.
Kafka Streams joins rely on the same idea, called co-partitioning: two topics joined by key must have the same number of partitions and must place each key in the same partition number in both. Then the Streams task for partition 5 sees the matching records from both topics. If one producer places a key differently, the task never sees the pair, and the join misses it.
2Cause 1: Different Hash Algorithms
The Java client's built-in logic hashes the serialised key bytes with murmur2. librdkafka, the C library under the popular Python, Go and .NET clients, has several partitioners, and its default is consistent_random: CRC32 of the key for keyed records, random for keyless ones. Different hash functions give different partitions for the same bytes.
So the Java service writes account acct-1002 to partition 3 and the Python service writes it to partition 0. Consumers see the account's events split across two partitions, with no ordering between them.
3Cause 2: Different Key Bytes
Even with the same algorithm, the hash is computed over bytes. If the Java service uses a StringSerializer, which writes the UTF-8 bytes of acct-1002, and the Python service serialises its key as JSON, it sends the bytes "acct-1002" with the quotes included, and murmur2 places it in partition 10. Numbers are another trap: a Java IntegerSerializer writes four big-endian bytes, while another service may send the text 1001. Same logical id, different bytes, different partition.
| Producer | Key bytes | Hash | Partition |
|---|---|---|---|
Java, StringSerializer | acct-1002 (UTF-8) | murmur2 | 3 |
| Python, default partitioner | acct-1002 (UTF-8) | CRC32 | 0 |
| Python, JSON key | "acct-1002" with quotes | murmur2 | 10 |
| Python, fixed | acct-1002 (UTF-8) | murmur2 | 3 |
4The Fix
On every librdkafka-based producer we set the partitioner to murmur2_random, which uses the Java-compatible murmur2 for keyed records and random choice for keyless ones (murmur2 is also available). We agree on one key format, here the UTF-8 bytes of the account id with no JSON encoding, and write it down.
Then we prove it. A small contract test produces a set of sample keys from both services to a test topic with the production partition count and checks that each key lands in the same partition from both. It runs in both services' build pipelines, so a library upgrade or a config change that alters partitioning fails the build instead of production.
5Repairing Data Already Written
Fixing the producers does not move records already in the wrong partitions. If consumers only need ordering from now on, we can deploy the fix and accept the historical mix. If downstream state depends on order, or a Streams application must rebuild its state correctly, we re-key: a small job reads the topic, recomputes the partition with the agreed rule, and writes into a new topic with the same partition count, ordering each account's events by a sequence number or timestamp from the payload where one exists. Consumers then move to the new topic, and the Streams application is reset to rebuild its state from it. If events carry no reliable order field, we must say honestly that the original order between the two services' events cannot be recovered.
6Making It Part of the Contract
The deeper problem is that the partitioning rule was decided by each client's defaults. We make it part of the topic's documented contract: the key's meaning, its exact serialisation and the hash used. Shared client configuration for each language sets the partitioner, and the contract test above keeps everyone honest.
7Follow-up questions
Why not just switch the Java service to CRC32 instead?
Because Java producers, Kafka Streams and Kafka Connect all use murmur2 by default, and many more Java components will write to the company's topics. Aligning the non-Java clients to murmur2 is a single setting; changing every Java component would need a custom partitioner everywhere.
Does the partitioner matter for keyless records?
Not for ordering, since keyless records have no per-key order to keep. It only affects load spread and batching. The fix here is about keyed records.
8Conclusion
In this answer, we traced the split to two causes: librdkafka's default consistent_random partitioner hashes keys with CRC32 while Java uses murmur2, and different key serialisation produces different bytes even with the same hash. We fixed it with partitioner=murmur2_random and one agreed key format, proved it with a cross-client contract test, repaired existing data by re-keying into a new topic when order matters, and made partitioning part of the topic's contract.
Open with
Both services send account id as the key, but the partition is a client decision, and the two clients decide differently. The Java producer hashes the serialised key bytes with murmur2 and takes it modulo the partition count.
Then, if they want more
The Python service is most likely on confluent-kafka, which wraps librdkafka, and librdkafka's default partitioner is consistent_random, which uses CRC32 for keyed records. So the same account goes to, say, partition 3 from Java and partition 0 from Python.
Ordering per account breaks, and the Streams join breaks because co-partitioning requires every key in the same partition number on both sides. There's a second possible cause: the bytes must match. If Python sends the key as a JSON string with quotes, or an integer as text while Java sends four bytes, the hash differs even with the same algorithm.
The fix is to set partitioner to murmur2_random on the librdkafka clients, which is Java-compatible for keyed records, and agree on one key serialisation, here plain UTF-8 of the id. Then I'd add a contract test in both pipelines that checks sample keys land in the same partition from both clients.
For data already written, the producers fix doesn't move anything. If order matters downstream, I'd re-key into a new topic and reset the Streams app to rebuild from it. And I'd make the key format and hash part of the topic's contract, not each client's defaults.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A Kafka Streams join and per-account ordering both break: events for the same account...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/792841975d/