0
0
RabbitMQdevops~10 mins

Queue TTL and auto-expiry in RabbitMQ - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Queue TTL and auto-expiry
Message Enters Queue
Start TTL Timer
TTL Expires?
NoMessage Waits
Yes
Message Auto-Deleted
Queue Empty?
YesQueue Auto-Deleted
No
Wait for Next Message
Messages enter the queue and start a timer (TTL). When TTL expires, messages auto-delete. If the queue becomes empty and idle, it auto-expires.
Execution Sample
RabbitMQ
rabbitmqctl set_policy TTL "^myqueue$" '{"message-ttl":5000,"expires":10000}' --apply-to queues

# Publish message to 'myqueue'

# Wait 6 seconds

# Check queue state
Set TTL and auto-expiry on a queue, publish a message, wait for TTL to expire, then check queue state.
Process Table
StepActionMessage StateQueue StateResult
1Set TTL and expires policy on 'myqueue'No messagesQueue existsPolicy applied
2Publish message to 'myqueue'Message added, TTL timer started (5s)Queue has 1 messageMessage enqueued
3Wait 4 secondsMessage TTL running (1s left)Queue has 1 messageMessage still present
4Wait 2 more seconds (total 6s)Message TTL expired, message auto-deletedQueue emptyMessage removed
5Wait 5 more seconds (total 11s)No messagesQueue idle > expires (10s), queue auto-deletedQueue removed
6Check queue existenceNo messagesQueue does not existQueue auto-expired
💡 Queue auto-deleted after being idle longer than expires time (10s) with no messages
Status Tracker
VariableStartAfter Step 2After Step 4After Step 6
Message Count0100
Queue ExistsYesYesYesNo
Message TTL TimerN/ARunning (5s)ExpiredN/A
Queue Idle TimerN/AResetStarted (empty)Expired
Key Moments - 3 Insights
Why does the message disappear after 5 seconds even if not consumed?
Because the message TTL is set to 5000 ms, so after 5 seconds the message expires and RabbitMQ auto-deletes it (see execution_table step 4).
Why does the queue itself get deleted after 10 seconds?
The queue has an 'expires' setting of 10000 ms, so if it stays empty and unused for 10 seconds, RabbitMQ auto-deletes the queue (see execution_table step 5).
What happens if a new message arrives before the queue expires?
The queue idle timer resets, so the queue will not auto-delete as long as messages keep arriving before the expires time (implied by variable_tracker 'Queue Idle Timer' reset).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the message state?
AMessage not yet added to queue
BMessage TTL expired and message deleted
CMessage TTL running with 1 second left
DQueue auto-deleted
💡 Hint
Check the 'Message State' column in execution_table row for step 3
At which step does the queue get auto-deleted due to inactivity?
AStep 5
BStep 4
CStep 2
DStep 6
💡 Hint
Look at the 'Queue State' and 'Result' columns in execution_table rows
If the message TTL was set to 10000 ms instead of 5000 ms, what would change in the execution_table?
AMessage would expire at step 4 instead of step 6
BMessage would expire later, so message would still be present at step 4
CMessage would expire after step 5, delaying auto-deletion
DQueue would auto-delete earlier
💡 Hint
Compare message TTL times and message state at step 4 in execution_table
Concept Snapshot
Queue TTL and auto-expiry in RabbitMQ:
- message-ttl sets how long messages live in queue (ms)
- expires sets how long an empty queue lives (ms)
- expired messages auto-delete after TTL
- empty queues auto-delete after expires time
- policies apply TTL and expires settings automatically
Full Transcript
This visual trace shows how RabbitMQ handles message TTL and queue auto-expiry. When a message enters a queue with a TTL policy, a timer starts. If the message is not consumed before TTL expires, RabbitMQ deletes it automatically. If the queue remains empty and unused longer than the expires time, RabbitMQ deletes the queue itself. The execution table tracks each step: setting policies, publishing messages, waiting for TTL expiry, and queue auto-deletion. Variable tracking shows message count, queue existence, and timers. Key moments clarify why messages and queues disappear automatically. The quiz tests understanding of message states and timing of auto-deletion. This helps beginners see how TTL and auto-expiry work in RabbitMQ queues.