SpringSpring Kafka Configuration
What is the difference between ProducerFactory and KafkaTemplate in Spring Kafka?
Reference answer
ProducerFactory creates the Kafka Producer instances (the underlying Kafka client); DefaultKafkaProducerFactory is the standard implementation. Without transactions it by default creates a singleton producer shared by all clients, as recommended in the KafkaProducer Javadocs; the producerPerThread property makes it create and cache one producer per thread (then you must call closeThreadBoundProducer()). KafkaTemplate is the higher-level class that uses the ProducerFactory to send records: its send methods (topic and data, ProducerRecord, or Message) return a CompletableFuture<SendResult>. Transactions are enabled by giving the DefaultKafkaProducerFactory a transactionIdPrefix; the factory then keeps a cache of transactional producers whose transactional.id is the prefix plus a number, and a transactional KafkaTemplate must run inside a transaction unless allowNonTransactional is set. Spring Boot auto-configures a KafkaTemplate you can autowire. Most applications inject only KafkaTemplate and configure the factory through properties.
Expected key concepts: ProducerFactory, KafkaProducer, DefaultKafkaProducerFactory, KafkaTemplate, singleton producer, producerPerThread, transactionIdPrefix, CompletableFuture, auto-configures, transactional.id
How did that feel?
Your own verdict, not the score. It shows up next to this question in the list, so you can come back to the shaky ones.