Why RocketMQ Producers Need Load Balancing and How It Works

This article explains why load balancing is crucial for RocketMQ producers, details the internal selection algorithm using ThreadLocal indexes and round‑robin logic, and provides Java code examples illustrating how messages are distributed across queues.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Why RocketMQ Producers Need Load Balancing and How It Works

Why RocketMQ Producers Need Load Balancing?

In RocketMQ, a queue is the basic unit for sending messages. A Topic can contain multiple queues, so a producer instance may send messages to different queues. Without balanced distribution, message load becomes uneven, degrading performance, making producer load‑balancing essential.

RocketMQ Producer Mechanism

Typical message sending code:

Message msg = new Message("TopicTest",
    "TagA",
    "OrderID188",
    "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
SendResult sendResult = producer.send(msg);

The core sending logic resides in the DefaultMQProducerImpl class, specifically the sendDefaultImpl method.

The crucial step is the selectOneMessageQueue method, which chooses a queue for each message.

public MessageQueue selectOneMessageQueue(final TopicPublishInfo tpInfo, final String lastBrokerName) {
    return this.mqFaultStrategy.selectOneMessageQueue(tpInfo, lastBrokerName);
}

The strategy ultimately calls

org.apache.rocketmq.client.impl.producer.TopicPublishInfo#selectOneMessageQueue

, which uses a thread‑local index to achieve round‑robin selection.

public MessageQueue selectOneMessageQueue(final String lastBrokerName) {
    if (lastBrokerName == null) {
        return selectOneMessageQueue();
    } else {
        for (int i = 0; i < this.messageQueueList.size(); i++) {
            int index = this.sendWhichQueue.incrementAndGet();
            int pos = Math.abs(index) % this.messageQueueList.size();
            if (pos < 0) pos = 0;
            MessageQueue mq = this.messageQueueList.get(pos);
            if (!mq.getBrokerName().equals(lastBrokerName)) {
                return mq;
            }
        }
        return selectOneMessageQueue();
    }
}
private volatile ThreadLocalIndex sendWhichQueue = new ThreadLocalIndex();

public MessageQueue selectOneMessageQueue() {
    int index = this.sendWhichQueue.getAndIncrement();
    int pos = Math.abs(index) % this.messageQueueList.size();
    if (pos < 0) pos = 0;
    return this.messageQueueList.get(pos);
}

The ThreadLocalIndex class provides a thread‑local counter that is randomly initialized for the first send and then incremented, ensuring each producer thread maintains its own index and improves load distribution.

public class ThreadLocalIndex {
    private final ThreadLocal<Integer> threadLocalIndex = new ThreadLocal<>();
    private final Random random = new Random();

    public int getAndIncrement() {
        Integer index = this.threadLocalIndex.get();
        if (index == null) {
            index = Math.abs(random.nextInt());
            if (index < 0) index = 0;
            this.threadLocalIndex.set(index);
        }
        index = Math.abs(index + 1);
        if (index < 0) index = 0;
        this.threadLocalIndex.set(index);
        return index;
    }
}

Conclusion

The article dissects the underlying implementation of RocketMQ producers, highlighting clever design choices such as the round‑robin algorithm based on ThreadLocal and the sendWhichQueue index. The presented code handles non‑ordered messages; for ordered messages, users can specify a custom load‑balancing strategy.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javaload balancingMessage QueueRocketMQThreadLocalProducer
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.