tencent cloud

Feedback

Sequential Message

Last updated: 2023-09-12 16:32:32
    Sequential message is an advanced message type provided by TDMQ for RocketMQ. For a specified topic, messages are published and consumed in strict accordance with the principle of First-In-First-Out (FIFO), that is, messages sent first are consumed first, and messages sent later are consumed later.
    
    Sequential messages are suitable for scenarios that have strict requirements on the sequence of message sending and consumption.
    

    Use Cases

    The comparison between sequential message and general message is as follows:
    Message Type
    Consumption Sequence
    Performance
    Applicable Scenarios
    General message
    No sequence
    High
    Huge-throughput scenarios with no requirements for production and consumption sequence
    Sequential message
    All messages in the specific topic follow the FIFO rule
    Average
    Average-throughput scenarios that require publishing and consuming all messages in strict accordance with the FIFO rule
    
    Sequential messages are often used in the following business scenarios:
    
    Order creation: In some ecommerce systems, an order's creation, payment, refund, and logistics messages must be produced or consumed in strict sequence, No the order status will be messed up during consumption, which will affect the normal operation of the business. Therefore, the messages of this order must be produced and consumed in a certain sequence in the client and message queue. At the same time, the messages are sequentially dependent, and the processing of the next message must be dependent on the processing result of the preceding message.
    Log sync: In the scenario of sequential event processing or real-time incremental data sync, sequential messages can also play a greater role. For example, it is necessary to ensure that database operations are in sequence when MySQL binlogs are synced.
    Financial scenarios: In some matchmaking transaction scenarios like certain securities transactions, the first bidder is given priority in the case of the same biding price, so it is necessary to produce and consume sequential messages in a FIFO manner.
    

    How It Works

    In TDMQ for RocketMQ, the principle of sequential messages is shown in the figure below. You can partition messages according to a certain standard (such as ShardingKey in the figure), and messages of the same ShardingKey will be assigned to the same queue and consumed in sequence.
    
    
    
    The code of sequential message is as shown below:
    public class Producer {
    public static void main(String[] args) throws UnsupportedEncodingException {
    try {
    DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
    producer.start();
    
    String[] tags = new String[] {"TagA", "TagB", "TagC", "TagD", "TagE"};
    for (int i = 0; i < 100; i++) {
    int orderId = i % 10;
    Message msg =
    new Message("TopicTest", tags[i % tags.length], "KEY" + i,
    ("Hello RocketMQ " + i).getBytes(RemotingHelper.DEFAULT_CHARSET));
    SendResult sendResult = producer.send(msg, new MessageQueueSelector() {
    @Override
    public MessageQueue select(List<MessageQueue> mqs, Message msg, Object arg) {
    Integer id = (Integer) arg;
    int index = id % mqs.size();
    return mqs.get(index);
    }
    }, orderId);
    
    System.out.printf("%s%n", sendResult);
    }
    
    producer.shutdown();
    } catch (MQClientException | RemotingException | MQBrokerException | InterruptedException e) {
    e.printStackTrace();
    }
    }
    }
    
    The main difference here is that the SendResult send(Message msg, MessageQueueSelector selector, Object arg) method is called, MessageQueueSelector is the queue selector, and arg is a Java object, which can be passed in as the classification standard of the message sending partition.
    
    The MessageQueueSelector API is as follows:
    public interface MessageQueueSelector {
    MessageQueue select(final List<MessageQueue> mqs, final Message msg, final Object arg);
    }
    
    Among them, mqs is the queue that can be sent, msg is the message, arg is the object passed in the above send API, and the queue to which the message needs to be sent is returned. In the above sample, orderId is used as the partition classification standard, and the remainder of all queue numbers is used to send messages with the same orderId to the same queue.
    In the production environment, we recommend that you select the most fine-grained partition key for splitting. For example, when the order ID and user ID are used as the partition key keywords, the messages of the same end user will be processed in sequence, while those of different users will not.
    
    Note
    In order to ensure the high availability of messages, TDMQ for RocketMQ currently doesn't support "globally sequential messages" in a single queue (if you have already created globally sequential messages, you can use them normally); if you want to ensure global sequence, you can use consistent ShardingKey to do so.
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support