In this lesson · topic 5 of 7
Topic 5
Watch the age of the oldest message
7 min read
Depth is a count, age is a promise
A dashboard that shows 5,000 messages in a queue does not say whether that is a problem. If the consumers finish 2,500 a second, the backlog is two seconds of work. If they finish 5 a second, it is 1,000 seconds, about 17 minutes. The same depth means two different things.
Queue age is how long the oldest message has been waiting. It is the number users feel, because the oldest message is the one that has waited longest. It also answers the question depth cannot: is anything in here already too late to be useful?
Most brokers expose it in some form. Amazon SQS reports the age of the oldest message as a metric, and Kafka users compute the equivalent from lag and the production rate. If your broker offers neither, stamp each message with its enqueue time and have the consumer log the difference when it starts work.
A worked drain time
An outage ends. The queue holds 12,000 messages, new ones arrive at 6 a second, and three consumers can finish 9 a second between them.
The backlog shrinks only by the spare capacity, which is 9 minus 6, or 3 a second. Drain time is 12,000 divided by 3, which is 4,000 seconds, a little over an hour. During that hour every new message waits behind the backlog, so a message that arrives at the start waits about 12,000 / 9, or 1,333 seconds, even though capacity is “back”.
Double the consumers to six and capacity is 18 a second. Spare capacity is now 12, and the backlog clears in 1,000 seconds, under 17 minutes. Two lessons fall out. Recovery speed depends on the gap between capacity and arrivals, and not on capacity alone. And a system running at 90 percent load recovers five times slower than the same system at 50 percent, because its spare capacity is a fifth as large.
Predict first
Expire what nobody wants any more
Draining 12,000 stale messages in order is often the wrong goal. Suppose the messages are push notifications that mean nothing after five minutes. Of the backlog, everything older than five minutes is dead weight, and processing it delays the fresh messages behind it.
Two tools handle this.
A time to live on the message. The producer, or the queue, marks each message with a deadline. A consumer that pulls an expired message discards it and takes the next. RabbitMQ supports per-message and per-queue TTL, and SQS has a retention period after which messages are deleted.
A deadline check in the consumer. The consumer compares the message’s enqueue time with the current time and skips work that has already lost its value. This works with any broker, and it can use a rule that depends on the message type.
The trade-off is that an expired message is lost on purpose. That is right for a notification and wrong for a payment. Decide per message type, and record what was dropped so someone can see how much.
What age does not tell you
Age tracks the oldest message, and it can be dominated by one stuck item. A single poison message at the head of a strictly ordered queue can make age climb while everything behind it is fine. Look at age together with the rate of completed work. If the rate is healthy and age is climbing, suspect one blocked message.
Sources
- Amazon Web Services, Available CloudWatch metrics for Amazon SQS, including ApproximateAgeOfOldestMessage.
- RabbitMQ documentation, Time-To-Live and Expiration.
- Mor Harchol-Balter, Performance Modeling and Design of Computer Systems, Cambridge University Press, 2013.