Backpressure and Dynamic Consumer Pause and Resume
Manage consumer backpressure dynamically using KafkaListenerEndpointRegistry, prevent rebalance storms during downstream distress, and integrate circuit breakers.
Kafka brokers can stream messages at hundreds of megabytes per second. However, consumer applications rarely perform pure in-memory work; they write to relational databases, execute complex business transactions, and invoke downstream microservices over HTTP.
When a downstream database experiences connection pool exhaustion or an external payment provider rate-limits requests, the consumer faces a backpressure mismatch. The broker continues pushing records faster than the application can process them.
If unmanaged, this mismatch leads to out-of-memory errors, thread exhaustion, or unhandled timeouts that exceed max.poll.interval.ms, triggering catastrophic consumer rebalance storms.
01The risk of downstream saturation
Consider what happens when a downstream service degrades:
Kafka Broker ──(High-Speed Pull: 5,000 msg/sec)──> Consumer Instance
│
Thread Pool
│
(Bottleneck: 20 req/sec)
▼
Downstream Database
(Connection Pool Maxed)If the consumer application attempts to buffer incoming messages in memory:
- Java heap memory fills up, leading to high garbage collection pauses.
- If the listener thread blocks on a slow database call, the duration between successive
consumer.poll()invocations increases. - Once the interval between
poll()calls surpassesmax.poll.interval.ms(default 300,000 ms or 5 minutes), the broker group coordinator marks the consumer dead and evicts it from the consumer group. - The remaining consumers inherit the abandoned partitions and promptly crash under the same load, cascading the failure across the entire cluster.
Loading the rest of the lesson…