0
0
RabbitMQdevops~10 mins

Message TTL (Time To Live) in RabbitMQ - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Message TTL (Time To Live)
Message Published
Message Enters Queue
TTL Timer Starts
TTL Expires?
NoMessage Waits
Yes
Message Discarded or Dead-lettered
End
This flow shows how a message with TTL is published, waits in the queue until TTL expires, then is discarded or sent to a dead-letter queue.
Execution Sample
RabbitMQ
rabbitmqctl set_policy TTL "^myqueue$" '{"message-ttl":5000}'
rabbitmqadmin publish routing_key=myqueue payload="Hello"
Set a 5-second TTL on 'myqueue' and publish a message to it.
Process Table
StepActionMessage StateTTL Timer (ms)Result
1Publish message to 'myqueue'Message in queue5000Message accepted
2Wait 3000 msMessage in queue2000Message still valid
3Wait 2000 msMessage in queue0TTL expired
4Remove message from queueMessage removedN/AMessage discarded or dead-lettered
5Check queueQueue emptyN/ANo messages remain
💡 TTL expired at step 3, message removed at step 4, queue empty at step 5
Status Tracker
VariableStartAfter 1After 2After 3After 4Final
Message StateNot publishedIn queueIn queueTTL expiredRemovedRemoved
TTL Timer (ms)N/A500020000N/AN/A
Key Moments - 2 Insights
Why does the message get removed exactly after 5000 ms?
Because the TTL timer counts down from 5000 ms when the message enters the queue, and at step 3 in the execution_table, TTL reaches 0, triggering removal at step 4.
What happens if the message is consumed before TTL expires?
If consumed earlier, the message is removed normally and TTL no longer matters. This is not shown in the current table but is important to know.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the TTL timer value after waiting 3000 ms?
A3000 ms
B5000 ms
C2000 ms
D0 ms
💡 Hint
Check the TTL Timer column at step 2 in the execution_table.
At which step does the message get removed from the queue?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the Result column describing message removal.
If the TTL was set to 10000 ms instead of 5000 ms, what would be the TTL timer at step 3?
A7000 ms
B5000 ms
C8000 ms
D0 ms
💡 Hint
TTL counts down from initial value minus elapsed time; step 3 is after 5000 ms total wait.
Concept Snapshot
Message TTL in RabbitMQ sets how long a message lives in a queue.
When TTL expires, the message is removed or dead-lettered.
Set TTL via policies or queue arguments.
TTL countdown starts when message enters the queue.
Messages consumed before TTL expire normally.
Useful for expiring stale messages automatically.
Full Transcript
Message TTL (Time To Live) in RabbitMQ controls how long a message stays in a queue before it is discarded or dead-lettered. When a message is published to a queue with a TTL set, a timer starts counting down from the TTL value. If the message is not consumed before the timer reaches zero, the message is removed from the queue. This process helps remove stale messages automatically. The TTL can be set using policies or queue arguments. If a message is consumed before TTL expires, it is removed normally and TTL no longer applies. The visual execution shows the message entering the queue, the TTL timer counting down, and the message removal after TTL expiration.