Kafka Consumer
Python Programming
Message Consumption
Data Streaming
Kafka-Python API

Kafka Consumer How to start consuming from the last message in Python

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 streaming platform capable of handling high volumes of data and enables the building of real-time streaming data pipelines and applications. At its core, Kafka is based on a publish-subscribe model of messaging and operates on topics that can be consumed by multiple consumers. In many scenarios, particularly in real-time applications, developers need to configure Kafka consumers to begin reading messages from the most recent point in the Kafka topic rather than consuming all available historical messages.

Understanding Kafka Consumer Basics

A Kafka consumer subscribes to one or more topics and reads the messages in the order they were written. The consumer maintains an offset to keep track of the next message it needs to read. There are certain scenarios, such as during a system restart or new deployment, where a consumer needs to start consuming from a specific point in the topic. Specifically, starting from the last message ensures that the consumer only processes new messages that arrive after it starts.

Configuring the Consumer to Start from the Last Message

In Kafka, the position where the consumer begins reading messages is controlled by the auto.offset.reset setting in the consumer configuration. This setting tells the consumer what to do when there is no initial offset or if the current offset does not exist on the server (for example, if it was deleted):

  • earliest: automatically reset the offset to the earliest offset
  • latest: automatically reset the offset to the latest offset

To start consuming from the latest message in Python, you can use the confluent_kafka Python package, which is a popular and robust Kafka client. Below is an example of how to create a Kafka consumer that subscribes to a topic and starts consuming from the latest message:

python
1from confluent_kafka import Consumer, KafkaError
2
3# Kafka consumer configuration 
4config = {
5    'bootstrap.servers': 'localhost:9092',
6    'group.id': 'my_consumer_group',
7    'auto.offset.reset': 'latest'
8}
9
10# Creating a Kafka consumer object
11consumer = Consumer(config)
12
13# Subscribe to a Kafka topic
14consumer.subscribe(['my_topic'])
15
16# Poll for new messages and process them
17try:
18    while True:
19        msg = consumer.poll(timeout=1.0)  # Poll every 1 second
20        if msg is None:
21            continue
22        if msg.error():
23            if msg.error().code() == KafkaError._PARTITION_EOF:
24                continue  # End of partition event
25            else:
26                print(msg.error())
27                break
28        print('Received message: {}'.format(msg.value().decode('utf-8')))
29finally:
30    # Clean up on exit
31    consumer.close()

Why Start from the Latest Message?

This method is particularly useful in real-time applications where the processing of only new messages aligns with business requirements or system resource management strategies. Use cases include real-time monitoring systems, event-driven architectures, and microservices that interact based on the latest events.

Summary Table

ConfigurationDescriptionUsage Scenario
auto.offset.resetControls where to start consuming if no offset is foundConfigurable as latest or earliest
latestConsumes only messages that arrive after consumer startsReal-time processing
earliestConsumes all messages from the start of the logHistorical data processing

Conclusion

Configuring a Kafka consumer to start from the last message is a useful technique in scenarios requiring real-time data processing. Understanding and correctly setting the auto.offset.reset configuration ensures that applications behave as expected without redundant processing of old messages. This approach helps developers leverage Kafka's real-time processing capabilities to build responsive and efficient streaming applications.

Using tools and libraries like confluent_kafka in Python further simplifies implementing this configuration, allowing developers to focus more on the logic and performance of their applications.


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.