TL;DR

  • Scenario: RocketMQ consumer selection and online consumption backlog, delay, duplicate consumption troubleshooting
  • Conclusion: RocketMQ “Push” essence is client long polling pull; differences mainly in rhythm control and offset management responsibility
  • Output: Push/Pull mechanism comparison + cluster workflow + common fault quick reference and fix path

RocketMQ Consumption Modes

RocketMQ provides two message subscription modes, PUSH mode and PULL mode:

1. PUSH Mode (MQPushConsumer)

  • Surface is Broker actively pushing messages to Consumer
  • Actual implementation is through long polling mechanism maintained inside Consumer
  • Typical use scenario: businesses needing real-time message consumption, such as order processing, instant notifications, etc.
  • Advantage: Developer friendly, automatically handles message pulling and consumption progress management

2. PULL Mode (MQPullConsumer)

  • Consumer proactively initiates pull requests to Broker
  • Developer needs to control pull frequency and message processing logic
  • Typical application scenario: batch processing tasks, scheduled tasks and other non-real-time scenarios
  • Advantage: Consumption rhythm completely controlled by application

Implementation Mechanism说明

Although the two modes are conceptually different, underlying both based on pull mechanism. PUSH mode is essentially implemented through:

  1. Consumer registers with Broker after startup
  2. Internal thread periodically (default 5 seconds) initiates pull request to Broker
  3. Returns immediately when new messages exist; when no messages, Broker holds request (up to 15 seconds max)
  4. If new message arrives during this period, Broker responds immediately

Technical Details Comparison

FeaturePUSH ModePULL Mode
Message AcquisitionAuto polling, default 5 second intervalMust explicitly call pullBlockIfNotFound
Consumption Progress ManagementAuto commit offsetManual offset management
Exception HandlingBuilt-in retry mechanismNeed to implement retry logic yourself

RocketMQ Cluster Workflow

1. NameServer Startup

NameServer starts and listens on specified port, serving as routing control center waiting for Broker, Producer and Consumer connections.

2. Broker Startup

Broker starts and maintains long connection with all NameServers, periodically sending heartbeat packets containing Broker info and Topic data.

3. Message Production Flow

When producer starts:

  • Establishes long connection with any NameServer
  • Gets Broker info for target Topic
  • Uses round-robin to select queue
  • Establishes connection with target Broker and sends message

4. Message Consumption Flow

When consumer starts:

  • Connects to any NameServer
  • Gets Broker info for subscribed Topic
  • Directly establishes consumption channel with relevant Broker
  • Starts consuming messages

Push Mode

Core Characteristics

Push mode is a message push mechanism where server actively pushes messages to consumer in real-time. Typical application scenarios include instant messaging, real-time data monitoring and other fields with high timeliness requirements.

Advantage Analysis

  1. High Real-time: Messages pushed immediately after generation, ensuring lowest latency
  2. Server Initiative: Consumer doesn’t need to poll, reducing invalid requests
  3. Resource Savings: Avoids waste from consumer frequent queries

Potential Issues

  1. Consumer Pressure: When facing burst traffic, server may instantly push large number of messages
  2. Processing Capacity Bottleneck: Consumer processing capacity is usually limited, message backlog may occur
  3. Cascading Failures: Severe cases can cause consumer service crash

Response Strategies

  1. Rate Limiting: Server implements message push rate limiting
  2. Elastic Scaling: Consumer implements auto scaling mechanism
  3. Degradation Plan: Set message importance classification

Pull Mode

Advantages

  1. High Real-time: Consumer can proactively pull messages as needed, reducing message transmission latency
  2. Controllable Consumption: Consumer can decide pull frequency and quantity based on its own processing capacity
  3. High Resource Utilization: Server doesn’t need to maintain state information for each consumer

Disadvantages

  1. Limited Consumer Processing Capacity: When consumer processing capacity is insufficient, message backlog easily occurs
  2. Polling Overhead: Consumer needs to continuously poll to check for new messages, may generate additional network overhead

Optimization Solutions

  1. Dynamically adjust pull frequency: Automatically adjust pull interval based on processing capacity
  2. Batch processing: Appropriately increase number of messages pulled each time
  3. Consumer load balancing: Deploy multiple consumer instances to share processing pressure

Detailed Comparison: Push vs Pull

Push Mode Implementation Principle

consumer.registerMessageListener(new MessageListenerConcurrently() {
    @Override
    public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
        // Business processing logic
        return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
    }
});

Pull Mode Implementation Details

  1. Get message queue collection:
Set<MessageQueue> mqs = consumer.fetchSubscribeMessageQueues("TopicTest");
  1. Traverse and process each message queue:
PullResult pullResult = consumer.pull(mq, "*", offset, 32);

Error Quick Reference

SymptomRoot CauseFix
Consumption delay suddenly increases, backlog surgesConsumer thread pool/business processing slowsCheck Consumer end processing time, concurrency
Same message consumed multiple timesAt-least-once semantics, crash before ackBusiness idempotency
Consumption offset chaotic, jumping or rollbackManual offset management logic errorVerify offset storage and write-back frequency
Frequent Rebalance, consumption jitterConsumer instances frequently go online/offlineVerify heartbeat/timeout configuration
Some queues not consumed for long timeUneven queue allocationCompare each MessageQueue’s consumption progress
Pull mode can’t pull messagesSubscription expression/Tag mismatchVerify subscription expression