All Products
Search
Document Center

ApsaraMQ for RabbitMQ:Message TTL

Last Updated:Sep 01, 2023

This topic introduces the terms that are used for message time-to-live (TTL) in ApsaraMQ for RabbitMQ. This topic also describes the use scenarios, usage notes, and setting methods of message TTL.

What is Message TTL?

In ApsaraMQ 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 is processed by using one of the following methods:

  • If you did not configure a dead-letter exchange for the queue, the message is discarded by the queue.

  • If you configured a dead-letter exchange for the queue, the message is discarded by the queue and forwarded to the dead-letter exchange. Then, the dead-letter exchange routes the message to the dead-letter queue. You can obtain the message in the dead-letter queue. For more information, see Dead-letter exchanges.

Use 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 specify message TTL to discard expired messages. This prevents message accumulation for your business.

Rules for specifying message TTL

  • ApsaraMQ for RabbitMQ allows you to specify message TTL by using the x-message-ttl or expiration argument. ApsaraMQ for RabbitMQ does not allow you to specify message TTL by using the policy parameter of the rabbitmqctl tool.

  • You can specify the message TTL of a queue when you create the queue in the ApsaraMQ for RabbitMQ console. You can also specify the message TTL when you send the message.

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

  • The actual message TTL of a delayed message is calculated by using the following formula: Actual message TTL of a delayed message = min {Message-level TTL, queue-level TTL} + delay time. For more information, see Delayed messages.

  • The maximum value of message TTL is the same as the scheduled interval specified for sending scheduled messages in an instance. The value of message TTL must be a non-negative integer, in milliseconds.

  • If the message TTL of a queue is specified as 0, after a message is routed to the queue, the message is discarded or forwarded to the dead-letter exchange of the queue unless it is immediately consumed.

Setting methods

ApsaraMQ for RabbitMQ allows you to use the following methods to specify message TTL:

  • ApsaraMQ for RabbitMQ console

    You can specify the message TTL of a queue in the ApsaraMQ for RabbitMQ console. For more information, see Manage queues.

  • OpenAPI Explorer

    You can use OpenAPI Explorer to call the CreateQueue operation to specify the message TTL of a queue. For more information, see CreateQueue.

  • Open source RabbitMQ SDKs

    You can use an open source RabbitMQ SDK to specify the message TTL of a queue or a message. For more information, see Supported SDKs. Sample code:

    • Specify the message TTL of a queue by using the x-message-ttl argument

      Map<String, Object> props = new HashMap<String, Object>();
      props.put("x-message-ttl", 1000);
      channel.queueDeclare("myqueue", false, false, false, props);
    • Specify the message TTL of a message by using the expiration argument

      byte[] messageBodyBytes = "test".getBytes();
      AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
              .expiration("1000")
              .build();
      channel.basicPublish("myqueue", peoperties, messageBodyBytes);