Search code examples
amazon-web-servicesaws-lambdaamazon-sqs

Not all SQS messages end up in Lambda: most just disappear


I have an AWS SQS Queue (standard, non-FIFO) that has a Lambda function as a consumer.

Whenever I send a bunch of messages (usually around 10 at a time) to the queue, only about 2 get picked up by lambda (verified in CloudWatch Logs). The others disappear from the queue.

The Lambda batch size is set to 1, so I would expect all 10 messages to sit in the queue and get picked up by Lambda one by one, but that's not happening. I'm using CloudWatch to check what Lambda is doing, and there is no trace of the missing messages.

I verified in Lambda that it only gets one message every time, by logging the size of the event.Records array (which is always 1).

The Queue also has a Dead Letter Queue. Initially the Maximum Receives was set to 1. When I increased that to 3, more messages were getting picked up after the queues Visibility timeout, but still only a few.

My Queue settings

  • Visibility timeout: 2 minutes
  • Delivery delay: 0 seconds
  • Receive message wait time: 5 seconds
  • Message retention period: 4 days
  • Maximum message size: 256kb

I'm wondering why the messages aren't being processed, but instead disappear?


Solution

  • Turns out this was related to the Reserved Concurrency of the Lambda function. My concurrency was set to 1, which caused issues.

    My expectation SQS messages will remain in the queue until there's a Lambda function available to pick them up.

    In reality Messages that are not picked up by Lambda because the function is throttled, and after the Visibility Timeout are treated as failed message.

    There's an excellent blog post about this issue: https://data.solita.fi/lessons-learned-from-combining-sqs-and-lambda-in-a-data-project/