This is an automated email from the ASF dual-hosted git repository.

chia7712 pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new 58d439a1aa9 KAFKA-14768 Add latency optimization guidance to 
KafkaProducer.send() (#20842)
58d439a1aa9 is described below

commit 58d439a1aa923a098e5ac3e167750219a71f36f9
Author: Jian <[email protected]>
AuthorDate: Thu Nov 13 01:59:12 2025 +0800

    KAFKA-14768 Add latency optimization guidance to KafkaProducer.send() 
(#20842)
    
    Background: There have been many similar issues discussed about send
    latency (block for first sent) in JIRA and KIPs. After diving deep into
    the code and weighing  the effort versus the reward, I think the most
    suitable approach might  be to simply add a guideline in the JavaDoc to
    help more people become  aware of it and understand possible solutions
    so that many persons can reduce work load for deep dive this similar
    issue.
    
    FYI:  The similar JIRAs https://issues.apache.org/jira/browse/KAFKA-6705
    https://issues.apache.org/jira/browse/KAFKA-3539
    https://issues.apache.org/jira/browse/KAFKA-14768
    
    Reviewers: Chia-Ping Tsai <[email protected]>
---
 .../java/org/apache/kafka/clients/producer/KafkaProducer.java | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git 
a/clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java 
b/clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java
index 6e656f590e4..9672b595e36 100644
--- a/clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java
+++ b/clients/src/main/java/org/apache/kafka/clients/producer/KafkaProducer.java
@@ -937,8 +937,14 @@ public class KafkaProducer<K, V> implements Producer<K, V> 
{
      * once the record has been stored in the buffer of records waiting to be 
sent.
      * This allows sending many records in parallel without blocking to wait 
for the response after each one.
      * Can block for the following cases: 1) For the first record being sent 
to 
-     * the cluster by this client for the given topic. In this case it will 
block for up to {@code max.block.ms} milliseconds if 
-     * Kafka cluster is unreachable; 2) Allocating a buffer if buffer pool 
doesn't have any free buffers.
+     * the cluster by this client for the given topic. In this case it will 
block for up to {@code max.block.ms} milliseconds 
+     * while waiting for topic's metadata if Kafka cluster is unreachable; 2) 
Allocating a buffer if buffer pool doesn't
+     * have any free buffers.
+     * <p>
+     * <b>Reducing first-send latency:</b> You can reduce the latency of the 
first send by preloading the metadata
+     * with {@link #partitionsFor(String)}. However, be aware that metadata 
cache will be cleared to free up resources after
+     * {@code metadata.max.idle.ms} of inactivity, so subsequent sends after a 
long idle period will still
+     * experience delays.
      * <p>
      * The result of the send is a {@link RecordMetadata} specifying the 
partition the record was sent to, the offset
      * it was assigned and the timestamp of the record. If the producer is 
configured with acks = 0, the {@link RecordMetadata}
@@ -1036,6 +1042,7 @@ public class KafkaProducer<K, V> implements Producer<K, 
V> {
      * @throws InterruptException     If the thread is interrupted while 
blocked
      * @throws SerializationException If the key or value are not valid 
objects given the configured serializers
      * @throws KafkaException         If a Kafka related error occurs that 
does not belong to the public API exceptions.
+     * @see #partitionsFor(String)
      */
     @Override
     public Future<RecordMetadata> send(ProducerRecord<K, V> record, Callback 
callback) {

Reply via email to