How does using SQS help reduce Lambda costs?

# How SQS Can Reduce Lambda Costs

Lambda costs are primarily driven by two factors:

  1. Memory allocated
  2. Execution time

Using an SQS queue as an event source for Lambda functions can help reduce costs in the following ways:

Batching Messages

By configuring a batch size and batch window for the SQS event source mapping, Lambda can process messages in batches instead of one at a time. This means:

  • Fewer Lambda invocations
  • Shorter execution time per invocation

For example, processing 10 messages in a single invocation will be cheaper than 10 separate invocations, even if the total execution time is the same.

You can configure the batch size up to 10,000 messages and the batch window up to 20 seconds. Larger batch sizes and windows generally mean lower costs.

{
  "BatchSize": 100,
  "BatchWindow": 5  
}

Adjusting Memory

Lambda will allocate more memory when processing larger batches of messages to optimize performance.

You can define memory “steps” based on the batch size to avoid overprovisioning memory.

For example:

  • 1-100 messages: 128 MB
  • 100-600 messages: 384 MB
  • 600-1100 messages: 512 MB

This ensures the optimal memory is used for a given batch size.

Avoiding Empty Receives

Long polling SQS queues can help avoid “empty receives”, which are billed requests that return no messages.

By configuring a long polling duration of up to 20 seconds, Lambda will wait until a message is available before invoking the function. This minimizes empty receives.

You can enable long polling in your Lambda function code or at the queue level.

ALSO READ  Ace Your AWS Transfer Family Interview: Top 10 Questions and Answers

Following these best practices around batching, memory allocation, and long polling can help optimize the cost of Lambda functions triggered by SQS messages. Let me know if you have any other questions!

Sources

  1. https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/reducing-costs.html
  2. https://aws.amazon.com/blogs/compute/estimating-cost-for-amazon-sqs-message-processing-using-aws-lambda/
  3. https://repost.aws/knowledge-center/sqs-high-charges

Abhay Singh

I'm Abhay Singh, an Architect with 9 Years of It experience. AWS Certified Solutions Architect.

More Reading

Post navigation

Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *