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.
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 offsetlatest: 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:
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
| Configuration | Description | Usage Scenario |
auto.offset.reset | Controls where to start consuming if no offset is found | Configurable as latest or earliest |
latest | Consumes only messages that arrive after consumer starts | Real-time processing |
earliest | Consumes all messages from the start of the log | Historical 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
- Kafka Consumer in C++
- kafka consumer in R
- Kafka consumer in Spark Streaming
- Kafka Consumer is not consuming messages from all partitions
- Kafka Consumer seektoBeginning
- Kafka Find Controller ID in a cluster using Kraft protocol
- Kafka Consumer poll messages with python
- Kafka not receiving messages when indicating group_id in Python

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.