producer.send() does not send anything. It puts a record into an in-memory buffer and returns a future. A background I/O thread does the actual network work. Almost every producer surprise comes from forgetting this.
The path a record takes
send(record)
│
▼
serializer ── key and value → bytes
│
▼
partitioner ── choose target partition
│
▼
record accumulator (in-memory buffer, one batch per partition)
│ waits until batch.size is full OR linger.ms elapses
▼
sender thread ── one request may carry batches for many partitions
│
▼
partition leader broker ── append, replicate, acknowledge
│
▼
callback / future completes
Why batching is not optional
Per-record network round trips waste both network and broker CPU. Kafka's throughput assumes batches. Two settings control them:
| Setting | Meaning | Practical note |
|---|---|---|
batch.size | Max bytes per partition batch | An upper bound, not a wait target |
linger.ms | How long to wait for a batch to fill | Default 0 means send as soon as the sender thread is free |
With linger.ms=0 you still get batching under load — records accumulate while the previous request is in flight — but you get almost none at low or bursty rates. Setting linger.ms to 5-20 ms typically trades a small, bounded latency increase for a large drop in request count. Measure it in your environment rather than copying a number.
Compression (compression.type) is applied per batch, so bigger batches compress better. lz4 and snappy favour CPU; zstd and gzip favour ratio.
Buffer pressure and blocking
The accumulator is bounded by buffer.memory. If the broker is slow or unreachable and the buffer fills, send() blocks for up to max.block.ms and then throws. This is the mechanism by which a Kafka problem becomes your service's problem, so decide deliberately: block and apply backpressure, or fail fast and shed load. Silence is not one of the options.
Fire-and-forget is a data-loss decision
These three styles are not stylistic preferences:
// 1. Fire and forget — you will never know it failed
producer.send(record);
// 2. Asynchronous with callback — recommended default
producer.send(record, (meta, ex) -> { if (ex != null) handle(ex); });
// 3. Synchronous — blocks until acknowledged, kills throughput
producer.send(record).get();
Style 1 discards exceptions after retries are exhausted. Style 3 reduces you to one in-flight record at a time; reserve it for low-volume, must-not-proceed-without-it writes.
Ordering inside the callback path
Callbacks for a single partition fire in offset order, on the producer's I/O thread. Two rules follow: do not do blocking work in a callback, and do not assume callbacks across partitions are ordered.
Producer lifecycle
A KafkaProducer is thread-safe and expensive to create. Create one per application and share it. Closing it flushes buffered records — a producer that is never closed, or a JVM killed without a shutdown hook, loses whatever was still buffered.
Exercise: A service sends one record per HTTP request with linger.ms=0, acks=all, and send(...).get(). Throughput is far below expectation and p99 latency tracks broker round-trip time exactly. Identify the three separate causes and say which change you would make first, and why that ordering matters.