SpringSpring Kafka Advanced
How do you configure consumer listener concurrency for optimal throughput?
Reference answer
Set ConcurrentKafkaListenerContainerFactory.setConcurrency(N) or spring.kafka.listener.concurrency=N. Rule of thumb: concurrency = partition count / instances. For example, 12 partitions across 3 instances = concurrency=4 per instance. This maximizes parallelism without idle threads. For CPU-bound processing: don't exceed CPU cores × listeners. For I/O-bound: can safely exceed core count. Test with your workload — too much concurrency creates context switching overhead. Each concurrency increment creates a new KafkaConsumer with its own network connection to the broker.
Expected key concepts: setConcurrency, spring.kafka.listener.concurrency, partition count, instances, CPU-bound, I/O-bound, KafkaConsumer, network connection, context switching, parallelism