DEV Community

Vigilmon
Vigilmon

Posted on

AWS SQS Monitoring: Queue Depth, DLQ Alerts, and Consumer Health

AWS SQS Monitoring: Catch Queue Failures Early

SQS queues fail silently. Messages pile up and users experience delays with no obvious error.

The Critical Metric: ApproximateAgeOfOldestMessage

This tells you how stale your oldest unprocessed message is. If it grows, consumers are not keeping up. Alert when it exceeds 5 minutes.

Consumer Logging

async function processMessages() {
  const { Messages = [] } = await sqs.send(new ReceiveMessageCommand({
    QueueUrl: process.env.QUEUE_URL,
    MaxNumberOfMessages: 10,
    WaitTimeSeconds: 20
  }));

  for (const msg of Messages) {
    const start = Date.now();
    try {
      await handleMessage(JSON.parse(msg.Body));
      await sqs.send(new DeleteMessageCommand({ QueueUrl: process.env.QUEUE_URL, ReceiptHandle: msg.ReceiptHandle }));
      console.log(JSON.stringify({ event: 'processed', duration_ms: Date.now() - start }));
    } catch (err) {
      console.error(JSON.stringify({ event: 'failed', error: err.message }));
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Dead Letter Queue

Set a CloudWatch alarm on DLQ depth at threshold 1. Any DLQ message is a processing failure.

Consumer Health Endpoint

app.get('/health', async (req, res) => {
  const depth = await getQueueDepth();
  res.status(depth < 10000 ? 200 : 503).json({ queue_depth: depth });
});
Enter fullscreen mode Exit fullscreen mode

Monitor with Vigilmon.

Alert Summary

Metric Threshold
OldestMessage age above 5 min
DLQ depth above 0
Consumer health non-200

Top comments (0)