You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add concurrency support to ShareKafkaMessageListenerContainer
Share consumers (KIP-932) enable record-level load balancing where
multiple consumers can cooperatively process from the same partitions.
Unlike traditional consumer groups with exclusive partition ownership,
share groups distribute work at the broker level via the share group
coordinator.
This commit adds native concurrency support to the existing
`ShareKafkaMessageListenerContainer` rather than creating a separate
`ConcurrentShareKafkaMessageListenerContainer`. This design choice
avoids the parent/child container complexity that exists in the regular
consumer model, since share consumers fundamentally operate differently:
- Work distribution happens at the broker level, not at the Spring layer
- Multiple threads simply provide more capacity for the broker to
distribute records across
- No partition ownership model to coordinate between child containers
This approach provides:
- Simpler architecture with a single container managing multiple threads
- No parent/child context propagation concerns
- Better alignment with share consumer semantics (record-level vs
partition-level distribution)
- Increased throughput for high-volume workloads
- Better resource utilization across consumer threads
Users can configure concurrency at three levels:
1. Per-listener via `@KafkaListener(concurrency = N)`
2. Factory-level default via `factory.setConcurrency(N)`
3. Programmatically via `container.setConcurrency(N)`
The feature works seamlessly with both implicit (auto-acknowledge) and
explicit (manual acknowledge/release/reject) acknowledgment modes, with
each consumer thread independently managing its own acknowledgments.
* Address PR feedback on concurrency implementation
- Use primitive int for concurrency in factory (consistent with phase field)
- Remove unnecessary `getConcurrency()` getter (only used in trivial tests)
- Use `HashMap` instead of `ConcurrentHashMap` in metrics() (already inside lock)
- Use `CompletableFuture.allOf()` for cleaner shutdown coordination
- Remove debug logging from tests (unnecessary noise in CI/CD)
- Remove thread tracking from concurrency tests (over-complicates assertions)
Clarify documentation based on KIP-932 specifications:
- Add explicit note that concurrency is additive across application instances
- Replace high-level distribution description with precise KIP-932 details
- Document pull-based model, acquisition locks, and batch behavior
- Explain `max.poll.records` as soft limit with complete batch preference
- Set accurate expectations about broker-controlled record distribution
Signed-off-by: Soby Chacko <soby.chacko@broadcom.com>
The `ShareKafkaMessageListenerContainer` provides a simple, single-threaded container for share consumers:
110
+
The `ShareKafkaMessageListenerContainer` provides a container for share consumers with support for concurrent processing:
111
111
112
112
[source,java]
113
113
----
@@ -151,6 +151,172 @@ Share consumers do not support:
151
151
* Manual offset management
152
152
====
153
153
154
+
[[share-container-concurrency]]
155
+
=== Concurrency
156
+
157
+
The `ShareKafkaMessageListenerContainer` supports concurrent processing by creating multiple consumer threads within a single container.
158
+
Each thread runs its own `ShareConsumer` instance that participates in the same share group.
159
+
160
+
Unlike traditional consumer groups where concurrency involves partition distribution, share consumers leverage Kafka's record-level distribution at the broker.
161
+
This means multiple consumer threads in the same container work together as part of the share group, with the Kafka broker distributing records across all consumer instances.
162
+
163
+
[IMPORTANT]
164
+
====
165
+
**Concurrency is Additive Across Application Instances**
166
+
167
+
From the share group's perspective, each `ShareConsumer` instance is an independent member, regardless of where it runs.
168
+
Setting `concurrency=3` in a single container creates 3 share group members.
169
+
If you run multiple application instances with the same share group ID, all their consumer threads combine into one pool.
170
+
171
+
For example:
172
+
* Application Instance 1: `concurrency=3` → 3 share group members
173
+
* Application Instance 2: `concurrency=3` → 3 share group members
174
+
* **Total**: 6 share group members available for the broker to distribute records to
175
+
176
+
This means setting `concurrency=5` in a single container is operationally equivalent to running 5 separate application instances with `concurrency=1` each (all using the same `group.id`).
177
+
The Kafka broker treats all consumer instances equally and distributes records across the entire pool.
178
+
====
179
+
180
+
==== Configuring Concurrency Programmatically
181
+
182
+
[source,java]
183
+
----
184
+
@Bean
185
+
public ShareKafkaMessageListenerContainer<String, String> concurrentContainer(
* **Thread Safety**: Each consumer thread has its own `ShareConsumer` instance and manages its own acknowledgments independently
253
+
* **Client IDs**: Each consumer thread receives a unique client ID with a numeric suffix (e.g., `myContainer-0`, `myContainer-1`, etc.)
254
+
* **Metrics**: Metrics from all consumer threads are aggregated and accessible via `container.metrics()`
255
+
* **Lifecycle**: All consumer threads start and stop together as a unit
256
+
* **Work Distribution**: The Kafka broker handles record distribution across all consumer instances in the share group
257
+
* **Explicit Acknowledgment**: Each thread independently manages acknowledgments for its records; unacknowledged records in one thread don't block other threads
258
+
259
+
==== Concurrency with Explicit Acknowledgment
260
+
261
+
Concurrency works seamlessly with explicit acknowledgment mode.
262
+
Each consumer thread independently tracks and acknowledges its own records:
0 commit comments