KafkaTemplate is Spring for Apache Kafka's high-level producer class. It wraps a Kafka Producer, handles creation and sharing of the underlying producer through a ProducerFactory, and gives you send methods that publish records to topics and return a future for the result. In a Spring Boot application it is auto-configured from spring.kafka.* properties, so you can inject it and start publishing.
Sending is asynchronous: send returns immediately and the record is batched and transmitted by the producer's background thread. Since Spring Kafka 3.0 the returned type is CompletableFuture<SendResult<K, V>>. The older ListenableFuture and addCallback shown in many tutorials were removed.
ProducerFactory, so one template can be shared by the whole application.send overloads for a topic, key, value, partition, timestamp, headers, a ProducerRecord or a Spring Message.ProducerConfig.sendDefault.| Method | Use |
|---|---|
send(topic, value) | Value only, no key |
send(topic, key, value) | Key decides the partition |
send(topic, partition, key, value) | Explicit partition |
send(ProducerRecord) | Full control, including headers |
send(Message) | Spring messaging, headers mapped to Kafka headers |
sendDefault(key, value) | Uses spring.kafka.template.default-topic |
With acks: all the leader waits for the in-sync replicas before confirming. Since Kafka clients 3.0, idempotence is on by default when you leave acks at its default, but stating it makes the intent explicit.
Using userId as the key sends all of one user's messages to the same partition, which keeps them ordered. The callback runs on the producer's I/O thread, so keep it short and never block in it.
To wait for the broker's acknowledgement, call future.get(timeout, unit) or join(). That makes the call synchronous and lowers throughput, so reserve it for cases where the caller must know the outcome.
Configure JsonSerializer as the value serializer and the same template sends objects. For headers or explicit control, build a ProducerRecord:
If you need several template types (for example one for strings and one for JSON), declare a ProducerFactory and KafkaTemplate bean per type, because the Boot template is bound to a single set of serializers. Alternatively RoutingKafkaTemplate picks a factory by topic pattern.
Setting spring.kafka.producer.transaction-id-prefix makes the template transactional. Use executeInTransaction to publish several records atomically:
Consumers see these records only if they use isolation.level=read_committed. A transaction does not include your database, so for "write to the database and publish an event" use the transactional outbox pattern rather than hoping both succeed.
Yes. It shares one producer that is thread safe, so inject the same instance everywhere instead of creating templates per request.
The record is queued and the failure is reported through the future, after the delivery.timeout.ms period. Always handle the result, or log failures in a ProducerListener.
Only within a partition. Records with the same key go to the same partition, and idempotence keeps retries from reordering them.
CompletableFuture, from Spring Kafka 3.0 and Spring Framework 6. Use whenComplete, thenAccept or exceptionally.
KafkaTemplate is the standard way to produce Kafka records in Spring: a thread-safe, auto-configured wrapper over the Kafka producer with typed send methods, a CompletableFuture result, headers, and optional transactions. Choose keys deliberately, handle the result, and use the outbox pattern when database consistency matters.