Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Kafka configure number of partitions for topic

Is it possible in Spring Kafka configure the number of partitions for the specific topic in order to be able to effectively use org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory.setConcurrency(Integer) method to parallel consumers on this topic in order to speed up message consumptions and processing? If so, could you please show the example of how it can be done.

like image 773
alexanoid Avatar asked Aug 03 '18 07:08

alexanoid


People also ask

How do I increase the number of partitions in a Kafka topic?

If you want to change the number of partitions or replicas of your Kafka topic, you can use a streaming transformation to automatically stream all of the messages from the original topic into a new Kafka topic that has the desired number of partitions or replicas.

How many partitions should I have for a topic in Kafka?

For most implementations you want to follow the rule of thumb of 10 partitions per topic, and 10,000 partitions per Kafka cluster. Going beyond that amount can require additional monitoring and optimization. (You can learn more about Kafka monitoring here.)

How do you choose the number of partitions in a topic?

A rough formula for picking the number of partitions is based on throughput. You measure the throughout that you can achieve on a single partition for production (call it p) and consumption (call it c). Let's say your target throughput is t. Then you need to have at least max(t/p, t/c) partitions.

Can we reduce the number of partitions in Kafka topic?

No. If you want to use fewer partitions, delete the corresponding topic, create another one, and specify the desired number of partitions.


1 Answers

See Configuring Topics.

@Bean
public NewTopic topic1() {
    return new NewTopic("foo", 10, (short) 2);
}

Will create a topic foo with 10 partitions and a replication factor of 2 (if there is a KafkaAdmin bean in the application context).

Spring boot auto-configures a KafkaAdmin @Bean.

like image 146
Gary Russell Avatar answered Dec 08 '22 17:12

Gary Russell