Spring-Kafka Concurrency Property
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Spring Kafka provides a powerful way to integrate Kafka with your Spring application. One pivotal feature in Spring Kafka is its concurrency support that lets applications process messages parallely, improving throughput and efficiency. This is primarily configured via the concurrency property in the Kafka listener container.
Understanding Kafka Listener Containers
In Spring Kafka, message listeners are wrapped in a container that handles the thread management and message polling from Kafka topics. The @KafkaListener annotation in Spring Kafka marks a method to be the target of a Kafka message listener within a managed bean.
Concurrency Property: Overview
The concurrency property specifies the number of consumer threads that the listener container should create for each @KafkaListener annotated method. This allows the application to process multiple Kafka partitions concurrently. When you set up concurrency, the Spring Kafka listener container creates multiple Kafka consumers in separate threads, each polling messages from different partitions of a topic.
How It Works
When you define a concurrency level of, say, 5, Spring will create five threads to listen for messages, where each thread handles one or more partitions of the topic, depending on the topic's partitioning. The rule of thumb is that the concurrency level should not be higher than the number of partitions in the topic, because a Kafka consumer cannot consume from more than one partition of the same topic simultaneously.
Setup in Spring Kafka
Let's set up a simple Spring Kafka application that uses the concurrency property for parallel processing. First, you will need the Spring Boot Starter for Kafka and the Kafka dependencies in your build configuration:
Then define a Kafka listener in your Spring service:
In this example, the concurrency property is set to 3, implying that three threads will be listening to the Kafka topic commonly named myTopic.
Points to Consider
- Over Partitioning: Setting a concurrency level higher than the number of partitions might lead to idle consumers, which is a waste of resources.
- Thread Safety: Make sure that your code inside the Kafka listener is thread-safe. Concurrency introduces multiple threads handling messages simultaneously, which could lead to race conditions or data integrity issues if not handled properly.
- Scaling: If your Kafka topic is partitioned properly, increasing the
concurrencycan effectively scale your message processing horizontally within one or more service instances.
Best Practices
- Partition Count: Ensure the number of partitions in your Kafka topic matches or exceeds the
concurrencylevel. - Thread Safety: Always take care of thread safety when processors modify shared resources.
- Resource Allocation: Consider the number of listeners and the hardware resources available, as consuming too many resources could affect overall application performance.
Summary Table
Here is a quick summary of the key points discussed:
| Aspect | Description |
| Concurrency | Number of threads created for processing messages in parallel. |
| Max Value | Should not exceed the number of topic partitions. |
| Thread Safety | Crucial to ensure safe operation in a multi-threaded environment. |
| Scaling | Increasing concurrency can improve throughput if partitions are adequate. |
Conclusion
The concurrency property in Spring Kafka is a crucial feature for optimizing performance and scalability in Kafka-based messaging applications. By wisely setting up this property, developers can ensure efficient processing of messages, leveraging parallel processing capabilities that align well with Kafka’s design principles.
Related reading
- Spring-Kafka How to pass the kafka topic from the application.yml
- Spring-Kafka vs. kafka-clients directly
- Spring-Kafka vs. kafka-clients directly
- Spring / RabbitMQ transaction management
- Spring Async - no data found in integration test
- Spring Async not allowing use of autowired beans
- spring4.2.1, hibernate5 integrate abstract method error
- Spring - Multiple Spring Data modules found, entering strict repository configuration mode

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.