When a producer sends messages to the ApsaraMQ for RocketMQ service, ApsaraMQ for RocketMQ uses a load balancing policy to distribute messages evenly across multiple queues, which prevents hot spot queues and performance bottlenecks. This topic describes the producer load balancing policies for ApsaraMQ for RocketMQ.
Background information
- Disaster recovery: You can determine how messages are switched over when local nodes fail.
- Message ordering: You can better understand how ApsaraMQ forRocketMQ ensures strict first-in-first-out message ordering.
- Load balancing: You can understand the distribution pattern of messages across multiple nodes and configure suitable traffic migration and scaling solutions.
Round robin
Usage scope
Round-robin load balancing is the default load balancing policy and the only load balancing policy that is supported for unordered messages (normal, scheduled, and transactional messages) in ApsaraMQ forRocketMQ.
How the policy works
In round-robin load balancing, a producer sends messages to all queues in a specified topic in a cycle to ensure that messages are evenly distributed across all the queues in the topic.

In the preceding figure, Queue 1, Queue 2, and Queue 3 are queues in the topic, and the combination of the letter M and a number indicates a message that is sent in order.
The producer sends messages to the queues in a cycle. The producer sends the first message M1 to Queue 1, the second message M2 to Queue 2, and the third message M3 to Queue 3, and then repeats the distribution pattern until all messages are sent.
Exception handling
If a message fails to send, ApsaraMQ for RocketMQ determines the cause of the failure. Based on the cause, it may temporarily skip the node that contains the failed queue when selecting a destination. This process provides automatic fault isolation.
Features
Round-robin load balancing is supported for only unordered messages. This policy ensures that messages are evenly distributed to maximize the performance of topics.
Example
The RoundRobin mode is the default for non-sequential messages and requires no additional settings.
// By default, round-robin load balancing is enabled for normal messages.
// Send normal messages.
MessageBuilder messageBuilder = null;
for (int i = 0; i < 10; i++) {
// You do not need to configure round-robin load balancing to deliver normal messages. The SDK includes the logic that is required to perform round-robin messaging to evenly distribute messages.
Message message = messageBuilder.setTopic("normalTopic")
// Specify the message index key so that the system can use a keyword to accurately locate the message.
.setKeys("messageKey")
// Specify the message tag so that consumers can use the tag to filter the message.
.setTag("messageTag")
// Configure the message body.
.setBody("messageBody".getBytes())
.build();
try {
// Send the messages. Focus on the result of message sending and exceptions such as failures.
SendReceipt sendReceipt = producer.send(message);
System.out.println(sendReceipt.getMessageId());
} catch (ClientException e) {
e.printStackTrace();
}
}
MessageGroupHash
Usage scope
MessageGroupHash-based load balancing is the default load balancing policy and the only load balancing policy that is supported for ordered messages in ApsaraMQ forRocketMQ.
Policy principle: Hash algorithm
MessageGroupHash-based load balancing uses a SipHash algorithm to distribute messages. In the MessageGroupHash-based load balancing mode, a producer sends messages in the same message group to the same queue based on the built-in SipHash algorithm and ensures that messages in the same message group are stored in the queue in the order in which the messages are sent.

In the preceding figure, G1-M1 is the first message in MessageGroup 1, G1-M2 is the second message, and G1-M3 is the third message. The producer sends the messages to the same queue (MessageQueue 1) and ensures that the three messages are stored in the queue in the same order in which the messages are sent.
Features
MessageGroupHash-based load balancing is supported for only ordered messages. This load balancing policy ensures the ordered delivery of messages in the same message group.
If the numbers of messages in multiple message groups are significantly different, MessageGroupHash-based load balancing cannot ensure even distribution of messages across and performance scaling. In specific scenarios, MessageGroupHash-based load balancing may cause most messages to be stored in a small number of queues. We recommend that you distribute the messages across multiple message groups to prevent many messages from being stored in a small number of message groups.
Example
By default, MessageGroupHash-based load balancing is enabled for ordered messages. The following sample code provides an example of MessageGroupHash-based load balancing:
// By default, MessageGroupHash-based load balancing is enabled for ordered messages.
for (int i = 0; i < 10; i++) {
Message message = messageBuilder.setTopic("fifoTopic")
// Specify the message index key so that the system can use a keyword to accurately locate the message.
.setKeys("messageKey")
// Specify the message tag so that the consumer can filter the message based on the specified tag.
.setTag("messageTag")
// Configure MessageGroup. Messages in the same message group are distributed to the same queue based on the SipHash algorithm.
.setMessageGroup("fifoGroupA")
// Configure the message body.
.setBody("messageBody".getBytes())
.build();
try {
// Send the messages. Focus on the result of message sending and exceptions such as failures.
SendReceipt sendReceipt = producer.send(message);
System.out.println(sendReceipt.getMessageId());
} catch (ClientException e) {
e.printStackTrace();
}
}Version compatibility
- MessageGroupHash mode
The MessageGroupHash load balancing policy is supported starting from ApsaraMQ for RocketMQ server version 5.x. Earlier versions, such as 4.x and 3.x, do not support this policy.
Therefore, if you upgrade the sending mechanism for ordered messages from version 4.x or 3.x to 5.x, you must take steps to ensure message ordering is preserved. For example, you can consume all messages in the topic before you switch to version 5.x.
- Round-robin mode
The round-robin load balancing policy is supported by server versions 5.x, 4.x, and 3.x and does not have compatibility issues.
Usage notes
Avoid overloading queues when you use MessageGroupHash-based load balancing
In MessageGroupHash mode, ApsaraMQ for RocketMQ guarantees that messages from the same message group are sent to the same queue. If your business logic concentrates messages into only a few message groups, the service will also store these messages in only a few queues. This can increase storage pressure, cause queue hot spots, and limit the topic's ability to scale.
We recommend that you distribute the messages across multiple message groups. For example, you can use order IDs and usernames as the keywords of message groups in e-commerce scenarios to ensure that messages are distributed among multiple message groups and that user-specific messages are processed in order.
Avoid sending messages to one queue
To ensure performance scaling and disaster recovery, we recommend that you use more than one queue for load balancing for ordered messages and unordered messages. If a topic contains only one queue, messages are sent only to the queue, regardless of the load balancing policy. This can cause performance bottleneck issues and disaster recovery issues.