Kafka set the maximum number of messages to read from the topic
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 robust, distributed event streaming platform capable of handling trillions of events a day. Initially conceived as a messaging queue, Kafka is based on an abstraction of a distributed commit log. Since being open-sourced by LinkedIn in 2011, Kafka has become a key component in data architectures and real-time processing pipelines, used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications.
Controlling Message Consumption
In certain scenarios, such as testing or specific application requirements, there might be a need to limit the number of messages a Kafka consumer reads from a topic. Kafka doesn't provide an explicit configuration parameter to specify the maximum number of messages to consume directly. However, developers can control this behavior programmatically in their consumer application.
Implementing Message Limits in Kafka Consumers
To set a maximum number of messages that a Kafka consumer should read, you can manage this on the client side using the programming language of your choice. Here, we’ll discuss a Java example to illustrate the concept:
Key Considerations
- Poll Loop: The consumer retrieves a batch of messages from Kafka in each poll loop iteration. You control the number of messages processed by controlling the number of times this loop executes in conjunction with the batch size retrieved in each poll.
- Consumer Configuration: The
max.poll.recordsconfiguration in consumer properties can be set to control the maximum number of records returned in each poll call. Combining this setting with the control loop allows finer control over the number of messages consumed.
Summary Table
| Parameter | Description | Example |
bootstrap.servers | Kafka cluster's address | localhost:9092 |
group.id | Consumer group's identifier | test-group |
enable.auto.commit | Enables automatic offset commit | true |
key.deserializer | Key deserializer used by consumer | org.apache.kafka.common.serialization.StringDeserializer |
value.deserializer | Value deserializer used by consumer | org.apache.kafka.common.serialization.StringDeserializer |
max.poll.records | Maximum records per poll request | 100 (explicit setting required) |
Advanced Scenario
For more sophisticated scenarios, such as when processing must be paused after consuming a certain number of messages, Kafka's consumer APIs support pausing and resuming the consumption from specific partitions. This can be particularly useful in workflows where message processing involves complex computations or external system integrations.
Conclusion
While Kafka itself does not limit message consumption directly through configuration, it provides enough flexibility through its consumer API to implement this functionality as needed. By carefully managing the poll loop and leveraging consumer configurations, developers can effectively limit the number of messages processed, aligning with specific application requirements or system limitations.
Related reading
- Kafka setup with docker-compose
- Kafka Should Number of Consumer Threads equal number of Topic Partitions
- Kafka single consumer failure in a group
- Kafka Single consumer group in multiple instances
- Kafka Stream Scala API slow performance
- Kafka Streams in docker-compose takes long time for partition assignment
- Kafka Sink Connector fails Schema not found; error code 40403
- Kafka sink connector No tasks assigned, even after restart

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.