Tuning Kafka for your goal: throughput, latency, durability, availability
No configuration maxes out all four goals: rank throughput, latency, durability and availability first (a financial system differs from an ingestion pipeline), then pick producer, consumer, topic and broker values against Kafka 4.x defaults; plus Spring Boot, the auto-commit trap, CooperativeSticky and KIP-848 and how to review AI-written config.
On this page
1. The problem
An e-wallet (~2,000 transactions/s) and a clickstream (150,000 events/s) share a cluster and one configuration. A rolling restart of one broker stops the clickstream from accepting writes; the next week a wallet pod crashes with auto-commit and thread-pool processing, losing about 10,000 transactions.
2. Key idea
The four goals pull the knobs in different directions. Raising one costs another. ISR is the set of replicas keeping up with the leader; unclean election lets a lagging replica become leader (and can lose acknowledged records).
| Goal | Knobs that favor it | Cost |
|---|---|---|
| Throughput | large linger.ms, large batch.size, compression, more partitions |
higher latency |
| Latency | linger.ms=0, fetch.min.bytes=1 |
many small requests |
| Durability | acks=all, RF=3, min.insync.replicas=2, idempotence, unclean off |
lower availability |
| Availability | low min ISR, unclean election on | records can be lost |
2.1 Which profile for each flow?
| Profile | Use when | Don't use when |
|---|---|---|
| Durability | money, orders, ledgers | you accept loss to keep accepting writes |
Throughput: linger.ms 20–50, zstd |
logs, clickstream, warehouse loads | each message must arrive within a few ms |
| Latency | notifications, interactive flows | heavy load |
| Availability: min ISR 1, unclean on | metrics, logs where loss is fine | data you've already told a user "succeeded" |
Rule: one profile per flow; broker defaults follow the strictest flow, overridden per topic; change one knob at a time and measure.
3. In Spring Boot
A wallet service that puts durability first:
spring.kafka:
producer:
acks: all
compression-type: lz4
properties:
enable.idempotence: true
linger.ms: 10
delivery.timeout.ms: 60000 # must be ≥ linger.ms + request.timeout.ms
consumer:
group-id: wallet-balance
enable-auto-commit: false
properties:
partition.assignment.strategy: org.apache.kafka.clients.consumer.CooperativeStickyAssignor
listener:
ack-mode: record # commit after each processed record
concurrency: 3 # total consumers shouldn't exceed partitionsAdd a DefaultErrorHandler with a DeadLetterPublishingRecoverer: without a DLT (dead letter topic) a failing record is retried 9 times and then dropped. KafkaTemplate.send() is async: catch errors in whenComplete, not try/catch. Other languages: librdkafka clients (Go, .NET, Python) use nearly the same keys but defaults may differ.
Consumers: offset commits, rebalancing and KIP-848
- Auto-commit commits periodically during
poll(). With synchronous processing a crash causes duplicates; with processing on another thread a crash causes loss. Commit after processing, and make the DB write idempotent (unique by event id). - Eager rebalancing stops the whole group; CooperativeSticky moves only the partitions that need to move.
- KIP-848 (Kafka 4.0): the broker coordinates rebalancing, enabled with
group.protocol=consumer. Once on, the client no longer usessession.timeout.ms,heartbeat.interval.ms,partition.assignment.strategy. - For important topics, check RF,
min.insync.replicasand unclean withkafka-configs --describe, not by trusting a description.
4. Working with AI
Hand to AI: a docker compose with 3 KRaft brokers, kafka-producer-perf-test scripts over several configs, a table summarizing every topic's config.
Check yourself in AI-written code:
- Producers writing important topics have
acks=alland idempotence. -
delivery.timeout.ms≥linger.ms+request.timeout.ms. - No auto-commit combined with
@Async, a thread pool orCompletableFuturein the listener. - There's an error handler with a DLT and bounded retries.
- Any default the AI mentions matches the
kafka-clientsversion inpom.xml.
5. Exercises
Exercise 1: Review a "never lose a message" config
The AI wrote the config below for the wallet service, described as "durable, fails fast". The listener calls executor.submit(() -> balances.apply(event)) and returns immediately. Find the bugs and fix them.
producer:
acks: all
properties:
enable.idempotence: true
linger.ms: 10
delivery.timeout.ms: 30000
consumer:
enable-auto-commit: trueSolution 1
delivery.timeout.ms=30000is less thanlinger.ms+request.timeout.ms(10 + 30,000): the producer throwsConfigExceptionat startup. To fail fast, lower both, e.g.request.timeout.ms=10000,delivery.timeout.ms=15000.- Auto-commit plus an executor: the offset commits before
applyfinishes, so a pod crash loses it. Turn auto-commit off, process synchronously,ack-mode: record, idempotent DB writes. - No DLT: failing records are dropped after 9 retries.
- "Durable" also depends on the topic: RF 3,
min.insync.replicas=2, uncleanfalse.
Exercise 2: Read a broker-loss incident
A 3-broker cluster with two RF=3 topics: payments (min.insync.replicas=2) and strict (min.insync.replicas=3). One broker stops. Service A (acks=all) reports timeouts after 2 minutes; service B (acks=1) keeps writing. Which topic is affected, what's the real error, and why does the app only see a timeout?
Solution 2
paymentsstill has 2 replicas in ISR: writes work with anyacks.strictneeds 3:acks=allfails. The broker returnsNotEnoughReplicasException, the producer retries automatically, so the app only seesTimeoutException: Expiring 1 record(s)afterdelivery.timeout.ms.- Right now: restart the broker. Long term: min ISR 3 with RF 3 stops writes on every rolling restart; lower it to 2.
- B "still writing" isn't safe:
acks=1ignores min ISR.
Summary
- Rank the four goals per flow before picking
acks, min ISR,linger.ms, compression. - Durability and availability pull against each other through
min.insync.replicasand unclean election. - Auto-commit duplicates with sync processing and loses with async; commit after an idempotent write.
- Spring Kafka needs an error handler with a DLT.
- Defaults change between versions: set them explicitly, override per topic.
Further reading: Why Kafka is fast and durable.
Practice interview questions on this topic
- How do you choose the partition count for a new topic, and should you just create lots of partitions to be safe?
- How do you commit Kafka offsets correctly: is auto-commit safe, and when does auto.offset.reset apply?
- How does KafkaProducer.send() work internally: async sends, batching, linger.ms, compression and timeouts?
- What are the ISR, high watermark and unclean leader election, and why is Kafka durable without fsync before ACK?
- How do heartbeats, session.timeout.ms and max.poll.interval.ms differ, and how do you handle records that take minutes?
- What is consumer lag, how do you measure it, and should you add consumers as soon as lag grows?
Comments
Community rules