Skip to content
Kafka Is FunProgress

SpringSpring Kafka Configuration

What is the difference between ProducerFactory and KafkaTemplate in Spring Kafka?

Reference answer

ProducerFactory creates KafkaProducer instances (the underlying Kafka client). By default it's a DefaultKafkaProducerFactory which may pool or create per-thread producers. KafkaTemplate is a higher-level abstraction that uses ProducerFactory to get a producer, sends messages, and handles futures. For transactional producers, ProducerFactory must have a transactionIdPrefix — it creates a new producer per transaction. In Spring Boot, both are autoconfigured. You typically only inject KafkaTemplate unless you need to customize producer lifecycle.

Expected key concepts: ProducerFactory, KafkaProducer, DefaultKafkaProducerFactory, KafkaTemplate, abstraction, transactionIdPrefix, autoconfigured, producer lifecycle, pool, per-thread