Video in progress

How do the producer's linger.ms and batch.size settings affect throughput?

The producer collects messages into per-partition batches before sending. batch.size is the maximum batch size (in bytes), and linger.ms is how long to wait for a batch to fill. Waiting a little makes batches bigger and raises throughput, but it increases the latency of each message.

📖 Answer

When you call send(), the message doesn't go to the network right away; it lands in the RecordAccumulator buffer. A separate sender thread ships batches when they're ready: either batch.size is full or linger.ms has passed.

spring:
  kafka:
    producer:
      batch-size: 65536        # 64 KB
      compression-type: lz4
      properties:
        linger.ms: 10

Up to Kafka 3.x the default was linger.ms=0; in Kafka 4.0 it changed to 5. So in newer versions the producer waits a little by itself.

Compression works at the batch level, so bigger batches compress much better. For JSON, lz4 or zstd saves network and disk several times over.

When it's worth thinking about: if you send tens of thousands of log or tracking events per second, linger.ms=10–50 and compression make a big difference. If low latency matters for every message (a payment confirmation, say), stay with small values.

If buffer.memory (32 MB by default) fills up, send() blocks for up to max.block.ms; that's another thing to monitor separately.