Using RocketMQ for Peak Shaving in Spring Boot: Configuration, Consumer Tuning, and Batch Consumption
This article explains how to use RocketMQ with Spring Boot to implement peak‑shaving, covering core components, consumer configuration, Maven setup, code examples, performance testing, and strategies for dynamic scaling and batch consumption.
RocketMQ’s main characteristics—decoupling, asynchronous processing, and peak shaving—are introduced, followed by a description of its core components: Producer, Broker, Consumer, and NameServer.
The consumption flow is highlighted, emphasizing that consumers pull messages from brokers and that many configuration parameters operate at the queue level rather than the topic level.
rocketmq-spring-boot-starter usage is demonstrated. A Maven dependency is added: <dependency> <groupId>org.apache.rocketmq</groupId> <artifactId>rocketmq-spring-boot-starter</artifactId> </dependency> Application properties are configured in application.yml to define the name server, producer group, and database connection.
Two data classes are shown: PraiseRecord (the entity) and a simple MessageController that receives a praise request and sends it via rocketMQTemplate.sendOneWay() for higher throughput, accepting possible message loss.
The consumer implementation PraiseListener implements RocketMQListener<PraiseRecordVO> and RocketMQPushConsumerLifecycleListener . In prepareStart the pull interval is set to 2000 ms and the pull batch size to 16, resulting in a theoretical consumption rate of 128 messages per 2 seconds in a 2‑broker, 4‑queue‑per‑broker setup.
Performance testing shows the actual insertion rate stays close to the theoretical value, confirming that MQ effectively smooths database load during traffic spikes.
For dynamic scaling, the article explains that increasing the number of queues per broker or the maxTransferCountOnMessageInMemory parameter can raise the consumption capacity without restarting brokers.
Batch consumption is not supported out‑of‑the‑box by rocketmq-spring-boot-starter . A custom DefaultMQPushConsumer bean is provided, configuring pullBatchSize and consumeMessageBatchMaxSize to enable batch processing, with a sample listener that aggregates messages into a list for batch insertion.
Additional notes cover environment setup (NameServer + two Brokers), dashboard usage, and troubleshooting consumer registration when queues are added.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.