liveBook · Manning

Batch size

As explained in Apache Kafka in Action

A batch size is the maximum amount of record data grouped into a producer batch before transmission.

A producer sending 10 KB records may get little benefit from Kafka’s default 16 KB batches. Sending more records together can reduce network requests, but the right size depends on the message rate and the latency the application can tolerate. section 6.2.1 section 6.2.2

The producer setting `batch.size` limits a batch’s size in bytes; its default is 16,384 bytes. The producer buffers records by topic and partition, forms batches, and sends them to the appropriate brokers. `linger.ms` sets how long it may wait for more records. Even when that wait is zero, records can be batched if they arrive faster than individual sends complete. section 6.2.1 section 8.1.2

Figure 6.5 The producer batches messages for each topic and partition. A message is sent as soon as either the batch is full or the linger time is up.
A group of colorful squares

AI-generated content may be incorrect.

The diagram shows the two triggers for sending a partition’s batch: it reaches the size limit, or its linger time expires. section 6.2.1

Measure before and after tuning

A producer performance test with one million 10 KB records initially reported about 117 MB/s and 165 ms average latency. With `batch.size=100000` and `linger.ms=100`, it reported 437.55 MB/s and 64.61 ms. These are results from changing both settings together, not a guaranteed gain from batch size alone. section 6.2.2

$ kafka-producer-perf-test.sh \
    --topic performance-test \
    --num-records 1000000 \
    --record-size 10000 \
    --throughput -1 \
    --producer-props bootstrap.servers=localhost:9092 \
                     batch.size=100000 linger.ms=100
...
1000000 records sent, 45879.977978 records/sec (437.55 MB/sec),
64.61 ms avg latency, 279.00 ms max latency, 58 ms 50th,
101 ms 95th, 126 ms 99th, 178 ms 99.9th.

The runnable test passes both settings through `--producer-props` and reports throughput and latency for the tuned run. section 6.2.2

Larger batches generally improve throughput, while waiting to fill them can add latency. A too-small batch can also hurt latency when records pile up, as in the test above. Compare repeated tests using production-like records and conditions rather than adopting one benchmark’s values unchanged. section 6.2.1 section 6.2.2

How Apache Kafka in Action covers it

How deep it goes
Defines the byte limit and traces partition-based buffering through broker sends, without detailing buffer allocation internals.
Examples
Compares performance-test results for 10 KB records at default settings and with batch.size=100000 and linger.ms=100.
What it assumes
Introduces batching from producer message flow while assuming familiarity with topics, partitions, and brokers.
What you can do after
Apply it via kafka-producer-perf-test.sh and producer properties to compare throughput and latency under production-like conditions.
How it connects
Connects batch size to linger.ms, producer buffers, topic partitions, throughput, and latency.

Also explained in

Compare how 12 books explain batch size →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Apache Kafka in Action. The section links point to the parts of the book it draws on.