Quick revision
The short answer
The biggest change is to stop blocking on every send: handle the CompletableFuture that KafkaTemplate returns asynchronously and check each result for failure. Then let the producer batch, by raising linger.ms and batch.size so many small events share one request (the default linger.ms changed in Kafka 4, so check your version), and enable lz4 or zstd compression to cut network and disk use for some CPU. With idempotence enabled, up to 5 in-flight requests per connection keep ordering, and because batching adds latency per record, we measure end-to-end latency, not only throughput.
What to remember
In this answer, we raised producer throughput by first removing the blocking get() on every send and handling each CompletableFuture asynchronously with real failure handling. We then let the producer batch with linger.ms and batch.size, noting the Kafka 4 change to the linger.ms default, added lz4 or zstd compression for some CPU cost, and kept up to 5 in-flight requests with idempotence to preserve ordering. Because batching adds latency, we measure end-to-end latency percentiles as well as throughput.
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.
The biggest change is to stop blocking on every send: handle the CompletableFuture that KafkaTemplate returns asynchronously and check each result for failure. Then let the producer batch, by raising linger.ms and batch.size so many small events share one request (the default linger.ms changed in Kafka 4, so check your version), and enable lz4 or zstd compression to cut network and disk use for some CPU. With idempotence enabled, up to 5 in-flight requests per connection keep ordering, and because batching adds latency per record, we measure end-to-end latency, not only throughput.
Why this question is asked
A producer that sends one small JSON event and waits for the broker's answer before sending the next is limited by round trips, not by Kafka. If a round trip takes 2 ms, the ceiling is about 500 events per second per thread, however powerful the cluster is. Interviewers ask this to see whether we know where producer throughput comes from and what each setting costs. In this answer, we will see why blocking is the bottleneck, move to asynchronous sends with proper error handling, then tune batching, compression and in-flight requests, and finish with how to measure the result.
1Why Blocking on Each Send Is the Bottleneck
KafkaTemplate.send() does not send anything over the network by itself. It hands the record to the producer's accumulator, which groups records into batches per partition, and a background sender thread sends those batches to brokers. Calling get() on the returned future makes our thread wait until the broker has acknowledged this one record. With acks=all, that includes replication to followers.
Notice the side effect: because only one record is ever waiting, every batch contains exactly one record, so we pay the full per-request overhead, headers, a network round trip and a log append, for each 300-byte event. The producer's batching machinery never gets a chance to work.
2Sending Asynchronously and Checking Failures
The fix is to let send() return immediately and handle the result in a callback. The sender thread can then pack many records into each request while our thread keeps producing.
The catch is that a failure no longer throws in our loop, so it is easy to lose it. Every future must have a callback that does something real with an exception: retry later, store the event, or mark the originating request as failed. Spring Kafka also lets us register a ProducerListener on the template, which is called for every success and failure, a convenient central place for logging and metrics. If the service must know all events were delivered before replying, it can collect the futures and wait for all of them with CompletableFuture.allOf, which still allows batching because all records were handed over first.
3Batching: linger.ms and batch.size
Two settings control batching. batch.size, 16,384 bytes by default, is the maximum size of one batch per partition. linger.ms is how long the producer waits for more records before sending a batch that is not yet full. With linger.ms at 0, a batch is sent as soon as the sender thread is free, so under light load batches stay small. Kafka 4 changed the default linger.ms from 0 to 5 ms, because a few milliseconds of waiting usually improves both throughput and efficiency; check the default in your client version.
For small events at high volume, a common starting point is linger.ms between 5 and 20 and batch.size of 64 to 256 KB. With 300-byte events, a 64 KB batch holds about 200 records, so one request replaces 200. The cost is latency: each record may wait up to linger.ms before it is sent.
| Change | Gain | Cost |
|---|---|---|
Async send instead of get() | many records per request | failures must be handled in callbacks |
linger.ms 10 | fuller batches, fewer requests | up to 10 ms extra latency per record |
batch.size 64 KB | more records per request | more producer memory per partition |
| lz4 or zstd compression | less network and disk | CPU on producer and consumer |
| in-flight requests up to 5 | pipelined requests | needs idempotence to keep order |
4Compression
JSON is repetitive: the same field names in every event. Compression works on whole batches, so bigger batches compress better. lz4 is fast with good ratios and a common default choice; zstd usually compresses more for a little more CPU. The batch stays compressed on the broker, in replication and on disk, and is decompressed only by consumers, so compression saves network bandwidth, broker disk and cross-zone transfer at once. Ratios of 3 to 5 times for JSON are typical, but we should measure with our own data.
5In-Flight Requests and Ordering
max.in.flight.requests.per.connection is how many requests the producer may send to one broker without waiting for responses, 5 by default. More than one in flight lets requests pipeline. Without idempotence, a failed and retried request could land after a later one, reordering records. With idempotence enabled, which is the default since Kafka 3.0, the broker uses sequence numbers to keep the order within a partition for up to 5 in-flight requests. So we keep idempotence on and the value at 5 or less; raising it above 5 silently disables idempotence.
6Measuring the Result
Throughput is easy to measure and easy to over-tune. Higher linger.ms always looks better in a throughput benchmark and can still break a latency target. So we measure end-to-end latency, from the moment the business event happens to the moment the consumer processes it, as percentiles, alongside throughput. The producer exposes metrics such as batch-size-avg, records-per-request-avg, record-queue-time-avg, compression-rate-avg and request-latency-avg, which show whether batching actually happens and how long records wait.
A good process is to change one setting at a time under realistic load, for example the kafka-producer-perf-test tool or a replay of production traffic, and stop when the latency percentile we promised starts to move.
- Remove blocking get() callshandle futures in callbacks
- Set linger.ms and batch.sizefor example 10 ms and 64 KB
- Enable lz4 or zstd
- Measure throughput and p99 end-to-end latency
- Latency target still met?YesKeep, try the next stepNoBack off the last change
5 steps in order. Play them, or step through them one at a time.
7Follow-up questions
How do you make sure failed async sends are not silently lost?
Every send gets a callback, or the template gets a ProducerListener, that handles failures explicitly by storing the event in a retry table, writing it to a dead letter location, or failing the original request, and that increments a metric we alert on, together with the producer's record-error-rate. Before shutdown, we call flush() so records still in the buffer are sent, and Spring does this when the producer factory is destroyed. For events that must never be lost, the strongest pattern is a transactional outbox, where the event is committed to the database first and the relay re-sends until Kafka acknowledges it.
Does acks=all limit throughput?
It adds replication time to each request, which mostly affects latency. With batching and several in-flight requests, throughput with acks=all is usually close to acks=1, so we keep acks=all for important data and tune batching instead.
8Conclusion
In this answer, we raised producer throughput by first removing the blocking get() on every send and handling each CompletableFuture asynchronously with real failure handling. We then let the producer batch with linger.ms and batch.size, noting the Kafka 4 change to the linger.ms default, added lz4 or zstd compression for some CPU cost, and kept up to 5 in-flight requests with idempotence to preserve ordering. Because batching adds latency, we measure end-to-end latency percentiles as well as throughput.
Open with
The main problem is blocking on each send. KafkaTemplate send only hands the record to the producer's buffer; calling get waits for the broker's acknowledgement, so each thread does one round trip per event, maybe 500 events a second, and every batch holds a single record.
Then, if they want more
First I would make sends asynchronous and handle the returned CompletableFuture in whenComplete, or with a ProducerListener on the template, so every failure is stored, retried or reported and counted in a metric. If a request needs all its events delivered, I collect the futures and wait with allOf after sending them all.
Then I would tune batching: linger.ms around 5 to 20 milliseconds and batch.size around 64 KB, so hundreds of small JSON events share one request. Kafka 4 changed the default linger.ms to 5, so I would check the client version.
I would enable lz4 or zstd compression, which works on whole batches and cuts network, replication and disk for some CPU. I keep idempotence on, which is the default, so up to 5 in-flight requests per connection still preserve order per partition.
The cost of all this is latency, since a record may wait for its batch, so I measure end-to-end p99 latency together with throughput, change one setting at a time, and look at batch-size-avg and record-queue-time-avg to confirm batching is really happening.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Kafka 3.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, Spring for Apache Kafka reference
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 "A producer service needs higher throughput; it sends small JSON events one at a time...": https://scrivio-interview-library.netlify.app/spring-boot/kafka/2d9392cf8c/