Set an appropriate visibility timeout when using SQS as an event source

Serverless Tip #12: Set an appropriate visibility timeout when using SQS as an event source

When you use SQS as an event source for Lambda functions, the Lambda service polls messages from the queue and deletes them after processing them.

One of the settings when configuring SQS and Lambda is the visibility timeout. This timeout ensures that while a consumer is processing a message, the message is invisible to other consumers. You must ensure that this timeout is longer than it takes to process a message or a batch of messages.

Imagine a function that gets messages in batches of 50, each taking one second to process. If the visibility timeout is 30 seconds, you will inevitably process messages twice.

The first function will get a batch of 50 and get to work. After 30 seconds, it will have processed about 30 messages. However, at that time, the messages will be visible again in the queue, kicking off another function with a batch of 50.

To avoid unnecessary invocations and duplicate processing, set an appropriate visibility timeout, depending on your batch size and the time it takes to process one message.