KafkaTemplate is the Spring Kafka class you use to publish records to Apache Kafka topics. It wraps the low-level Kafka producer, handles producer creation and lifecycle, converts keys and values with the configured serializers, and returns a CompletableFuture so you can react to success or failure of each send.
Spring Boot auto-configures a KafkaTemplate bean when spring-kafka is on the classpath, so in most applications you inject it and call send.
KafkaTemplate<K, V> sits on top of a ProducerFactory<K, V>. The factory holds the producer configuration and shares one thread-safe Kafka Producer between callers, so you create a single template and reuse it everywhere.
The template adds these things on top of the raw client:
ProducerRecord or Spring Message.CompletableFuture<SendResult<K, V>>.ProducerListener for logging, observability through Micrometer and transactions.sendDefault.executeInTransaction for exactly-once pipelines.Add the starter dependency. Spring Boot manages the version.
Configure the broker and the serializers in application.yml:
acks: all waits for all in-sync replicas, and idempotence prevents duplicate records when the producer retries. Recent Kafka clients enable both by default, but stating them keeps the intent visible.
Declare the topic as a bean, and Spring Boot's KafkaAdmin creates it at startup if it does not exist:
Inject the template and call send. The key decides the partition, so records with the same key keep their order.
The common overloads are:
| Method | Use |
|---|---|
send(topic, value) | No key, partitioner chooses the partition |
send(topic, key, value) | Same key goes to the same partition |
send(topic, partition, key, value) | Explicit partition |
send(ProducerRecord) | Full control, including headers |
send(Message) | Spring Message with headers such as KafkaHeaders.TOPIC |
sendDefault(key, value) | Uses the template's defaultTopic |
send returns immediately. The record is delivered to a batch and sent in the background, so check the returned future to learn whether the broker acknowledged it.
Since Spring Kafka 3.0 (Spring Boot 3) send returns a CompletableFuture. Older versions returned a ListenableFuture with addCallback, which was removed. To block until the broker answers, call future.get(10, TimeUnit.SECONDS). Do it only where you need a synchronous guarantee, because it limits throughput.
To attach headers, build a ProducerRecord:
For atomic writes to several topics, set spring.kafka.producer.transaction-id-prefix to enable transactions and use the template:
Consumers must read with isolation.level=read_committed to see only committed records. A Kafka transaction does not include your database, so use the outbox pattern when you must update a database and publish an event reliably.
KafkaTemplate for every message instead of reusing the single bean.String template key with an Object serializer configured, which produces ClassCastException or SerializationException.send to be synchronous. Call flush() before shutting down tests that read the topic immediately.Yes. It shares a thread-safe Kafka producer, so one bean can serve all threads.
KafkaTemplate produces messages. @KafkaListener consumes them from a topic.
Configure JsonSerializer as the value serializer, as in the YAML above, and send a plain object. The consumer uses JsonDeserializer with trusted packages configured.
Handle the returned CompletableFuture. A successful SendResult carries the topic, partition and offset.
KafkaTemplate is Spring's high-level Kafka producer. Reuse one instance, give records a key when ordering matters, always handle the CompletableFuture it returns, and enable idempotence or transactions when delivery guarantees matter.