Kafka Consumer
Multiple Topics
Message Order
Data Streaming
Distributed Systems

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.

Practice system design

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.records configuration: This limits the number of records returned from poll().
  • The fetch size configurations, like fetch.min.bytes and fetch.max.wait.ms, which control the data volume and wait time for each poll() operation.

Example Scenario

Here’s an example to illustrate consuming from multiple topics:

  • Topics/Partitions: Consumer subscribes to TopicA (Partition1, Partition2) and TopicB (Partition3).
  • Message Order: Messages in Partition1 are processed as Message1, Message2, ..., MessageN, and similarly for Partition2 and Partition3.
  • Consumption: The consumer could simultaneously fetch messages from Partition1, Partition2, and Partition3. The relative order of messages fetched from each partition is not fixed.

Key Considerations

FactorDescription
Partition AllocationConsumers within a group will have partitions assigned dynamically or statically. This allocation affects the overall ordering of messages consumed.
Consumer ConcurrencyIf concurrency is needed, consider using separate consumer instances each assigned to certain partitions to ensure message order within those partitions.
Polling StrategyConfiguration 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.


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