Everything was green and nothing was working

At 19:35 a queue alarm paged me. The first thing I looked at was the depth of the queue it was complaining about. The depth was zero.

The second thing I looked at was the Lambda error count for the consumer on that queue. Also zero, flat across the whole window, not a single invocation error.

At that moment the pipeline was 26 minutes behind and had already pushed hundreds of messages into its dead-letter queue, the side queue where SQS parks a message once it has been handed to a consumer too many times without ever being acknowledged. Every number on the dashboard was correct. Every one of them was measuring something other than what I thought it was measuring.

Zero visible, fifteen thousand checked out

I pulled the queue snapshot once I stopped trusting the dashboard.

ApproximateNumberOfMessagesVisible          0
ApproximateNumberOfMessagesNotVisible  15,227
ApproximateAgeOfOldestMessage           1,548  (seconds)
DLQ depth                                 778

What most dashboards label "queue depth" is Visible, and Visible counts messages that nobody currently holds. A message that has been received by a consumer moves to NotVisible and stays there until either the handler deletes it or its visibility timeout expires, which is the window SQS gives a consumer to finish the work before it hands the same message to somebody else.

So a queue reading zero has two very different meanings. Either nothing is waiting, or everything is checked out and none of it is coming back soon. Fifteen thousand messages were sitting in the second state, held inside visibility windows that the consumer itself kept extending.

The only instrument that told the truth here is ApproximateAgeOfOldestMessage. It does not care whether a message is visible or in flight. It reports how long the oldest unacknowledged message has been in the queue, which is the thing you actually mean when you say a pipeline is behind.

Backing off politely spends a budget you cannot see

The consumer handled retries by extending the visibility window rather than by throwing. When a call failed in a way that looked worth retrying, it computed a delay and pushed the message's timeout out by that much:

function backoffSeconds(receiveCount) {
  const base = Math.pow(2, receiveCount - 1) * 60;
  return Math.round(base * (0.8 + Math.random() * 0.4));
}
// delays in seconds: 60, 120, 240, 480, 960

This is a reasonable thing to do when an upstream is rate limiting you. Instead of hammering, you hand the message back and wait.

The part that bit us is that SQS does not count failures. It counts receives. Every one of those backoff cycles ends with the message becoming visible again, getting redelivered, and having its ReceiveCount incremented. With maxReceiveCount set to 5, meaning SQS gives up and dead-letters a message on its sixth delivery, that backoff schedule doubles as the countdown on the entire retry budget. It spends all five deliveries in about 31 minutes, whether or not the handler ever got a real chance at a healthy upstream.

Worse, the whole time that countdown is running, the queue reads as empty.

The error metric is reporting on something else

Lambda's Errors metric counts invocations that failed. Ours never failed. The handler caught the rate-limit error and reported the message back through partial batch failure reporting, where the function returns the IDs of the messages it could not process instead of throwing:

async function handler(event) {
  const batchItemFailures = [];
  for (const record of event.Records) {
    try {
      await processRecord(record);
    } catch (err) {
      if (isRetryable(err)) {
        batchItemFailures.push({ itemIdentifier: record.messageId });
        continue;
      }
      throw err;
    }
  }
  return { batchItemFailures };
}

That return value is a successful invocation. The platform sees a 200, records zero errors, and keeps the queue's bookkeeping honest by making only the listed messages visible again. This is exactly what partial batch responses are for, and it is the right pattern. It also means the metric we had an alarm on was structurally incapable of seeing the failure mode it was installed to catch.

Underneath the flat zero, 63,402 rate-limit errors were logged between the deploy and the page. The baseline for the previous 24 hours was zero.

The alarm was 85 minutes late and correctly configured

The age of the oldest message went from 205 seconds at 18:10 to a peak of 1,557 seconds at 19:15. The page arrived at 19:35.

The alarm was set to fire on 6 of 10 one-minute datapoints at or above 600 seconds. Nothing about that is misconfigured in any obvious way. The age has to climb past ten minutes, and then hold there for the majority of a ten-minute evaluation window. By the time those conditions are satisfiable, the pipeline has already been degraded for the length of the ramp plus the window.

The first symptom appeared at roughly 18:10. The page came 85 minutes later. Nothing was broken in the alarm. The threshold and the evaluation window simply encode a decision that a ten-minute lag is not worth waking anyone for, which is a defensible decision that nobody in the room had ever consciously made.

