edit-icon download-icon

Is a message having a priority of 1 certainly consumed first?

Last Updated: May 05, 2017

Problem

When a message is sent through the Message Service, the priority value of the message can be specified. (The value ranges from 1 to 16. “1” indicates the highest priority. The default priority is “8”.) A message with a higher priority is more likely to be consumed earlier.

Is a message having a priority value of 1 certainly consumed earlier than that valued with 2?

The answer is uncertain.

Here is a test:

100 messages are sent in sequence, in which the priority value of the 50th message is 1 and the value of the rest is 8 by default.

  1. CloudQueue queue = createQueue();
  2. for(int i=1;i<=100;i++){
  3. Message message = new Message();
  4. if(i==50){
  5. message.setPriority(1);
  6. }
  7. message.setMessageBody(“msg-“+i, Message.MessageBodyType.RAW_STRING);
  8. Message putMsg = queue.putMessage(message);
  9. System.out.println(“PutMessage MsgId: + putMsg.getMessageId()+”,priority=”+ putMsg.getPriority());
  10. }

The result shows that neither messages called out by pop are generally in sequence nor the msg-50 is first consumed.

Why?

The Message Service tries its best to ensure that messages are consumed in an FIFO manner. However, due to some features of distributed message queues, it cannot be ensured that you can consume messages in the order they are sent. If your service requires FIFO, it is recommended that you add sequence numbers to messages so that the messages are resorted after consumption.

The priority of a message in the Message Service is partial instead of general.

Put it in another way: the messages are stored in multiple channels and those stored in the same channel are called slices. The priority setting only takes effect to a slice in which lies a message with a higher priority.

For example, if the first slice contains a message with a higher priority, the Message Service will first return messages in this slice. If not, the Message Service may still first return messages in this slice.

That’s one of the reasons why the Message Service supports a high QPS but does not ensure the consumed order of messages.

If you have further questions, contact Alibaba Cloud After-Sales Technical Support.

Thank you! We've received your feedback.