Scrivio Interview library
DebuggingIntermediate5 min read · updated 13 November 2026

A Java service and a Python service both produce to the topic "account-events" keyed by account id.

A Kafka Streams join and per-account ordering both break: events for the same account land on different partitions depending on which service sent them. Explain the cause and the fix.

Try it yourself

Consumer group: who reads which partition

Change the number of partitions and consumers, and see who owns what, who is idle, and where ordering holds.

Read this example as text

Who reads which partition

  1. 3 partitions, 2 consumers. C0 reads P0, P1; C1 reads P2.
  2. 4 partitions, 2 consumers. C0 reads P0, P1; C1 reads P2, P3.
  3. 3 partitions, 3 consumers. C0 reads P0; C1 reads P1; C2 reads P2.
  4. 6 partitions, 4 consumers. C0 reads P0, P1; C1 reads P2, P3; C2 reads P4; C3 reads P5.
  5. 2 partitions, 4 consumers. C0 reads P0; C1 reads P1; C2 is idle; C3 is idle.

A partition has exactly one reader in the group. A consumer beyond the number of partitions is idle.

Six records with 3 partitions and 2 consumers

  1. Record A1. Key A goes to P0, read by C0.
  2. Record B1. Key B goes to P1, read by C0.
  3. Record A2. Key A goes to P0, read by C0.
  4. Record C1. Key C goes to P2, read by C1.
  5. Record B2. Key B goes to P1, read by C0.
  6. Record A3. Key A goes to P0, read by C0.

A1, A2 and A3 sit in one partition and are read in that order. Nothing orders a record in one partition against a record in another.

What this example leaves out
  • One topic, and Kafka's range assignor, the first in the default list. Other assignors spread partitions differently.
  • A, B and C stand for key hashes 0, 1 and 2. The real client hashes the key bytes; the rule shown, same key to the same partition, is the same.
  • A rebalance is shown as instant. In a real group, consumers stop briefly while partitions move.

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.

Java
# Same key bytes, 12 partitions
key = b"acct-1002"
murmur2 (Java default) -> partition 3
CRC32 (librdkafka consistent_random) -> partition 0
# Key encoded as a JSON string by one service: b'"acct-1002"'
murmur2 -> partition 10

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.

Why the same account lands in different partitions (12 partitions)
ProducerKey bytesHashPartition
Java, StringSerializeracct-1002 (UTF-8)murmur23
Python, default partitioneracct-1002 (UTF-8)CRC320
Python, JSON key"acct-1002" with quotesmurmur210
Python, fixedacct-1002 (UTF-8)murmur23

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.

Java
# Python (confluent-kafka), Java-compatible partitioning
from confluent_kafka import Producer
p = Producer({
"bootstrap.servers": "b1:9092",
"partitioner": "murmur2_random", # default is consistent_random (CRC32)
})
p.produce("account-events", key="acct-1002".encode("utf-8"), value=payload) # partition 3, same as Java

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.

What to say in the interviewAbout 90 seconds aloud

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/