KafkaProducer - send
This content was put together with KafkaProducer Client Internals and GPT.
A Producer provides a single-message publishing API called send(), but internally it batches multiple messages together before sending them to the broker.
var future = kafkaTemplate.send(
"credit-event",
orderId,
event
);- Checks topic and partition metadata
- Serializes the key and value into bytes
- Determines the destination partition
- Adds the record to the batch for that partition in the RecordAccumulator
- Returns a Future that can be used to check the publishing result
-> The fact that send has returned does not mean the record has been written to the broker.
It adds the record to an internal buffer and returns; the Sender performs the actual network transmission.
Partition Selection Policy
- Partition specified directly: Uses the specified partition
- No partition specified, key provided: Uses the hash of the serialized key
- No partition specified, no key: Collects records using the Sticky approach, then selects a partition
Sticky: Rather than choosing a different partition each time, as with Round-Robin, it collects records up to a certain amount and sends them to one partition to improve batching efficiency.
RecordAccumulator
Manages batches for each TopicPartition (topic name, partition number) pair.
- For a new record, checks the last batch for that partition
- If there is space, adds the record to the existing batch
- If there is not enough space, creates a new batch
- Multiple batches can be waiting for a single partition
BufferPool manages the memory used to hold batches
- `[A, B, C]` is written to the `credit-event-0` batch
- `[F, G]` is written to the `client-erasure-0` batchFuture
send returns a Future that can be used to check the publishing result for that record.
var futureA = kafkaTemplate.send(topic, keyA, eventA);
var futureB = kafkaTemplate.send(topic, keyB, eventB);get(5, SECONDS)- Success: Returns the publishing result
- Failure: Reports the failure as an exception
- Not complete: Waits for the result
- Still incomplete after 5 seconds: Ends the caller's wait with a timeout
- This is not a
flush; it does not force an immediate send
Does
send().get()cause batch transmission?
-> It can. If multiple request threads share a Producer, records for the same partition that accumulate before transmission can be sent in the same batch.
When Are Batches Sent?
Two settings generally determine when a batch is ready to be sent:
-> batch.size and linger.ms.
batch.size: The size threshold, in bytes, for accumulating a batch per partitionlinger.ms: The additional wait time allowed to accumulate more records in a batch
The order is as follows:
- If the batch fills first (
batch.size), it does not wait for thelinger.msdelay - Even if the batch is not full, it becomes ready for transmission once the
linger.msdelay expires
=> Ready for transmission and Transmission completed are different things!! ⭐
Transmission may be delayed further depending on factors such as the broker connection, the number of requests in flight, and the broker's processing speed.
In this context, even if
linger.msis 0, multiple records can accumulate before the Sender picks them up and send as a batch.
Sender Thread
Fetches batches that are ready for transmission, sends them to the broker, and processes the responses.
max.in.flight.requests.per.connection: The number of requests that can be in progress on a broker connection without having received a response- This does not mean the number of messages or batches
- A single request can contain multiple batches and records
Partition Batches -> Broker Requests
The RecordAccumulator manages batches by TopicPartition.
When sending, the Sender checks the leader broker for each partition and puts batches destined for the same broker into a single request.
| Partition batch | Records included | Leader broker |
|---|---|---|
credit-event-0 | A, B, C | Broker 1 |
client-erasure-0 | F, G | Broker 1 |
credit-event-1 | D, E | Broker 2 |
The request sent to Broker 1 contains credit-event-0 and client-erasure-0; the request sent to Broker 2 contains credit-event-1.
send Can Also Wait
Although this is an asynchronous publishing API, the call itself does not always return immediately.
- If topic or partition metadata is unavailable, it waits for that information — API call
- If there is not enough buffer space for a batch, it waits for memory to become available — BufferPool allocation
max.block.ms limits the total time spent waiting.