Most "Kafka is slow" tickets we see on the write path turn out to be a producer sending many tiny requests. The brokers are idle, the network is quiet, and each record pays for its own round trip. This note covers the settings that control batching, how they trade latency for throughput, and how to confirm a change helped.
How the producer sends data
The producer does not send each send() call individually. Records are appended to a per-partition batch in a memory buffer. A background sender thread ships ready batches, grouped by destination broker, in a single produce request. A batch is ready when it is full or when it has waited long enough.
Three settings define that behaviour:
batch.sizeis the maximum size in bytes of a batch for one partition. The default is 16384 (16 KiB).linger.msis how long the producer waits for more records before sending a batch that is not yet full. Older clients default to 0; Kafka 4.0 changed the default to 5 ms (KIP-1030). Check the version you actually run.buffer.memoryis the total memory for unsent records, 32 MiB by default.
With linger.ms=0 a batch still fills while a previous request is in flight, so busy producers batch somewhat anyway. A lightly loaded producer sends nearly one record per request.
Step 1: measure before changing anything
The producer exposes the signals you need through JMX or your client's metrics registry:
batch-size-avgagainstbatch.size: if the average is a small fraction of the limit, batches are leaving mostly empty.records-per-request-avg: close to 1 means no effective batching.record-queue-time-avg: how long records sit in the buffer. This is the latencylinger.msadds.compression-rate-avg: compressed size over uncompressed size. Lower is better.buffer-available-bytesandbufferpool-wait-time-ratio: whether the application is blocking on a full buffer.request-latency-avg: broker-side round-trip, which separates a slow broker from a slow client.
Record these for a representative hour first. Without a baseline you cannot say whether a change helped.
Step 2: raise linger.ms in small steps
If records-per-request-avg is low, raise linger.ms to 5, then 10, then 20, and re-measure. Each step adds at most that much latency to a record that arrives at an otherwise idle producer. For a stream carrying thousands of records per second per partition, batches fill before the linger expires, so the added latency is smaller than the setting suggests. For a low-volume producer, the setting is the latency.
The trade-off to state honestly: if an end-to-end budget is a few milliseconds, linger is not free. Tune it against the budget, not toward the biggest batch.
Step 3: size batches for the traffic you have
If batch-size-avg sits near batch.size and linger.ms is rarely the trigger, batches are filling to the limit and a larger batch.size (64 KiB or 128 KiB) can cut request count further. If batches are mostly empty, increasing batch.size does nothing except reserve more memory per partition. Note that memory is allocated per active partition, so a producer writing to thousands of partitions needs buffer.memory sized accordingly.
Partition count also affects batching: the same record rate spread over more partitions produces smaller batches. The default partitioner for keyless records sticks to one partition per batch (KIP-480, refined by KIP-794) to keep batches large. Keyed records follow the key hash and cannot be redirected.
Step 4: turn on compression
Compression is applied per batch, so it works better with larger batches. Set compression.type on the producer; the default is none. Options are gzip, snappy, lz4, and zstd.
lz4is a low-CPU choice with a moderate ratio.zstdgenerally gives a better ratio at a higher CPU cost, and tends to pay off on JSON and other text-heavy payloads.gzipis rarely the right choice for new work.
Watch compression-rate-avg and producer CPU. Keep the broker or topic-level compression.type at producer (the default) so brokers store batches as received instead of recompressing them. Setting a different codec on the broker forces recompression and spends broker CPU. Compression also reduces replication and cross-AZ traffic, which on cloud estates is often the larger saving.
Compressing already-compressed or encrypted payloads, such as pre-compressed images or Avro with compressed blocks, gains little.
Step 5: set timeouts so failures are bounded
Batching settings interact with timeouts, and it is common to set them inconsistently.
delivery.timeout.ms(default 120000) bounds the total time fromsend()to success or failure, including linger, retries, and in-flight time. It must be at leastlinger.ms + request.timeout.ms.request.timeout.ms(default 30000) bounds a single request.max.block.ms(default 60000) bounds how longsend()blocks when the buffer is full or metadata is missing.
Decide what the application should do when the buffer is full. Blocking for 60 seconds inside a request handler is rarely intended. Lower max.block.ms and handle the exception explicitly.
Keep the durability settings in mind while tuning for speed. Since Kafka 3.0 the producer defaults to acks=all and enable.idempotence=true, which is safe and permits up to five in-flight requests per connection while preserving order. Do not lower acks to buy throughput without a documented reason; see our note on where Kafka actually loses messages.
Step 6: change one thing, then compare
A workable procedure:
- Capture the baseline metrics above for a representative period.
- Change
linger.msonly. Comparerecords-per-request-avg,record-queue-time-avg, and end-to-end latency. - Change
batch.sizeonly if batches are hitting the limit. - Enable compression and compare
compression-rate-avg, producer CPU, and broker network throughput. - Roll out to one producer instance first, then the fleet.
If produce request latency on the broker side is high in the baseline, stop: the problem is on the broker (disk, replication, request handler saturation), and client tuning will not fix it.
What this does not fix
Producer tuning raises efficiency on a healthy cluster. It does not fix under-replicated partitions, a hot partition from a skewed key, or a consumer that cannot keep up. Those need broker metrics, key analysis, and consumer-group work.
If you would like a second pair of eyes on a producer fleet or a latency budget, describe the cluster and the symptom through our contact page and we will propose a first step.