9
votes

I have a AWS Lambda function which is triggered by SQS. This function is triggered approximately 100 times daily, but request count to the SQS queue is approximately 20.000 times daily. I don't understand why the number of requests made to the SQS is too high. My expectation is that the number of requests made to the SQS should be same with the Lambda invocation.

I have only one Lambda function and one SQS queue in my account.

Can be related with polling of SQS queue? I tried to change the polling interval of SQS from the queue configuration but nothing changed. Another possibility is to change polling interval from Lambda function configuration. However, I cannot find any related parameter.

Briefy, I want to reduce number of SQS request, how can i do that while invoking Lmabda function with SQS?

2
What do you mean by "change the polling interval of SQS"? Also, where do you see the 20,000 requests listed?John Rotenstein
Polling interval actually is "Receive Message Wait Time". Requests can be seen at billing page. Specifically under the title: "All Free Tier services by usage".Mustafa İlhan

2 Answers

17
votes

When using SQS as an event source for AWS Lambda, AWS Lambda regularly polls the configured SQS queue to fetch new messages. While the official documentation isn't clear really about that, the blog post announcing that feature goes into the details:

When an SQS event source mapping is initially created and enabled, or when messages first appear after a period with no traffic, then the Lambda service will begin polling the SQS queue using five parallel long-polling connections.

According to the AWS documentation, the default duration for a long poll from AWS Lambda to SQS is 20 seconds.

That results in five requests to SQS every 20 seconds for AWS Lambda functions without significant load, which sums up to the ~21600 per day, which is close to the 20000 you're experiencing.

While increasing the long poll duration seems like an easy way to decrease the number of requests, that's not possible, as the 20 seconds AWS Lambda is using by default is already the maximum possible duration for an SQS queue. I'm afraid there is no easy way to decrease the requests to SQS, when using it as event source for AWS Lambda. Instead depending it could be worth evaluating if another event source, like SNS, would fit your use case as well.

3
votes

Here is how we originally implemented when there is no SQS trigger.

Create a SNS trigger with the SQS Cloudwatch Metric

ApproximateNumberOfMessagesVisible > 0

Trigger a Lambda from SNS, Read Messages from SQS and deliver it to whichever the lambda needs the message.

Alternatively, you can use Kinesis to deliver it to Lambda.

SQS --> Cloudwatch (Trigger Lambda) --> Lambda(Reads Messages) --> Kinesis (Set Batch Size) --> Lambda (Handle Actual Message)

You can also use Kinesis directly but there is no delayed delivery.

Hope it helps.