Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

KafkaProducer is designed to be thread-safe and we encourage users to share a single producer instance across multiple threads [link]. This approach is effective for scenarios where numerous threads append records infrequently and it helps to avoid creating many producers on resource-limited systems. These threads often need to send records to different topics, which is supported by the current producer implementation. However, another important configuration—acks—is currently set at the producer level only. 

This limitation prevents users from reusing KafkaProducer instance when different partitions require distinct asks settings, forcing them to create separate producers for each configuration. Furthermore, this capability has always been available at the RPC-level [link], and with this update, we're simply bringing it back into users' hands.

...

We only add a new field into ProducerRecord constructor and its getter.


Code Block
languagejava
titleProducerRecord
linenumberstrue
public ProducerRecord(String topic, Integer partition, Long timestamp, K key, V value, Iterable<Header> headers, Short acks) {
        if (topic == null)
            throw new IllegalArgumentException("Topic cannot be null.");
        if (timestamp != null && timestamp < 0)
            throw new IllegalArgumentException(
                    String.format("Invalid timestamp: %d. Timestamp should always be non-negative or null.", timestamp));
        if (partition != null && partition < 0)
            throw new IllegalArgumentException(
                    String.format("Invalid partition: %d. Partition number should always be non-negative or null.", partition));
        this.topic = topic;
        this.partition = partition;
        this.key = key;
        this.value = value;
        this.timestamp = timestamp;
        this.headers = new RecordHeaders(headers);
		this.acks = acks; // new field
    }

	public Short acks() {
		return acks;
	}
}


Proposed Changes

  • The proposed change is straightforward, as the ProduceRequest already contains an acks field. All we need is to add a new field to the ProduceRecord constructor. This new field will be null by default, and the producer-level acks setting will be used unless users specified otherwise.

...