KafkaTemplate is the Spring for Apache Kafka class you use to send records to Kafka topics. It wraps a Kafka Producer, creates and caches it through a ProducerFactory, and exposes send methods that return a CompletableFuture you can use to check the result.
In a Spring Boot application, Boot auto-configures a KafkaTemplate<?, ?> bean from spring.kafka.* properties, so you usually only inject it. Since Spring Kafka 3.0 (Boot 3), send returns CompletableFuture<SendResult<K, V>>; the older ListenableFuture was removed.
send, sendDefault, flush, partitionsFor and metrics.executeInTransaction and a transactional producer factory.Setting acks: all makes the broker confirm only after all in-sync replicas have the record. Recent Kafka clients enable idempotence by default when the other settings allow it, and stating it makes the intent clear. Without these properties, strings need no extra configuration because Boot defaults to StringSerializer.
The key decides the partition, so all events for one orderId land on the same partition and keep their order. Without a key, the producer spreads records across partitions.
send is asynchronous: it returns once the record is queued in the producer buffer. To block, call get(timeout, unit) on the future, but do this sparingly because it limits throughput.
| Method | Use it for |
|---|---|
send(topic, value) | Value only, no key |
send(topic, key, value) | Keyed record |
send(topic, partition, key, value) | Explicit partition |
send(ProducerRecord) | Full control, including headers |
send(Message) | Spring Message with KafkaHeaders |
sendDefault(key, value) | Uses spring.kafka.template.default-topic |
Headers example:
A returned future that completes normally means the broker acknowledged the record according to acks. Failures such as timeouts or serialization errors complete the future exceptionally, so always handle that case. Tune delivery.timeout.ms and retries for how long the producer keeps trying, and keep idempotence on so retries do not create duplicates.
For atomic writes across topics, set spring.kafka.producer.transaction-id-prefix and use executeInTransaction, or annotate a method with @Transactional:
Consumers must use isolation.level=read_committed to see only committed records.
Declare topics as beans so Boot's KafkaAdmin creates them at startup:
Yes. It shares one producer by default, so inject a single instance and call it from many threads.
The call is asynchronous. The future completes when the broker acknowledges the record, so use the callback to confirm delivery.
Yes. Configure JsonSerializer for the value and send the object directly. Alternatively serialize to a String yourself with an ObjectMapper.
It was replaced by CompletableFuture in Spring Kafka 3.0. Replace addCallback with whenComplete.
KafkaTemplate is the standard producer abstraction in Spring Kafka. Boot configures it from properties, it serializes keys and values, sends asynchronously and supports headers and transactions. Use keys to preserve order, handle the CompletableFuture result, keep acks=all with idempotence for safety, and declare topics as beans.