Is Kafka message headers the right place to put event type name?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Apache Kafka is a powerful, distributed event streaming platform often used for building real-time data pipelines and streaming applications. In Kafka, a message (also called a record) is composed of key-value pairs, and can also include optional metadata headers. As developers or architects design their Kafka implementations, one common consideration is the best placement of metadata such as event type names. This article explores whether message headers are the suitable spot for storing event type names in Kafka messages.
Understanding Kafka Message Components
Before diving into the use of headers, let's first outline the main components of a Kafka message:
- Key: Typically used to decide the partition within a Kafka topic where the record will be sent.
- Value: The actual data payload of the message.
- Headers: Optional key-value pairs that can store metadata about the message.
Why Store Event Type Names?
Event type names classify the kind of data or event the message represents, which can be crucial for consumers to parse and react to the data accordingly. By specifying event type, consumers can implement logic to handle different types of messages differently, or route messages to different processing streams based on their type.
Pros and Cons of Storing Event Type in Headers
Advantages
- Decoupling: Storing event types in headers helps in decoupling the payload from its metadata. This way, the payload format need not be altered to accommodate event type changes, aiding in achieving schema stability.
- Flexible Consumer Logic: Consumers can read the header to determine how to process the message without needing to deserialize the entire message payload, enhancing performance especially in scenarios where payload processing is expensive.
- Versioning and Compatibility: Headers can also facilitate easy changes in event type naming or semantics without impacting the message payload or disrupting existing consumers who might be using an older version.
Disadvantages
- Increased Message Size: Headers add to the overall message size, which might affect network performance and storage, especially with a high throughput of messages.
- Complexity in Consumer Implementation: Consumers need to implement additional logic to read and process headers, adding to the complexity of consumer applications.
Technical Implementation
When using Kafka client libraries such as Java, you can easily add headers to a Kafka message. Below is an example using the Kafka Java client:
Alternatives to Using Headers
- Embedding within payload: Including the event type inside the message payload itself.
- Using topic naming conventions: For example, distinct topics for different event types like
orders-placed,orders-fulfilled, etc.
Summary Table
| Factor | Headers | Payload | Topic Structure |
| Decoupling | High | Low | Medium |
| Consumer Processing | Simplified logic | Complex logic | Simplified logic |
| Message Size | Increased | No change | No change |
| Complexity | Medium | Low | High |
| Performance Optimization | Possible | Not possible | Not possible |
Conclusion
Whether Kafka headers are the right place to put event type names depends on specific use cases. If having compact, efficient messages is crucial and event types are not frequently changed or are uniform across messages, embedding event information in the payload or using naming conventions for topics might be better. However, for scenarios prioritizing flexibility and minimal coupling between message content and metadata, using headers to store event type is a practical and powerful solution.
Related reading
- Is Kafka ready for production use?
- is kafka reliable when used as a message bus in micro services
- Is Kafka Stream StateStore global over all instances or just local?
- Is Kafka suitable for running a public API?
- Is logical replication using pglogical possible with timescaleDB?
- is Lost update possible with RAFT?
- Is Kafka timestamp order corresponding to the offset?
- Is KafkaTemplate thread safe

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.