Kafka Consumer
System Shutdown
Programming
Consumer Applications
Debugging Issues

Shutting down Kafka Consumer

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a distributed event streaming platform that enables its users to publish and subscribe to streams of records, store records in a fault-tolerant manner, and process them as they occur. Kafka’s consumers read data from a topic and are typically part of a consumer group. When multiple consumers are subscribed to a topic and belong to the same consumer group, each consumer in the group will receive messages from a subset of the partitions of the topic.

Graceful Shutdown of a Kafka Consumer

Proper shutdown and cleanup of resources are crucial for Kafka consumers to ensure that there are no memory leaks and that all sockets and connections are closed properly. Managing consumer shutdowns effectively also helps in maintaining the integrity of the stream processing, ensuring accurate message consumption without data loss or duplication.

Technical Explanation and Examples

Here's a step-by-step guide on how to gracefully shutdown a Kafka consumer using Java (which is one of the most common languages used with Kafka):

  1. Handling Interrupt Signal -
    Typically, shutdown occurs when some interrupt signal is received (like SIGTERM in UNIX-based systems) or upon application termination. You can handle this in Java by adding a shutdown hook:
java
   Runtime.getRuntime().addShutdownHook(new Thread(() -> {
       consumer.close();
   }));
  1. Closing the Consumer -
    The Kafka consumer's close() method is crucial as it handles the commit of the offsets (if the consumer's auto-commit is false) and informs the group coordinator that the consumer is leaving the group, allowing other consumers to rebalance:
java
1   try {
2       while (true) {
3           ConsumerRecords<String, String> records = consumer.poll(Duration.ofMillis(100));
4           for (ConsumerRecord<String, String> record : records) {
5               processRecord(record);
6           }
7           consumer.commitSync(); // Manual commit of offset
8       }
9   } catch (WakeupException e) {
10       // ignore for shutdown
11   } finally {
12       consumer.close(); // Close the consumer
13   }

In the above example, consumer.poll() is called in a loop to fetch records. A WakeupException can be used to break out of the polling loop under certain conditions, such as during a shutdown.

  1. Using Wakeup -
    The wakeup() method of the Kafka consumer is a thread-safe method and can be used from an external thread to break out of the poll():
java
1   public class KafkaConsumerRunner implements Runnable {
2       private final AtomicBoolean closed = new AtomicBoolean(false);
3       private final KafkaConsumer consumer;
4
5       public void run() {
6           try {
7               while (!closed.get()) {
8                   ConsumerRecords records = consumer.poll(timeout);
9                   // process records
10               }
11           } catch (WakeupException e) {
12               // Ignore exception if closing
13               if (!closed.get()) throw e;
14           } finally {
15               consumer.close();
16           }
17       }
18
19       // Shutdown hook which can be called from a separate thread
20       public void shutdown() {
21           closed.set(true);
22           consumer.wakeup();
23       }
24   }

Table: Key Functions and Their Descriptions

Here is a summary of the key functions and methods used in the shutdown process of Kafka consumers:

Function/MethodDescription
consumer.poll(duration)Fetches records from the Kafka broker. Must be called continuously within an active consumer loop.
consumer.close()Closes the consumer, commits offsets if necessary, and notifies the group coordinator that the consumer is leaving the group.
consumer.wakeup()Used to break out from polling and throw a WakeupException.
runtime.addShutdownHook(thread)Attaches a thread to the Java Runtime shutdown sequence, allowing for graceful shutdown activities.

Best Practices for Consumer Shutdown

  • Always use try-catch blocks to manage expected and unexpected errors.
  • Ensure that any non-Kafka resources (like file handles or database connections) used by the consumer are also closed properly.
  • Test the shutdown process to ensure that the consumer handles shutdowns gracefully even during high load scenarios.

Proper handling of Kafka consumer shutdowns is essential to avoid resource leakage and ensure data consistency across distributed systems. Employing the aforementioned patterns and methods will help maintain a robust event-driven architecture.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track 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.

Practice system design

All Rights Reserved.