Kafka: Producer batching trades latency for throughput

linger.ms waits for more records before sending; batch.size caps how large a batch may get.

Code
props.put(ProducerConfig.LINGER_MS_CONFIG, 20);        // wait up to 20ms
props.put(ProducerConfig.BATCH_SIZE_CONFIG, 64 * 1024);
props.put(ProducerConfig.COMPRESSION_TYPE_CONFIG, "lz4");
Output
linger.ms=0  : lowest latency, one request per record
linger.ms=20 : far fewer requests, 20ms worse tail latency
Compression works on the batch, so bigger batches compress better.
Advertisement

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-11