Mastering RocketMQ Transaction Messages: Theory, Flow, and Real‑World Example

This article explains RocketMQ transaction messages by covering their use cases, underlying mechanisms, and a step‑by‑step implementation that demonstrates how to ensure eventual consistency between message production and local database transactions in distributed Java back‑end systems.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Mastering RocketMQ Transaction Messages: Theory, Flow, and Real‑World Example

1 Application Scenarios

In an e‑commerce checkout flow, a payment order is created, the order status changes from unpaid to paid after successful payment, and points are awarded to the user. Using a normal asynchronous consumption model can lead to inconsistencies such as lost points or mismatched order states, requiring compensation logic or transaction status queries.

2 Functional Principle

RocketMQ transaction messages guarantee eventual consistency between message production and local transactions in distributed environments. The workflow is:

Producer sends a message to the broker.

Broker persists the message and returns an ACK, marking the message as "pending delivery" (a half‑transaction message).

Producer executes the local transaction logic.

Producer sends a second‑phase commit or rollback result (Commit or Rollback) to the broker.

If the broker does not receive the second‑phase result due to network issues or producer restart, it performs a message status check after a fixed interval, prompting the producer to re‑evaluate the local transaction outcome and resend the appropriate confirmation.

3 Practical Example

A demo project simulates the payment order creation, payment success, and points awarding process.

Key steps:

Create a real order topic order-topic.

Create three tables in the database: order , transaction_log , and points .

Implement a producer that creates a payment order and updates its status, invoking sendMessageInTransaction which sends a half‑transaction message first, then runs the local transaction.

Configure a transactional producer and a transaction listener that implements local transaction execution and provides the broker with a status check interface.

The listener calls orderService.updateOrder, which is annotated with a transaction, updating the order status and inserting a transaction log atomically.

Based on the listener’s return ( LocalTransactionState.COMMIT_MESSAGE or LocalTransactionState.ROLLBACK_MESSAGE), the broker either delivers the message to consumers or discards it.

The consumer group subscribes to order-topic, processes messages idempotently, and inserts points records only when no existing record is found.

4 Summary

RocketMQ transaction messages enable distributed systems to maintain final consistency between message production and local database operations. Effective usage requires:

Cooperation between transactional producers and consumers.

Implementation of a transaction listener that records transaction outcomes (e.g., in a transaction log table).

Idempotent consumer logic with retry, alerting, and manual intervention mechanisms to ensure correct consumption results.

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.

Distributed Systemsjavabackend-developmentMessage QueueRocketMQTransaction Messages
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

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.