Sub topic 10
Reliable delivery: acks, retries and idempotence
This chapter assumes you can already publish with KafkaTemplate and know what partitions and offsets are. It covers the producer settings that decide whether a message survives broker failures, and what happens when the producer retries.
What can go wrong on send
A send can fail in three ways. The broker may never receive the record. The broker may store it but the acknowledgment may get lost. The leader broker may acknowledge it and then crash before followers copy it. Each producer setting in this chapter addresses one of these cases.
acks: who must confirm a write
The acks setting controls how many brokers must confirm a record before the send counts as successful.
| Value | Meaning | Risk |
|---|---|---|
| `0` | Do not wait for a reply | Silent loss |
| `1` | Wait for the partition leader only | Loss if the leader fails before replication |
| `all` | Wait for all in-sync replicas | Slowest, safest |
acks=all is only as strong as the number of in-sync replicas. The topic setting min.insync.replicas sets the minimum. If fewer replicas are in sync, the broker rejects the write instead of accepting it with weaker protection.
package com.example.orders;
import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.common.config.TopicConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.config.TopicBuilder;
@Configuration
public class DurableTopicConfig {
@Bean
public NewTopic durableOrdersTopic() {
return TopicBuilder.name("orders")
.partitions(3)
.replicas(3)
.config(TopicConfig.MIN_IN_SYNC_REPLICAS_CONFIG, "2")
.build();
}
}With replicas(3) and min.insync.replicas=2, a write survives the loss of one broker. A single-broker development setup, with replicas(1), gives acks=all no extra protection.
Retries and their side effect
The producer retries sends that fail with transient errors, such as a leader election. The delivery.timeout.ms setting bounds the total time spent on one record, including retries. The default is two minutes.
Retries cause duplicates. If the broker stored a record but the acknowledgment was lost, the retry stores it a second time.
Idempotence removes retry duplicates
With enable.idempotence=true, the broker assigns the producer an ID. Each record carries a sequence number per partition. The broker discards any record whose sequence number it has already seen. Retries then cannot create duplicates or reorder records within a partition.
spring:
kafka:
producer:
acks: all
properties:
enable.idempotence: true
delivery.timeout.ms: 120000
max.in.flight.requests.per.connection: 5Idempotence requires acks=all and allows up to five in-flight requests while keeping order. Recent Kafka clients enable both by default, but stating them explicitly documents your intent.
Check the send result
KafkaTemplate.send returns immediately. If you ignore the returned CompletableFuture, a failure after all retries disappears. Always attach a whenComplete callback, as OrderPublisher does, and decide what a failure means for your application: log it, store the event for later, or surface an error.
Common errors
- Assuming idempotence gives exactly-once end to end. It protects one producer session against retry duplicates. A restarted application gets a new producer ID, and consumers can still see a record twice after a rebalance. Make consumer processing safe to repeat, for example by keying database writes on
orderId. - Setting
acks=allwith one replica. No second copy exists, so nothing is gained. - Leaving
min.insync.replicas=1.acks=allthen behaves likeacks=1once followers fall behind. - Disabling retries to avoid duplicates. This trades duplicates for loss. Use idempotence instead.
Exercise
A topic has replicas=3 and min.insync.replicas=2. The producer uses acks=all. Two brokers go down. What happens to new sends to a partition, and why?
Show answer
Sends fail. Only one replica is in sync, which is below min.insync.replicas, so the broker rejects the write rather than accept it with weaker protection. The producer retries until delivery.timeout.ms expires, then the future from send completes with an error, which your whenComplete callback must handle.