apache-kafkakafka-topickafka-partition

Do we need to know number of partitions for a topic beforehand?


We want to put messages/records of a different customers on different partitions of a kafka topic.

But number of customers is not known in prior. So how can we set partition count for kafka topic in this case? Do we need any other way where partition count changes at runtime based on keys (customer_id in this case). Thanks in advance.


Solution

  • need to know number of partitions

    Assuming Java, use AdminClient.describeTopics() method call and get partitions of each response object.


    Regarding the rest of the question, consumer instances automatically distribute partition assignment when subscribing to topics.

    Producers should not know about consumers, so you don't "put records on partitions" based on any factor of (possible) consumers.


    partition count changes at runtime based on keys (customer_id)

    Unclear what this means. Partition count can only increase, and if you do increase it, then your partitions will become unordered, so you should consider how large your keyspace is before creating the topic. For example, if you have a numeric ID, and use the first two digits as the partition value, then you could create a topic up to 100 partitions.