SteelReed

Search

Search posts, questions…

7. System Design

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.

18 Nov 20267 min readSenior

–

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:

YAML
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 partitions

Add 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 uses session.timeout.ms, heartbeat.interval.ms, partition.assignment.strategy.
  • For important topics, check RF, min.insync.replicas and unclean with kafka-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=all and idempotence.
  • delivery.timeout.ms ≥ linger.ms + request.timeout.ms.
  • No auto-commit combined with @Async, a thread pool or CompletableFuture in the listener.
  • There's an error handler with a DLT and bounded retries.
  • Any default the AI mentions matches the kafka-clients version in pom.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.

YAML
producer:
  acks: all
  properties:
    enable.idempotence: true
    linger.ms: 10
    delivery.timeout.ms: 30000
consumer:
  enable-auto-commit: true
Solution 1
  • delivery.timeout.ms=30000 is less than linger.ms + request.timeout.ms (10 + 30,000): the producer throws ConfigException at 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 apply finishes, 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, unclean false.

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
  • payments still has 2 replicas in ISR: writes work with any acks.
  • strict needs 3: acks=all fails. The broker returns NotEnoughReplicasException, the producer retries automatically, so the app only sees TimeoutException: Expiring 1 record(s) after delivery.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=1 ignores 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.replicas and 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

All Messaging & Kafka questions →

Loading comments…
Next on the roadmapInside the Kafka producer: send(), batching, the partitioner and transactions