This topic describes the concept, use scenarios, precautions, and setting methods of message time-to-live (TTL) in Message Queue for RabbitMQ.

What is Message TTL?

In Message Queue for RabbitMQ, message TTL determines how long messages can be retained in a queue. If the retention period of a message in a queue exceeds the message TTL of the queue, the message expires and is discarded.

Note If you have configured a dead-letter exchange for a queue, the queue transfers expired messages to the dead-letter exchange. Then, the dead-letter exchange routes the messages to a dead-letter queue. You can obtain the messages in the dead-letter queue. For more information, see Dead-letter exchange.

Scenarios

You can use message TTL to prevent message accumulation. For example, the messages of an application dramatically increase within specific periods of time. If you do not need to store expired messages, you can set message TTL to discard expired messages. This prevents message accumulation for your business.

Precautions

  • You can use only the x-message-ttl parameter to set message TTL. The policy parameter of the rabbitmqctl command line tool cannot be used.
  • Message Queue for RabbitMQ allows you to configure message TTL when you create a queue or send messages.

    If you specify a value for message TTL both when you create the queue and send the message, the smaller value is used as the TTL of the message.

  • Message TTL must be set to a non-negative integer, in milliseconds.
  • If the message TTL of a queue is set to 0, after a message is routed to the queue, it is immediately consumed. Otherwise, it is immediately discarded or transferred to the dead-letter exchange of the queue.

Setting methods

Message Queue for RabbitMQ allows you to use the following methods to set message TTL:

  • Message Queue for RabbitMQ console

    You can set message TTL in the Message Queue for RabbitMQ console. For more information, see Mange queues.

  • OpenAPI Explorer

    You can use OpenAPI Explorer to call the CreateQueue API operation to configure message TTL. For more information, see CreateQueue.

  • Open source RabbitMQ SDK

    You can use open source RabbitMQ SDK to set message TTL. For more information, see Supported SDKs. Sample code:

    Map<String, Object> props = new HashMap<String, Object>();
    props.put("x-message-ttl", 1000);
    channel.queueDeclare("myqueue", false, false, false, props);
    
    var props = new Dictionary<string, object>();
    props.Add("x-message-ttl", 1000);
    model.QueueDeclare("myqueue", false, false, false, props);