Quick revision
The short answer
The key should be the conversation id: then all messages of a conversation land on one partition, and every consumer reads them in the order the broker appended them, which is what readers of a conversation need. A sender id keeps only each sender's own messages in order, so messages from different people in one conversation can interleave differently, and a message id or no key spreads load but gives no order at all. Even with the right key, order is arrival order at the partition leader, not client time, and adding partitions, changing key serialization, retry topics and concurrent processing in the consumer can still reorder messages.
What to remember
In this answer, we saw that keying chat messages by conversation id gives every reader the same order for each conversation, while a sender id keeps only per-sender order and a message id or no key gives no order. Kafka's order is arrival order at the partition leader, so when exact order matters a server-assigned sequence is needed. Even with the right key, adding partitions, changing key serialization, retry topics and concurrent per-record processing in consumers can still reorder messages.
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 key should be the conversation id: then all messages of a conversation land on one partition, and every consumer reads them in the order the broker appended them, which is what readers of a conversation need. A sender id keeps only each sender's own messages in order, so messages from different people in one conversation can interleave differently, and a message id or no key spreads load but gives no order at all. Even with the right key, order is arrival order at the partition leader, not client time, and adding partitions, changing key serialization, retry topics and concurrent processing in the consumer can still reorder messages.
Why this question is asked
Choosing a record key is the most important decision when we design a Kafka topic, because the key decides both which records stay in order and how load spreads. A chat service makes the trade-off very visible: readers immediately notice when a reply appears above the question it answers. Interviewers ask this to check that we understand Kafka's ordering guarantee precisely and know the less obvious ways order breaks. In this answer, we will recap how keys map to partitions, compare the three key choices, see what order really means, and walk through what can still reorder messages.
1A Quick Recap: Keys, Partitions and Order
A topic is split into partitions, and Kafka guarantees order only within one partition: consumers read a partition's records in exactly the order they were appended. Across partitions there is no order at all.
The producer chooses the partition from the record key. The default partitioner serializes the key to bytes, hashes them with murmur2 and takes the result modulo the number of partitions. So the same key, serialized the same way, always goes to the same partition while the partition count stays the same. Records without a key are spread across partitions for throughput.
2Comparing the Three Keys
With the conversation id as key, every message in conversation C-77 goes to the same partition. All readers of C-77 see Alice's question, Bob's reply and Alice's thanks in the same order, the order in which they arrived at the broker. Load still spreads well, because there are millions of conversations.
With the sender id as key, all of Alice's messages stay in order, but Bob's messages go to another partition. A consumer reading both partitions may process Bob's reply before Alice's question, depending on which partition it fetches first and how far behind it is on each. Two consumers may even show different interleavings.
With the message id as key, or no key at all, records spread evenly, which maximises throughput, but there is no ordering between any two messages. That suits independent events, not a conversation.
| Key | Order within a conversation | Load spread |
|---|---|---|
| Conversation id | Total order per conversation | Good: many conversations |
| Sender id | Only per sender; senders interleave | Good |
| Message id or no key | None | Best |
3Order Means Arrival Order at the Leader
There is a subtle point here. Kafka's order is the order in which records were appended by the partition leader, not the order in which users pressed send. Suppose Alice and Bob are connected to two different app servers. Alice sends at 10:00:00.100, Bob at 10:00:00.120. Alice's server is briefly slow, so Bob's record reaches the leader first. Kafka appends Bob's message first, and every reader sees it first, consistently, but not in the order the users acted.
Usually this is fine, since a difference of milliseconds is invisible. When conversation order must be exact, for example for a legal record, we need a single authority to assign a sequence: a conversation service that numbers each message, sequence 41, 42, 43, before producing, and readers sort or display by that number. Client clocks cannot be trusted for this, because devices disagree by seconds.
6 messages in order. Play them, or step through them one at a time.
4What Can Still Reorder Messages
Adding partitions changes the modulo, so a conversation's key may now map to a different partition. New messages go to the new partition while older ones may still be unread on the old one, and a consumer can read them out of order. If we must add partitions, we do it at a quiet time or plan a migration.
Changing how the key is serialized has the same effect. The partitioner hashes bytes, not values, so a conversation id sent as a Long and later as a String produces different bytes and a different partition, even though the value looks the same.
Retry topics also reorder. If processing message 41 fails and Spring Kafka sends it to a retry topic, message 42 is processed first, and 41 comes back seconds later. For chat, a blocking retry on the main partition keeps order. Finally, consumers that process records of one partition concurrently, for example by handing each record to a thread pool, lose order inside the consumer even though Kafka delivered it correctly. Concurrency should be per key or per partition, not per record.
5Follow-up questions
Would a very popular group chat create a hot partition?
Yes, all of its messages go to one partition. For normal chats this is fine, because one partition handles thousands of messages a second. For extremely large channels, such as broadcast rooms, we can accept per-sender ordering or sub-key by thread, since readers of a huge room rarely need a strict global order.
Does enabling idempotence on the producer matter for ordering?
Yes. Without idempotence, a retried batch can be written after a later batch when several requests are in flight, which reorders records from one producer. With enable.idempotence=true, the default in current clients, the broker uses sequence numbers to keep the producer's order with up to five in-flight requests, so retries do not reorder.
6Conclusion
In this answer, we saw that keying chat messages by conversation id gives every reader the same order for each conversation, while a sender id keeps only per-sender order and a message id or no key gives no order. Kafka's order is arrival order at the partition leader, so when exact order matters a server-assigned sequence is needed. Even with the right key, adding partitions, changing key serialization, retry topics and concurrent per-record processing in consumers can still reorder messages.
Open with
I would key by conversation id. The default partitioner hashes the serialized key and takes it modulo the partition count, so all messages of one conversation land on one partition, and every consumer reads them in the order the broker appended them, which is what readers need.
Then, if they want more
With the sender id, each sender's messages stay in order, but messages from different senders in the same conversation land on different partitions, so readers can see any interleaving, a reply before its question. With the message id or no key, records spread evenly for throughput but there is no ordering at all.
I would also point out what order means: it is arrival order at the partition leader, not client time. If two app servers send for the same conversation, they can append in either order, so when conversation order must be exact I would add a sequence assigned by a server.
And several things can still reorder messages even with the right key. Adding partitions moves keys to different partitions. Changing the key's serialization, for example from Long to String, changes the hash. Retrying failures through retry topics lets later messages overtake.
And processing records of one partition concurrently in the consumer, for example with a thread pool, loses order inside the application.
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 "Should the record key be the sender id, the conversation id or the message id? Explain...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-design/2833a3e169/