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.
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:
or
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:
This configuration creates a ConcurrentKafkaListenerContainerFactory and sets the concurrency level to 3.
Important Considerations
- 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.
- Consumer Group: Ensure that each Kafka consumer instance has the same
group_idif they are part of the same consumer group. - 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/Method | Description |
spring.kafka.listener.concurrency | Property in application.yml or application.properties to set the number of concurrent consumers. |
ConcurrentKafkaListenerContainerFactory | Bean configuration to set concurrency programmatically. |
| Consumer Partitions | Concurrency should match or be less than the number of topic partitions for optimal performance. |
| Consumer Group | All 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
- How to set Kafka parameters from a properties file?
- How to set offset committed by the consumer group using Spark's Direct Stream for Kafka?
- How to set offset in kafkalistener
- How to set optimal config values - trigger time, maxOffsetsPerTrigger - for Spark Structured Streaming while reading messages from Kafka?
- How to setup a background process using asyncio as part of a module
- How to setup heterogeneous replication with tungsten?
- How to set multiple headers at once in Spring WebClient?
- How to set or change the default Java (JDK) version on macOS?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.