
Kafka Consumer Graceful Shutdown: Handle WakeupException and Commit Offsets Safely
In the previous article, we looked at how rebalancing works and why consumers pause during deployments. Now let’s look at something that causes even more real production issues: How do you gracefully shut down a Kafka consumer without losing work? In real systems: Pods restart Deployments happen Servers receive SIGTERM Auto-scaling removes instances If shutdown is not handled properly, you can: Reprocess messages Lose uncommitted offsets Cause inconsistent state Trigger unnecessary rebalances Kafka does not handle this automatically for you. You have to shut down the consumer correctly. Why Graceful Shutdown Matters A typical Kafka consumer runs inside a loop: while ( true ) { ConsumerRecords < String , String > records = consumer . poll ( Duration . ofMillis ( 100 )); for ( ConsumerRecord < String , String > record : records ) { process ( record ); } } Now imagine the application is terminated while: Records are already processed Offsets are not committed yet When the consumer restart
Continue reading on Dev.to
Opens in a new tab

