All Products
Search
Document Center

:How to set a message ID

Last Updated:Aug 27, 2026

To track and identify messages, you can set the message ID property in the producer client of ApsaraMQ for RabbitMQ to assign a unique identifier to each message. This topic describes what a message ID is and how to set it.

What is a message ID

A message ID is an optional message property. It is a short string that is typically set to a unique value for business purposes. This is useful for scenarios that require unique messages, such as tracking sales orders or tickets. ApsaraMQ for RabbitMQ does not perform server-side idempotence checks on messages. To ensure message idempotence, you must also handle this in the consumer client of ApsaraMQ for RabbitMQ. Message idempotence means that if a message is retried multiple times, the result of consuming the duplicate message is the same as consuming it once, without causing side effects. This requires setting a unique message ID for each message. For more information, see Message idempotence.

Methods

In the ApsaraMQ for RabbitMQ producer client, you can set the message-id property of Basic.Properties. The sample code is as follows:

Java

AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
    .messageId("messageid")
    .build();
channel.basicPublish("<ExchangeName>", "RoutingKey", true, props,
    "Message body".getBytes(StandardCharsets.UTF_8));

Python

properties = pika.BasicProperties(
    app_id='example-publisher',
    content_type='application/json',
    message_id='messageid'
)

PHP

$msg = new AMQPMessage($msgBody, [
    'application_headers' => $amqpTable,
    'content_type'        => 'text/plain',
    'delivery_mode'       => 2,
    'message_id'          => 'messageid',
]);

Go

err = ch.Publish(
    "helloExchange",  // exchange
    "hello",          // routing key
    false,            // mandatory
    false,            // immediate
    amqp.Publishing{
        ContentType: "text/plain",
        Body:        []byte(body),
        MessageId:   "messageId",
    },
)

Node.js

channel.publish("exchange", "routingKey", Buffer.from("Message body"), {
    messageId: "messageid",
});