Is key required as part of sending messages to Kafka?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
No, a Kafka message key is not required. You can publish records with a null key, but whether that is a good idea depends on how you want partitioning, ordering, and downstream processing to behave.
What The Key Actually Does
A Kafka record conceptually has:
- a topic
- an optional key
- a value
- headers and metadata
The key is not just extra payload. Producers and partitioners use it to influence which partition a record lands in.
That matters because Kafka guarantees ordering only within a partition, not across the entire topic.
Sending Records Without A Key
A producer can send a record with only a value:
This is valid. Kafka will still accept and store the message.
For a quick console example:
Every line you type is sent without a key unless you enable key parsing explicitly.
What Happens When The Key Is Missing
If no key is present, the partition is chosen by the producer's partitioning strategy. Older explanations often say "round-robin," but modern Kafka clients commonly use sticky partitioning behavior for unkeyed records to improve batching efficiency.
The important practical point is this: without a key, you should not depend on related messages landing in the same partition.
That means no per-entity ordering guarantee unless you set the partition yourself or provide a meaningful key.
When You Should Use A Key
A key is useful when you need one of these behaviors:
- all records for the same entity should go to the same partition
- order must be preserved for a user, account, or aggregate
- downstream stateful processing groups by key
- log compaction should keep the latest record for a logical identifier
For example, if all events for customer-42 should stay ordered, use that customer identifier as the key.
Now the same key will hash to the same partition, assuming the same partition count and partitioner behavior.
When No Key Is Fine
No key is usually fine when:
- events are independent of each other
- partition-level ordering by entity does not matter
- even distribution is more important than grouping
- consumers treat each message in isolation
Telemetry, fire-and-forget logs, and some background event streams fit this model.
Python Example
The same idea applies in Python clients:
With a key:
The second version is the one to use if per-customer ordering matters.
Keys And Partition Skew
Using a key is not automatically better. A bad key can overload one partition.
If one customer or tenant produces most of the traffic, hashing by that identifier can create skew, where one partition becomes the hotspot while others stay underused.
That is why key choice is a data-model decision, not just a producer syntax detail.
Keys And Compacted Topics
Keys are especially important on log-compacted topics. Compaction works by key, keeping the latest value for each key over time. If you publish without keys, compaction cannot represent the logical "latest value for entity X" pattern.
So for changelog or upsert-style topics, a key is usually essential even though the protocol still allows null.
Common Pitfalls
The most common mistake is assuming a key is required because many examples show one. It is optional.
Another mistake is omitting the key while still expecting all events for one entity to stay ordered. That only works if partition assignment is controlled in some other way.
Developers also repeat outdated explanations that unkeyed records always use round-robin partitioning. Client behavior has evolved, so focus on the practical guarantee: unkeyed records do not give you stable entity grouping.
Finally, choosing a highly skewed key can create a throughput bottleneck even though the code is technically correct.
Summary
- Kafka does not require a key for every message.
- A missing key is valid, but it weakens control over partition placement and ordering.
- Use keys when grouping, per-entity ordering, compaction, or stateful processing matter.
- Skip keys when messages are independent and partition grouping is irrelevant.
- Choose keys carefully to avoid partition skew.

