Order of receiving messages if Kafka consumer subscribes to multiple topics
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 highly popular event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications. As a key component in Kafka architecture, consumers play an essential role in reading and processing data from Kafka topics. Understanding how Kafka consumers handle the order of messages when subscribing to multiple topics is crucial for building robust and reliable applications.
Kafka Basics
Kafka stores records in topics that are split into a number of partitions. Each partition is an ordered, immutable sequence of records, and records within a partition have a specific sequence identified by an offset. Kafka guarantees that the consumer of a partition will read the records in the order they were stored.
Multiple Topic Subscriptions
When a single Kafka consumer subscribes to multiple topics, it effectively subscribes to all the partitions of these topics. How messages are consumed and processed from these partitions depends on several factors including:
- The configuration of the consumer
- Partition assignment strategy
- Consumer group management
Details of Consumption From Multiple Topics
When consuming from multiple topics, each partition from these topics can be thought of independently in terms of message ordering. Within a single partition, the order of messages is preserved. However, there is no inherent ordering between different partitions, even within the same topic. This means when a consumer reads from multiple partitions, it only ensures per-partition order but not between partitions.
Suppose a consumer is subscribed to two topics, Topic1 and Topic2, where Topic1 has Partition1 and Partition2, and Topic2 has Partition3. The consumer guarantees the order of messages in Partition1, Partition2, and Partition3 independently but not across these partitions.
Poll Behavior
Kafka consumers use a poll() method to fetch data from the server. When poll() is called, it retrieves records from each partition to which the consumer is assigned. The amount of data and ordering constraints fetched from each partition can be influenced by:
- The
max.poll.recordsconfiguration: This limits the number of records returned frompoll(). - The fetch size configurations, like
fetch.min.bytesandfetch.max.wait.ms, which control the data volume and wait time for eachpoll()operation.
Example Scenario
Here’s an example to illustrate consuming from multiple topics:
- Topics/Partitions: Consumer subscribes to
TopicA(Partition1,Partition2) andTopicB(Partition3). - Message Order: Messages in
Partition1are processed asMessage1,Message2, ...,MessageN, and similarly forPartition2andPartition3. - Consumption: The consumer could simultaneously fetch messages from
Partition1,Partition2, andPartition3. The relative order of messages fetched from each partition is not fixed.
Key Considerations
| Factor | Description |
| Partition Allocation | Consumers within a group will have partitions assigned dynamically or statically. This allocation affects the overall ordering of messages consumed. |
| Consumer Concurrency | If concurrency is needed, consider using separate consumer instances each assigned to certain partitions to ensure message order within those partitions. |
| Polling Strategy | Configuration settings for poll() can affect performance and message consumption patterns. |
Impact on Application Design
Applications should not depend on the order of messages across different partitions unless explicitly managed through design, such as using keyed messages that place all related messages into the same partition.
Conclusion
Understanding the message delivery semantics when consuming from multiple Kafka topics is fundamental for designing effective Kafka-based systems. By ensuring proper configurations and considering the implications of Kafka’s partitioning and polling mechanisms, developers can build robust systems that handle message ordering appropriately across multiple topics.
Related reading
- Ordering guarantees when using idempotent Kafka Producer
- org.apache.kafka.common.config.ConfigException Missing required configuration bootstrap.servers which has no default value
- org.apache.kafka.common.errors.TimeoutException Topic not present in metadata after 60000 ms
- org.apache.kafka.common.KafkaException Failed to construct kafka consumer
- Out-of-order AppendEntries in Raft
- Parallelism behaviour of stream processing engines
- org.apache.kafka.common.KafkaException io.confluent.kafka.serializers.KafkaAvroSerializer
- org.apache.kafka.common.network.InvalidReceiveException Invalid receive (size = 30662099 larger than 30662028)

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.