A pub-sub system, short for publish-subscribe system, is a messaging pattern used in distributed systems to enable communication between components or services while decoupling them from each other.
Our example will be based on RocketQ. RocketQ is an open-source pub-sub messaging system developed by Alibaba Group. It is designed to provide reliable, high-throughput, and low-latency message communication for distributed systems.
In this article we try to better understand the difference between a
Group and a
Topic in RocketQ.
Topic: A topic refers to a specific subject or category to which messages are published and subscribed. Publishers publish messages to specific topics, and subscribers can choose to receive messages from one or more topics of interest.
Group: A group allows to organize multiple consumers that subscribe to the same topic. By dividing consumers into groups, RocketQ ensures that only one consumer within a group receives a specific message, enabling parallel processing and load balancing across multiple consumers. This grouping mechanism helps to distribute the message processing workload efficiently and enhances the scalability and performance of the overall Pub-Sub system.
Real life example - Order creation
To better understand this, let's look at a specific real life example. Imagine we are creating a
topic called ORDER_CREATION in RocketQ. Our
publisher pushes messages into this topic as soon as a new order should be created.
In our distributed system, we have 4x server instances and therefore
4x consumers to handle the order creation. Those 4 consumers subscribe to the same
Of course, we do not want all
4x consumers to consume the same message and create the same order. This way, we would need to handle the concurrent processing with locks by ourselves. We only want
1 consumer to receive the message and therefore creates the order. This is where a
group comes in handy.
By creating a
group, let's say MY_SERVERS and assigning this
group to our
4x consumers, RocketQ only sends the message to
1x consumer within this group. This grouping mechanism helps to distribute the message processing workload efficiently and enhances the scalability and performance of the overall Pub-Sub system.
Understanding the distinction between a
group and a
topic in RocketQ is crucial for effectively utilizing the pub-sub system. Topics serve as specific subjects to which messages are published and subscribed. On the other hand,
groups enable the organization of multiple consumers subscribing to the same
topic, ensuring that only one consumer within a group receives a particular message.