Kafka
Spring Boot
Consumer Concurrency
Kafka Configuration
Programming

how to set kafka consumer concurrency using spring boot

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a popular distributed messaging system that provides high-throughput and fault-tolerant service used extensively for real-time streaming applications. Spring Boot simplifies Kafka integration through spring-kafka, a project which offers high-level abstractions for Kafka-based messaging solutions.

When building scalable Spring Boot applications that consume messages from Kafka topics, managing consumer concurrency is a fundamental aspect. This entails setting up multiple consumer instances in one or more consumer groups, thereby increasing throughput and improving performance by parallel processing of the messages.

Understanding Kafka Consumer Concurrency

In Kafka, concurrency refers to the ability to have multiple consumers read from a topic in parallel, each from different partitions. A Kafka topic can have multiple partitions and to leverage concurrency, it’s recommended that a Kafka consumer group has enough instances running to at least match the number of topic partitions.

In the context of Spring Boot using spring-kafka, each @KafkaListener can be configured to handle messages from multiple threads. This is managed by setting the concurrency level in the listener's container properties.

Configuring Kafka Consumer Concurrency in Spring Boot

Spring Boot makes it easy to configure Kafka consumer concurrency by setting properties in the application.yml or application.properties file, or by programmatically configuring the listeners.

Properties Configuration

You can specify the number of threads that will handle messages concurrently through application.yml or application.properties:

yaml
1spring:
2  kafka:
3    consumer:
4      bootstrap-servers: localhost:9092
5    listener:
6      concurrency: 3

or

properties
spring.kafka.consumer.bootstrap-servers=localhost:9092
spring.kafka.listener.concurrency=3

Here, concurrency is set to 3, meaning Spring Boot will start three threads to process messages concurrently.

Programmatic Configuration

You can also configure concurrency programmatically in your Spring Boot application using Java configuration:

java
1@EnableKafka
2@Configuration
3public class KafkaConfig {
4
5    @Bean
6    public ConsumerFactory<String, String> consumerFactory() {
7        Map<String, Object> props = new HashMap<>();
8        props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
9        props.put(ConsumerConfig.GROUP_ID_CONFIG, "some-group");
10        props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
11        props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName());
12        return new DefaultKafkaConsumerFactory<>(props);
13    }
14
15    @Bean
16    public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
17        ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>();
18        factory.setConsumerFactory(consumerFactory());
19        factory.setConcurrency(3); // Set concurrency here
20        return factory;
21    }
22}

This configuration creates a ConcurrentKafkaListenerContainerFactory and sets the concurrency level to 3.

Important Considerations

  1. Partition Count: The concurrency level should not exceed the number of partitions in a topic because each consumer in a group consumes from a unique partition.
  2. Consumer Group: Ensure that each Kafka consumer instance has the same group_id if they are part of the same consumer group.
  3. Ordering Guarantees: Within a partition, messages are processed in the order they are stored. However, across partitions, this order is not guaranteed.

Summary Table

Property/MethodDescription
spring.kafka.listener.concurrencyProperty in application.yml or application.properties to set the number of concurrent consumers.
ConcurrentKafkaListenerContainerFactoryBean configuration to set concurrency programmatically.
Consumer PartitionsConcurrency should match or be less than the number of topic partitions for optimal performance.
Consumer GroupAll consumers intended to work together should share the same group_id.

Setting the appropriate level of consumer concurrency is crucial for optimizing the performance of your Kafka consumers in Spring Boot applications. It allows for scalable, parallel processing of incoming messages while managing resource utilization effectively.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.