ProducerFactory is the Spring for Apache Kafka interface that creates the Kafka Producer instances used to send records. KafkaTemplate does not build producers itself: it asks its ProducerFactory for one, so the factory is where connection settings, serializers, reliability options and transactions are defined.
In a Spring Boot application you rarely write it by hand, because Boot auto-configures a DefaultKafkaProducerFactory from spring.kafka.* properties. You define your own when you need several clusters, different serializers per template, or settings the properties do not cover.
The interface (org.springframework.kafka.core.ProducerFactory<K, V>) has one core job: createProducer() returns a Kafka Producer<K, V>. The standard implementation, DefaultKafkaProducerFactory, adds the following behaviour:
acks and so on).KafkaTemplate<K, V> takes the factory in its constructor and calls it for every send.
With the spring-kafka dependency on the classpath, Boot creates a ProducerFactory and a KafkaTemplate from properties:
For many applications this is all you need. You can inject KafkaTemplate<String, OrderEvent> and send straight away.
Declare your own beans when properties are not enough. Boot backs off from its auto-configured ProducerFactory and KafkaTemplate once you define them.
Sending then looks like this:
send returns a CompletableFuture<SendResult<K, V>> in current Spring Kafka versions (3.x and later), so use whenComplete rather than the older callback API.
| Setting | Effect |
|---|---|
acks=all | The leader waits for all in-sync replicas before confirming a write |
enable.idempotence=true | Prevents duplicates caused by retries; the default in Kafka clients 3.0 and later |
retries and delivery.timeout.ms | Control how long the producer keeps retrying a failed send |
linger.ms and batch.size | Trade a little latency for larger, more efficient batches |
compression.type | Reduces network use (lz4, zstd, snappy, gzip) |
Different topics often need different serializers or clusters. Define one ProducerFactory and one KafkaTemplate per case and inject them by qualifier or generic type.
For Kafka transactions, set a prefix on the factory:
The factory then creates transactional producers, and KafkaTemplate operations can run inside a transaction through executeInTransaction or a KafkaTransactionManager. Transactions require acks=all and idempotence.
ProducerFactory creates and configures Kafka producers. KafkaTemplate is the high-level API that uses a factory to send records and offers convenience methods and listeners.
No. Boot creates a DefaultKafkaProducerFactory from spring.kafka.* properties. Define your own only for multiple clusters, special serializers or advanced settings.
No. Without transactions, DefaultKafkaProducerFactory shares one producer instance among all callers, because the Kafka producer is thread-safe.
DefaultKafkaProducerFactory offers methods to update or remove configuration properties and to reset the factory, which closes the cached producer so a new one is created with the updated settings.
ProducerFactory is the component that builds Kafka producers for KafkaTemplate. Spring Boot provides a ready-made DefaultKafkaProducerFactory, and you replace it with your own bean when you need custom serializers, several clusters or transactions. Keep reliability settings such as acks=all and idempotence explicit, and share the factory rather than creating producers by hand.