Apache Kafka is one of the most powerful tools for building distributed applications. Whether you’re building microservices, streaming systems, or real-time applications, Kafka acts as a reliable and scalable messaging backbone. And when it comes to Spring Boot, the KafkaTemplate
class is your go-to tool for producing messages to Kafka topics.
In this complete guide, you’ll learn how to use KafkaTemplate
effectively in Spring Boot, covering configuration, best practices, transactional messaging, common mistakes, and real-world code examples.
1. What is KafkaTemplate?
KafkaTemplate
is a Spring abstraction that simplifies the interaction with Kafka. It wraps the Kafka producer and provides a convenient way to send messages asynchronously or synchronously.
Key Capabilities:
- Send messages with or without keys
- Support for transactions
- Custom partitioning
- Callback for success/failure
- Integration with Spring’s dependency injection
2. When and Why to Use KafkaTemplate
- When you want strong integration with Spring Boot
- When your application produces messages to Kafka topics
- If you want to leverage Spring’s transaction management
- If you prefer clean, testable code via dependency injection
3. Setting Up Kafka in Spring Boot
Add the Kafka dependency in your pom.xml
:
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
4. Kafka Configuration in Spring Boot
Spring Boot simplifies Kafka configuration. Here’s how you can set it up:
application.properties
spring.kafka.bootstrap-servers=localhost:9092
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
or application.yml
spring:
kafka:
bootstrap-servers: localhost:9092
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
5. Basic KafkaTemplate Usage
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
public void sendMessage(String topic, String message) {
kafkaTemplate.send(topic, message);
}
You can also send with a key:
kafkaTemplate.send("topicName", "key1", "value1");
And use callbacks:
kafkaTemplate.send("topicName", "Hello!").addCallback(
result -> System.out.println("Message sent successfully"),
ex -> System.err.println("Message failed: " + ex.getMessage())
);
6. Advanced: KafkaTemplate with Transactions
To ensure atomic delivery to multiple topics, enable Kafka transactions.
Step 1: Configure Transaction ID Prefix
application.properties
spring.kafka.producer.transaction-id-prefix=txn-id
application.yml
spring:
kafka:
producer:
transaction-id-prefix: txn-id
Step 2: Annotate Your Method
import org.springframework.transaction.annotation.Transactional;
@Transactional
public void sendTransactionalMessage(String msg) {
kafkaTemplate.executeInTransaction(t -> {
t.send("topic1", msg);
t.send("topic2", msg);
return true;
});
}
Why Transactions?
- Guarantees exactly-once delivery (with idempotent consumers)
- Enables multi-topic publishing in a single logical unit
- Prevents partial writes in case of failure
7. Best Practices
- Use keys when message ordering per partition matters
- Monitor delivery status using callbacks
- Batch sends for high throughput
- Avoid blocking on
get()
unless necessary - Use topic-level configuration like replication and partitioning wisely
- Enable idempotence for safety (enabled by default in Spring Kafka >=2.3)
8. Common Mistakes to Avoid
- Not setting
transaction-id-prefix
when using@Transactional
- Forgetting to flush messages before shutdown
- Hardcoding topic names (use config instead)
- Blocking the calling thread in callbacks
- Sending large messages without tuning broker limits
9. Frequently Asked Questions (FAQs)
Can I send objects with KafkaTemplate?
Yes! Just configure a serializer (e.g., JSON):
spring.kafka.producer.value-serializer=org.springframework.kafka.support.serializer.JsonSerializer
What happens if the broker is down?
By default, Kafka will retry based on your producer config. You can customize retries, timeouts, and error handling.
How do I log KafkaTemplate activity?
Enable debug logging:
logging.level.org.springframework.kafka=DEBUG
10. Conclusion
KafkaTemplate
is a powerful tool that can help you seamlessly integrate Kafka into your Spring Boot applications. Whether you’re sending simple logs or building mission-critical pipelines, mastering KafkaTemplate is key to building scalable, resilient applications.
By configuring it correctly, using transactions when needed, and applying best practices, you’ll be able to produce Kafka messages like a pro.