Adding custom header using Spring Kafka
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Spring Kafka is a robust framework that allows developers to connect to Apache Kafka and send or receive messages efficiently. One powerful feature of Spring Kafka is the ability to customize the headers of Kafka messages before sending them. This can be particularly useful for implementing features like routing, message filtering, or enriching messages with additional metadata.
Introduction to Kafka Headers
Kafka headers are a feature introduced in Kafka 0.11.0. Headers allow storing key-value pairs associated with the message. These headers are useful for handling cross-cutting concerns like routing, tracing, and so on, without cluttering the message payload.
Configuring Spring Kafka
Before we dive into adding custom headers, it's essential to ensure that your project is configured to use Spring Kafka. This typically involves adding the appropriate Spring Boot starter dependency to your pom.xml or build.gradle file. For Maven, add:
For Gradle:
Adding Custom Headers to Kafka Messages
In Spring Kafka, you can customize headers when producing messages. To demonstrate this, let's assume we have a simple application that sends messages to a Kafka topic.
1. Creating Kafka Producer
First, define a Kafka template, which is required to send messages:
2. Sending Messages with Custom Headers
When sending a message, you can add custom headers using Message<?> of Spring Messaging. Here’s how it can be done:
Here X-Custom-Header is the custom header with its value set to "CustomHeaderValue". The KafkaHeaders.TOPIC is a standard header provided by Spring Kafka to specify the target topic.
Conclusion
Adding custom headers in Spring Kafka is straightforward thanks to useful abstractions like KafkaTemplate and Spring's Message building utilities. Custom headers are beneficial for adding metadata to messages, which can be used by consumers to make more detailed processing decisions.
Summary Table
| Feature | Utility or Use Case |
| Kafka Headers | Introduced in Kafka 0.11.0, used for metadata |
| Spring Kafka | Framework for Kafka integration |
| KafkaTemplate | Spring template for Kafka producers |
| Custom Headers | Useful for routing, tracing, and more |
By leveraging these features, developers can create robust Kafka applications with enhanced message capabilities.
Related reading
- Adding Custom Headers in Kafka Message
- Adding new ZooKeeper node in Kafka cluster?
- After Linux restart, Kafka throwing no brokers found when trying to rebalance
- Aggregate over multiple partitions in Kafka Streams
- Adding header for HttpURLConnection
- Adding header to all request with Retrofit 2
- Aggregator pattern in RabbitMQ
- Allowing RabbitMQ-Server Connections

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.