Quick revision
The short answer
send() serialises the record, picks its partition and appends it to that partition's open batch in the RecordAccumulator; a separate sender thread drains ready batches into one Produce request per broker. A batch is ready when it reaches batch.size (16 KB) or has waited linger.ms, which defaults to 5 ms in Kafka 4.0 clients. At about 167 records per second per partition, a record arrives every 6 ms or so, so most batches hold one or two records; with linger.ms=50 they hold about 8 records (2.4 KB), cutting requests several times for up to 50 ms of extra latency.
What to remember
In this answer, we followed a record through the Java producer: send() serialises it, picks a partition and appends it to that partition's batch in the RecordAccumulator, and the sender thread sends ready batches to each broker in one request. A batch is ready at batch.size or after linger.ms, now 5 ms by default. At about 167 records per second per partition, that gives one or two records per batch, while linger.ms=50 gives about eight, cutting requests several times for up to 50 ms of latency. buffer.memory caps unsent data, and send() blocks for up to max.block.ms when it is full.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
send() serialises the record, picks its partition and appends it to that partition's open batch in the RecordAccumulator; a separate sender thread drains ready batches into one Produce request per broker. A batch is ready when it reaches batch.size (16 KB) or has waited linger.ms, which defaults to 5 ms in Kafka 4.0 clients. At about 167 records per second per partition, a record arrives every 6 ms or so, so most batches hold one or two records; with linger.ms=50 they hold about 8 records (2.4 KB), cutting requests several times for up to 50 ms of extra latency.
Why this question is asked
Producer batching is the most important performance setting most teams never look at, and the numbers in this question show why: a perfectly ordinary service can send almost one request per record. Interviewers ask it to see whether we can follow a record through the producer and estimate batch sizes from traffic rather than guess. In this answer, we will follow a record from send() to the network, see when a batch is ready, calculate batch sizes with the default settings and with linger.ms=50, and finish with the memory limit that keeps all of this bounded.
1What send() Actually Does
KafkaProducer.send() does not send anything over the network. It runs on the application's thread and does three things: serialises the key and value into bytes, chooses a partition (for a keyed record, a hash of the key bytes modulo the partition count), and appends the record to the open batch for that partition in an in-memory structure called the RecordAccumulator. Then it returns a Future at once.
A separate background thread, the sender, loops continuously. It asks the accumulator which batches are ready, groups ready batches by the broker that leads their partition, and sends one Produce request per broker carrying all of them. When the response comes back, it completes the futures and runs the callbacks.
- Application thread: send()Serialise, choose partition, append to batch
- RecordAccumulatorOne queue of batches per partition, memory from
buffer.memory - Sender threadFinds ready batches, groups them by leader broker
- Network clientOne Produce request per broker, several batches inside
- BrokerAppends each batch to its partition and replies
5 layers, from the top down. Step through them one at a time.
2When Is a Batch Ready?
A batch becomes ready when either it is full, meaning adding the next record would exceed batch.size (16,384 bytes by default), or it has waited linger.ms since it was created. Kafka 4.0 changed the default linger.ms from 0 to 5 ms (KIP-1030), because a little waiting improves batching a lot for a small latency cost. Before 4.0, the default 0 meant 'send as soon as the sender is free', and batching happened only under load.
One detail makes real batches a little smaller than the simple model predicts: once some batch for a broker is ready, the sender also drains the other partitions' current batches for that same broker into the same request, even if they have not lingered fully. Requests stay efficient, but individual batches leave a bit early.
3Estimating the Batch Size With Defaults
Now the numbers. 2,000 records per second over 12 partitions is about 167 records per second per partition, one record roughly every 6 ms. Each record is 300 bytes, so a full 16 KB batch would need about 54 records, which would take over 300 ms to collect. The linger timer will always fire long before that.
With 5 ms of linger, a batch opens when a record arrives and closes 5 ms later, by which time another record has arrived perhaps half the time. So most batches hold one or two records, and the producer sends over a thousand tiny batches per second. With linger.ms=50, each partition's batch collects about 8 records, around 2.4 KB, and the number of batches falls to roughly 240 per second. The number of Produce requests falls in a similar ratio, which saves CPU on the producer and especially on the brokers.
The cost is latency: a record may now wait up to 50 ms in the accumulator before it is even sent. For most event pipelines that is fine; for a request-path service with a tight latency budget, it may not be. Notice also that compression works per batch, so bigger batches compress much better, another reason linger helps.
4The Memory Limit: buffer.memory and max.block.ms
All unsent batches live in memory, limited by buffer.memory (32 MB by default). If the brokers slow down or the application sends faster than the network can carry, the accumulator fills up. Then send() itself blocks the application thread, waiting for space, for up to max.block.ms (60 seconds by default), and throws a TimeoutException if no space appears.
In our service, 2,000 records of 300 bytes is only 600 KB per second, so 32 MB is far more than needed. But in a busy service, a blocked send() is a common surprise: it looks like the producer 'hangs' when the real cause is a slow broker or too little buffer for the traffic.
5Follow-up questions
Why does a larger batch.size alone not help this producer?
Because batches never get near the current 16 KB limit. With about 167 records per second per partition, the linger timer closes each batch after one or two records, long before it is full. Raising batch.size to 64 KB changes nothing unless linger.ms also rises; it only matters for partitions with enough traffic to fill batches before the linger time ends.
Would fewer partitions give bigger batches?
Yes. Each partition fills its own batch, so the same traffic over 6 partitions instead of 12 doubles the records per batch at the same linger. That is why topics with many partitions and little traffic batch badly. Keyless records also batch well, because the sticky partitioner sends to one partition until its batch is sent.
6Conclusion
In this answer, we followed a record through the Java producer: send() serialises it, picks a partition and appends it to that partition's batch in the RecordAccumulator, and the sender thread sends ready batches to each broker in one request. A batch is ready at batch.size or after linger.ms, now 5 ms by default. At about 167 records per second per partition, that gives one or two records per batch, while linger.ms=50 gives about eight, cutting requests several times for up to 50 ms of latency. buffer.memory caps unsent data, and send() blocks for up to max.block.ms when it is full.
Open with
send() doesn't touch the network. On the calling thread it serialises the record, picks the partition, for a keyed record murmur2 of the key bytes modulo the partition count, and appends it to that partition's open batch in the RecordAccumulator.
Then, if they want more
A background sender thread then picks up batches that are ready, groups them by leader broker, and sends one Produce request per broker. A batch is ready when it is full, at batch.size, 16 KB by default, or when it has waited linger.ms, which is 5 ms by default in the 4.0 client; before 4.0 it was 0.
Now the numbers: 2,000 records a second over 12 partitions is about 167 per partition, one every 6 ms. Filling 16 KB would take about 54 records of 300 bytes, which never happens, so linger decides. With 5 ms, most batches hold one or two records, so we send over a thousand tiny batches a second.
With linger.ms at 50, each batch collects about eight records, around 2.4 KB, about 240 batches a second, so several times fewer requests and better compression, at the cost of up to 50 ms more latency per record. Raising batch.size alone wouldn't help, since batches never fill.
Finally, buffer.memory, 32 MB by default, caps all unsent batches; when it's full, send() blocks for up to max.block.ms and then throws.
About this answer
- Last updated
- 13 November 2026
- Versions it names
- Kafka 4.0
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Apache Kafka documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain how the producer groups records into batches, roughly how many records each...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-clients-in-depth/6ff60c0596/