Two wrong answers before the right one

My first hypothesis was that the consumer had no rate limiting at all. I had a log line in hand that looked like proof, a clean rate-limit error repeated tens of thousands of times. I was reading it backwards. That string came from our own limiter. It was working fine, and saying so at a rate of about one message per second.

My second hypothesis was infrastructure drift. The deployed event subscription was subscribing to a wider set of events than the definition I was reading in my checkout. That felt like a smoking gun for about ten minutes, until I ran git fetch. My local copy was several days stale and the widening was a deliberate, reviewed, merged change. I had spent that ten minutes arguing with a file that no longer existed anywhere but on my laptop.

The guard that guarded the free path

The change that caused this re-enabled a wider event subscription. The author knew the wider subscription meant more traffic hitting a third-party CRM API, and shipped a guard alongside it. The guard skipped the expensive upstream lookup whenever an inbound event carried no associated company id.

The commit message described the guard as avoiding "calls that would have returned null anyway."

Read that sentence twice. If an event has no company id, there is nothing to look up. Those calls were already free. The guard was aimed precisely at the case that was not costing anything.

The traffic that produced the storm was a single customer running a bulk update across their CRM. Every event in that stream carried a populated company id, because that is what a bulk operation on company records produces. The guard never fired once. It could not have. It was a filter on a field whose presence is exactly correlated with the traffic it was meant to suppress.

This is the part I keep thinking about. Nobody was careless. Somebody anticipated the failure and wrote a mitigation into the same change. The mitigation just pointed at the opposite half of the traffic from the half that was dangerous.

Out of memory is strictly worse than throwing

A few weeks later a neighbouring consumer produced a similar-looking backlog through a different mechanism.

When a handler returns batchItemFailures, one bad message retries and the other nine get deleted. When the process is killed for exceeding its memory limit, nothing is returned at all. The invocation fails, and the whole batch of 10 becomes visible again. One bad message now costs ten redeliveries, and each of those ten increments its own ReceiveCount.

The signature was easy to read once I knew to look for it. Max Memory Used was pinned at 254 to 255 MB against a 256 MB limit, and durations were scattered from 2.8 seconds to 52.9 seconds.

That combination is diagnostic. A single message that always fails gives you the opposite pattern, the same input blowing up in roughly the same time every run. A memory ceiling reached at wildly varying durations means the container is accumulating something across invocations and dying whenever it happens to cross the line, regardless of what it is processing at the time. That is a leak in the reused container, not bad data.

A dead-letter queue is a parking lot, not a buffer

Nothing drains a dead-letter queue on its own. A transient credential failure on a long-running worker lasted seven minutes, dead-lettered 16 messages, and kept an alarm lit for six days, because the threshold was 10 and no process on earth was going to take those 16 messages off the queue. The incident was over in seven minutes. The alarm was a fact about the world for almost a week.

Meanwhile the leaking consumer's DLQ grew from 143,766 to 592,594 messages over 18 hours, about 7 per second, while its source queue reported a visible depth of 0 that entire time. A DLQ filling at 7 messages a second next to an empty-looking source queue is a complete description of the failure, and it is a pairing I have never seen on a dashboard.

The other half of this is what happens when you finally push the parked messages back through the source queue, which AWS calls a redrive. We sent 1,168 of them back. All of them bounced straight back within three minutes, producing 3,546 errors, roughly three per message, all of a single pattern. An idempotency check, the thing that stops the same event being applied twice, was finding that the event had already been applied, refusing to write a stale version over a newer one, logging at error level, and failing the message.

That guard was doing the right thing and reporting it the wrong way. An idempotency check that discovers the work is already done has succeeded. If it fails the message instead of acknowledging and deleting it, every duplicate in the system eventually becomes a dead letter, and a large share of what you find parked in a DLQ is the system correctly declining to do something twice, filed under the wrong outcome.

What I changed

Age of oldest message is now the primary lag signal, and depth is demoted to a secondary one. The alarm on age fires on 2 of 3 datapoints at 300 seconds, which would have paged at roughly 18:20 instead of 19:35.

Handled failures get their own counter, emitted by the handler at the point where it appends to batchItemFailures, because the platform error metric cannot see that path. Memory-limit terminations get a separate alarm from thrown errors, since they carry a tenfold amplification that a normal throw does not.

And the idempotency checks acknowledge now. Something like a third of that DLQ was never broken.