A RabbitMQ TTL Silently Threw Away 6,274 Transcription Jobs

My transcription backlog went from 5,101 messages to zero in three days. I did not do anything to make that happen. For a few minutes I even considered that something had gone right. Nothing had. The queue had not emptied because the work was done. It had emptied because the work expired.

The symptom

I run a RabbitMQ queue that feeds speech-to-text workers. Episodes to transcribe are published as messages, workers pick them up, and results land in storage. The queue for long episodes had been backed up for weeks, hovering around 5,000 messages. On a Sunday I checked it and it held 5,101.

Three days later I checked again:

jobs.long          messages=0      consumers=0
jobs.long.failed   messages=6274   consumers=0   arguments=[]

The main queue was empty. A companion queue named .failed, a dead-letter queue, held 6,274 messages. On the earlier date every .failed queue had been at zero.

What I wrongly assumed

I had two wrong assumptions in a row.

The first was that the long queue was just slow. Throughput on the fleet was around 68 episodes per hour, and at that rate a 5,000-message backlog takes three days of continuous work, which is about right for how long it had been sitting there. A slow drain was a comfortable explanation.

The second came when I saw the queue empty. I briefly assumed the workers must have finally caught up. But that would have required them to process thousands of messages in a window where they were mostly scaled down on their schedule. The arithmetic did not work, and the dead-letter count was the giveaway. Something had moved 6,274 messages out of the main queue, and it was not a consumer.

How I found the real cause

I looked at the queue’s declared arguments.

jobs.long   x-message-ttl = 21600000 ms = 6 HOURS

Six hours. A message that sits in that queue for more than six hours is discarded, or, if the queue has a dead-letter exchange configured, moved there.

Now do the sums. The fleet processes about 68 episodes per hour. Within a six-hour TTL, workers can reach at most about 408 messages. With 5,101 messages in the queue, about 4,693 were mathematically guaranteed to expire before any worker looked at them. The backlog was not slow. It was structurally impossible to drain. A TTL shorter than the drain time turns a capacity problem into silent data loss.

It got worse when I compared the tiers. The shorter-episode queue had a 48-hour TTL. The queues for long episodes, very quick episodes and huge episodes all had 6 hours. The queue holding the episodes that take the longest to transcribe had one of the shortest lifetimes. The tiers were backwards.

And the dead-letter queue was a dead end. It had no consumers, no TTL and no dead-letter exchange of its own. The 6,274 messages parked there were never going to be retried by anything. Unless someone moved them by hand, they were gone. Nobody was watching that queue, so it had quietly collected thousands of lost jobs.

The fix

The change itself was simple: raise the TTL from 6 hours (21,600,000 ms) to 48 hours (172,800,000 ms) on the scheduler and both worker deployments, and raise the scheduler’s republish interval to match. Then delete the empty queues so they would be redeclared with the new arguments.

That last part bit me.

The gotcha that cost me five minutes of downtime

RabbitMQ will not let you redeclare an existing queue with different arguments. If a queue already exists with x-message-ttl=21600000 and a client tries to declare it with x-message-ttl=172800000, the broker refuses:

PRECONDITION_FAILED - inequivalent arg 'x-message-ttl' for queue ...

I had assumed the TTL setting applied only to the long queue. It did not. That one environment variable governed the quick queue and the huge queue as well. I changed it while those two queues still existed with the old value, and the scheduler went into a retry loop on the failed declaration. That blocked publishing and it blocked the dead-letter consumer, so for about five minutes the scheduler was effectively down.

The rule I wrote down: if that variable is ever changed again, delete all of the non-short queues in the same maintenance window, not just the one you are thinking about.

The better outcome

Once the scheduler could connect again, something nice happened. The scheduler already had a republish loop that consumed from the dead-letter queue and put messages back on the main queue. It had never worked, because the dead-letter consumer had been failing to connect. With the precondition problem fixed, that loop started and drained the dead-letter queue from 6,925 messages to zero, all by itself. No manual shovel was needed. The main long queue then held 7,425 messages.

Expiry is no longer terminal. An expired message dead-letters, gets republished, and tries again. The recycle loop is closed.

What is still true

The fix removes the data loss. It does not remove the capacity problem. 7,425 messages at 68 per hour would take about 109 hours, longer than the new 48-hour lifetime, so some will still cycle through expiry and republish before they are reached. The real fix for throughput was elsewhere. I wrote up how an 8-thread worker was five times less efficient per core than a 4-thread one, which freed up a good portion of the lost capacity. Even so, I treat the queue depth as something to watch over a whole week, not a single reading.

Which reminds me of the follow-up I got wrong. I measured the queue twice and told myself it was draining at about 66 episodes per day. Then a third reading showed it had risen by 476 in three days. The workers only run on a schedule, so two readings from the same part of the cycle had given me a rate with the wrong sign. For anything that scales up and down on a schedule, compare the same phase, or measure across a full cycle.

What to check in your own queues

# depth, consumers and arguments for every queue
rabbitmqctl list_queues name messages consumers arguments

# is there a dead-letter queue, and does anything read from it?
rabbitmqctl list_queues name messages consumers | grep -i failed

The check I would automate

The simplest guard is arithmetic you can run in a script: queue depth divided by observed throughput, compared to the TTL. If the drain time exceeds the TTL, something is going to expire.

depth=5101        # messages waiting
rate=68           # episodes per hour, measured, not hoped for
ttl_hours=6
echo "drain time: $((depth / rate)) hours, TTL: ${ttl_hours} hours"
# drain time: 75 hours, TTL: 6 hours  -> expect most of the queue to expire

Seventy-five hours against a six-hour TTL. If I had written that two-line calculation on the day I set the TTL, it would have been obvious. A second alert on the dead-letter queue depth, anything above zero for more than an hour, would have caught the loss on day one rather than day three.

Lessons

  • A TTL must be longer than your worst-case drain time. Otherwise the queue is a timer on data loss. Compute backlog divided by throughput and compare it to the TTL.
  • Order your tiers by how long the work takes. The queue with the longest jobs should have the longest lifetime, not the shortest.
  • Dead-letter queues need a consumer or an alert. A dead-letter queue with nobody watching is a trash can labelled “failed” that nobody empties.
  • An empty queue is not a success signal. Ask where the messages went.
  • Queue arguments are immutable. Changing them means deleting and redeclaring. Know every queue your setting touches before you change it.
  • Do not infer a rate from two samples in the same phase of a cycle. I reversed my own conclusion on the third reading.

By Alex Rivers

I write hands-on guides to web development and programming: JavaScript and React patterns, Python scripting, CSS layout and the developer tools that tie it all together. I like explanations that start with working code and end with knowing why it works. If a post saves you an afternoon of debugging, it did its job.

Leave a Reply