To create a custom Kafka producer configuration in Spring Boot, define a ProducerFactory bean with your producer properties (bootstrap servers, serializers, acks, idempotence, batching) and expose a KafkaTemplate built from it. For many applications the spring.kafka.producer.* properties are enough, and a Java configuration class is needed only when you want several producers, different serializers or programmatic control.
The examples use Spring Boot 3 and Spring for Apache Kafka 3.x, where KafkaTemplate.send returns a CompletableFuture rather than the old ListenableFuture.
Spring Boot manages the version of spring-kafka.
The simplest configuration lives in application.yml and Spring Boot builds the ProducerFactory and KafkaTemplate for you.
Use this approach first. Move to Java configuration when you need more than one producer or settings that properties cannot express.
| Property | Effect | Recommended starting point |
|---|---|---|
acks | How many replicas must confirm a write | all for durability |
enable.idempotence | Prevents duplicates caused by retries | true (default in modern Kafka clients) |
retries | Retry count for transient errors | Default is very large; limit with delivery.timeout.ms |
delivery.timeout.ms | Upper bound for send plus retries | 120000 (default) |
linger.ms | Wait time to fill a batch | 5 to 20 for throughput |
batch.size | Max bytes per partition batch | 16384 default; raise for throughput |
compression.type | Compress batches | lz4 or zstd |
max.in.flight.requests.per.connection | Unacknowledged requests | 5 or lower with idempotence |
Newer Kafka clients (3.0 and later) enable idempotence and acks=all by default, so check your client version before assuming you must set them. Setting them explicitly documents the intent.
Declaring your own ProducerFactory makes Spring Boot's auto-configured one back off for the same generic types. The bean names matter when you inject by type, so give each KafkaTemplate a distinct name if you have several.
send is asynchronous. In Spring Kafka 3 it returns a CompletableFuture<SendResult<K, V>>, so attach callbacks instead of blocking.
Always choose a message key that groups related events. Kafka preserves order only inside a partition, and the key hash decides the partition.
If one application sends different types, you can use one factory per type, or a single factory with JsonSerializer and a KafkaTemplate<String, Object>. For a custom format such as Avro or Protobuf, set the value serializer to the vendor class, for example the Confluent KafkaAvroSerializer, and add its schema.registry.url property.
To write your own serializer, implement org.apache.kafka.common.serialization.Serializer<T> and reference the class in VALUE_SERIALIZER_CLASS_CONFIG.
Declare topics as beans so Spring creates them on startup through KafkaAdmin.
For tests use @EmbeddedKafka from spring-kafka-test, or Testcontainers with the KafkaContainer, which runs a real broker. To see messages from the command line:
Properties are enough for one producer with standard settings. Java configuration is needed for several producers, different value types, or runtime-computed settings.
It makes the leader wait for all in-sync replicas, which is the strongest durability setting for the producer. Pair it with a topic min.insync.replicas of 2 and a replication factor of 3.
Call .get(timeout, unit) on the returned future, or join(). Blocking lowers throughput, so use it only when you need the confirmation before continuing.
JsonSerializer adds type headers containing Java class names. Set spring.json.add.type.headers=false for non-Spring consumers.
Start with spring.kafka.producer.* properties. When you need more control, define a ProducerFactory with an explicit property map and expose a KafkaTemplate. Use acks=all with idempotence for durability, tune linger.ms, batch.size and compression for throughput, choose message keys deliberately, and handle the CompletableFuture result of send.