tulios / kafkajs

A modern Apache Kafka client for node.js
https://kafka.js.org
MIT License
3.75k stars 527 forks source link

Provide function on producer that returns partition for message key of topic #1690

Closed timsauvageot closed 5 months ago

timsauvageot commented 5 months ago

Is your feature request related to a problem? Please describe. Our application maintains cached state for entities. We load that state when the first message arrives for an entity. The entity id is used as partition key to ensure that only one server (consumer within group) handles messages for that entity. When another consumer receives the partition we would like to remove the cached state from the server that was previously responsible for the partition. Problem: To calculate the partition we'd have to copy & paste the current implementation from default producer.

Describe the solution you'd like It would be great if the producer would expose a function such as below calculatePartition(topic:string, key: string | Buffer) : number. The function could throw an error if the producer doesn't have the metadata available to calculate the partition.

Additional context