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

SQS LAMBDA integration via event source mappings


Let's assume the following scenario:

I integrated Lambda and SQS, via Event Source Mapping. With the following settings:

  • visibility timeout -> 60 seconds
  • Batch size -> 10

I received an event with 10 messages in the lambda function and I will start processing it in parallel using threads. 8 of the 10 messages were successfully processed and deleted, two failed to process and the handler was made for this failure without "exploding" the lambda function. All of this occurred before the visibility timeout expired.

Soon. The lambda function will respond without errors. And the entire batch will be deleted. But actually two messages should go back to the queue.

How to deal with this situation?

Resend messages to the queue. But I don't think the best way to solve the problem


Solution

  • First, you aren't supposed to delete the SQS messages from the queue in your Lambda function's code if you are using the SQS event source mapping. You are supposed to have your Lambda function return a success or error message, and based on that message the Lambda service will either consider the messages as processed, and delete the messages from the queue, or it will return the messages to the queue to be processed again.

    In your scenario, where you successfully process 8 messages, and failed to process 2 messages, your Lambda function should implement partial batch responses, which allows it to notify the Lambda service of exactly which messages were processed successfully, and which were not. The Lambda service will use that information to delete the successful messages from the queue, and only re-queue the failed messages.


    Alternatively, you could delete the successful messages from the queue, using code in your Lambda function to perform the SQS deletes, then at the end check if there were any failures and return a failure response instead of a success response. At that point the Lambda service will take any of the messages that still exist in the queue, and mark them as available for processing again.