0
0
RabbitMQdevops~5 mins

Message TTL (Time To Live) in RabbitMQ - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes messages in a queue become outdated or irrelevant if they stay too long. Message TTL lets you set a time limit for how long a message can live in a queue before RabbitMQ removes it automatically.
When you want to discard old messages that are no longer useful to consumers.
When you want to prevent queues from filling up with stale data.
When you want to control memory usage by limiting message lifetime.
When you want to implement delayed message processing by setting TTL and dead-lettering.
When you want to ensure real-time systems only process fresh data.
Config File - rabbitmq-ttl-queue.json
rabbitmq-ttl-queue.json
{
  "rabbitmq_queues": [
    {
      "name": "my-ttl-queue",
      "durable": true,
      "arguments": {
        "x-message-ttl": 60000
      }
    }
  ]
}

This JSON defines a durable queue named my-ttl-queue with a message TTL of 60000 milliseconds (60 seconds). The x-message-ttl argument tells RabbitMQ to discard messages older than 60 seconds automatically.

Commands
This command creates a durable queue named my-ttl-queue with a message TTL of 60000 milliseconds (60 seconds). Messages older than this will be removed automatically.
Terminal
rabbitmqadmin declare queue name=my-ttl-queue durable=true arguments='{\"x-message-ttl\":60000}'
Expected OutputExpected
Successfully declared queue 'my-ttl-queue'
durable=true - Ensures the queue survives RabbitMQ restarts
arguments='{"x-message-ttl":60000}' - Sets the message TTL to 60 seconds
This command sends a message with the text 'Hello, TTL!' to the my-ttl-queue queue.
Terminal
rabbitmqadmin publish exchange=amq.default routing_key=my-ttl-queue payload='Hello, TTL!'
Expected OutputExpected
Published message to exchange 'amq.default' with routing key 'my-ttl-queue'
This command lists all queues with the number of ready and unacknowledged messages, so you can check if the message is still in the queue.
Terminal
rabbitmqctl list_queues name messages_ready messages_unacknowledged
Expected OutputExpected
Listing queues ... my-ttl-queue 1 0
Wait 65 seconds to allow the message TTL to expire and the message to be removed automatically.
Terminal
sleep 65
Expected OutputExpected
No output (command runs silently)
Check the queue again after waiting to confirm the message has expired and been removed.
Terminal
rabbitmqctl list_queues name messages_ready messages_unacknowledged
Expected OutputExpected
Listing queues ... my-ttl-queue 0 0
Key Concept

If you remember nothing else from this pattern, remember: message TTL automatically removes messages older than the set time to keep queues fresh and manageable.

Common Mistakes
Setting message TTL on the wrong queue or forgetting to declare the queue with the TTL argument.
Messages will not expire if the queue does not have the TTL argument set.
Always declare the queue with the 'x-message-ttl' argument to enable message expiration.
Setting TTL too short without considering message processing time.
Messages may expire before consumers can process them, causing data loss.
Set TTL long enough to allow consumers to process messages but short enough to discard stale data.
Expecting TTL to delay message delivery instead of expire messages.
TTL only removes messages after the time expires; it does not delay message delivery.
Use TTL with dead-letter exchanges for delayed message processing.
Summary
Declare a queue with the 'x-message-ttl' argument to set message expiration time.
Publish messages to the TTL-enabled queue as usual.
Check queue message counts before and after TTL expiration to see messages removed automatically.