Quick revision
The short answer
Yes. The key decides partitioning, ordering, compaction and co-partitioning for joins, so changing it breaks consumers as surely as removing a field. The standard I would set is that every topic documents its key (which entity, its serialisation and the hash used), the ordering it promises and whether it is compacted, with the key schema in the schema registry; keys default to the id of the entity whose changes must stay in order, and no key is used only when there is no ordering need. Key changes happen only as versioned migrations, and the standard is enforced through topic creation and contract tests and measured by fewer broken consumers.
What to remember
In this answer, we saw that keys deserve the same governance as values because they decide partitioning, ordering, compaction and co-partitioning, so changing them is a breaking change. The standard documents each topic's key entity, serialisation, hashing, ordering promise and compaction, with the key schema registered; keys default to the id of the entity that needs ordering, and no key is used only without an ordering need. Key changes go through a versioned migration with dual publishing, and the standard is enforced at topic creation and through contract tests, with fewer broken consumers as the measure of success.
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.
Yes. The key decides partitioning, ordering, compaction and co-partitioning for joins, so changing it breaks consumers as surely as removing a field. The standard I would set is that every topic documents its key (which entity, its serialisation and the hash used), the ordering it promises and whether it is compacted, with the key schema in the schema registry; keys default to the id of the entity whose changes must stay in order, and no key is used only when there is no ordering need. Key changes happen only as versioned migrations, and the standard is enforced through topic creation and contract tests and measured by fewer broken consumers.
Why this question is asked
Most companies govern event values carefully, with schemas and compatibility checks, and leave keys to whoever writes the producer. The result is the situation in this question: each topic keyed differently, nobody sure what a key promises, and key changes that break consumers in production. Interviewers ask this to see whether a senior engineer understands why keys deserve the same discipline as payloads and can set a practical standard. In this answer, we will recap everything the key controls, define what the contract should say, set a default rule for choosing keys, describe how key changes happen, and finish with enforcement and measurement.
1A Quick Recap: Everything the Key Controls
The key looks like metadata, but it drives four behaviours. Partitioning: the producer hashes the key to choose the partition. Ordering: Kafka only orders records within a partition, so the key decides which events are ordered relative to each other. Compaction: on a compacted topic, Kafka keeps only the latest record per key, so the key defines what counts as 'the same thing'. Co-partitioning: Kafka Streams joins and similar processing need both topics keyed the same way and partitioned the same way.
So a consumer that keeps per-order state assumes all events for an order arrive in one partition, in order. If the producer switches the key from order id to customer id, the events of one order may still share a partition, but a compacted topic now keeps one record per customer instead of per order, and a join with an order-keyed topic stops matching. That is a breaking change, even though no field changed.
| Key controls | Consumer assumption | If the key changes |
|---|---|---|
| Partitioning | An entity's events reach one consumer | Events for an entity split across consumers |
| Ordering | Events for an entity arrive in order | Order between old and new partitions is lost |
| Compaction | Latest record per entity is kept | Wrong records survive or are deleted |
| Joins | Both topics keyed and partitioned alike | Joins silently miss records |
2What the Contract Says About the Key
Every topic's contract states, next to its value schema: which entity the key identifies, its exact serialisation, the partitioning rule (murmur2 of the key bytes, which non-Java clients must match), the ordering the topic promises (for example 'all events for one order are in order; no order between orders'), and whether the topic is compacted. The key schema itself can be registered in the schema registry under the topic's key subject, so serialisation is checked like the value's.
3The Default Rule for Choosing a Key
The key should be the id of the entity whose changes must stay in order. For order events, that is the order id; for account balance changes, the account id. That gives the finest key that still keeps the needed order, which spreads load best and makes compaction meaningful. A coarser key, like customer for order events, should be a deliberate choice with a stated reason, for example a consumer that must see all of a customer's orders in sequence.
No key is allowed only when events have no ordering need at all, such as independent log or metric records. Keyless topics get the best batching and load spread, which is a real benefit, but the contract must say there is no ordering promise, so nobody builds a consumer that assumes one.
4Changing a Key: Always a Versioned Migration
Because a key change is breaking, it never happens in place. The producer team creates a new topic version with the new key, for example orders.order-events.v2, and publishes to both topics for a transition period. Consumers move to v2 one by one, each taking care to switch at a clean point for its state, for example by rebuilding state from v2 from the beginning. When all consumers have moved, the producer stops writing v1, and the topic is retired after its retention.
- Propose the new key in a contract reviewEntity, format, ordering, reason
- Create topic v2 with the new keySame or new partition count
- Producer publishes to v1 and v2Dual publishing for the transition
- Consumers move to v2 one by oneEach switches at a clean point
- All consumers on v2?YesStop writing v1, retire after retentionNoKeep dual publishing
5 steps in order. Play them, or step through them one at a time.
5Enforcing It and Measuring the Payoff
Standards that live only in a wiki decay, so enforcement goes where topics are created. The topic creation process, whether a self-service portal, Terraform or a GitOps repository, requires the key section of the contract before a topic exists. The schema registry checks key compatibility. Producer contract tests in each service's pipeline check that the key format and partition for sample entities match the contract, which also catches the cross-language partitioner problem.
We track the payoff: the number of incidents caused by key changes or key mismatches, which should drop to near zero; the share of topics with a complete key contract; and how safely partition counts can now be grown, since a documented key tells us exactly which ordering a repartitioning migration must preserve. We roll the standard out to new topics first, then to the most critical existing topics, rather than demanding that every old topic be documented at once.
6Follow-up questions
Does this slow teams down?
A little at topic creation: a few lines in the contract and a review. In return, consumers can rely on keys without reading producer code, and the costly incidents from silent key changes go away. Making the key section part of a topic template keeps the cost to minutes.
What about existing topics with inconsistent keys?
We document what each one actually does today, even if it is not ideal, so consumers at least know the truth. Topics whose keys cause real problems get a planned v2 migration; the rest are left as they are and marked as legacy in the catalogue.
7Conclusion
In this answer, we saw that keys deserve the same governance as values because they decide partitioning, ordering, compaction and co-partitioning, so changing them is a breaking change. The standard documents each topic's key entity, serialisation, hashing, ordering promise and compaction, with the key schema registered; keys default to the id of the entity that needs ordering, and no key is used only without an ordering need. Key changes go through a versioned migration with dual publishing, and the standard is enforced at topic creation and through contract tests, with fewer broken consumers as the measure of success.
Open with
Yes, keys should be part of the governed contract, because a key isn't just metadata. It decides the partition, and therefore which events are ordered relative to each other.
Then, if they want more
On a compacted topic it decides what survives, since Kafka keeps the latest record per key. And joins depend on both topics being keyed and partitioned the same way. So changing a key breaks consumers just as much as removing a field, and that's what we've been seeing.
The standard I would set: every topic's contract describes the key, which entity it identifies, its exact serialisation, and the hash, murmur2 of the key bytes, with librdkafka clients set to murmur2_random. It also states the ordering promise, for example all events for one order are in order, and whether the topic is compacted.
The key schema goes in the schema registry like the value schema. The default choice is the id of the entity whose changes must stay in order. A coarser key needs a stated reason, and no key is only for events with no ordering need.
Key changes never happen in place: a new topic version, dual publishing, consumers move over one by one, then the old topic is retired. Enforcement goes where topics are created, so you can't create a topic without the key section, plus contract tests in producer pipelines. And I'd measure it by key-related incidents going to near zero.
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 keys be part of the governed event contract, and what standard would you set?": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/362be98a05/