Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda with SQSEvent & large batch size invokes multiple instances each handling few items

Bit of background, I'm using Serverless and .Net to create a lambda with a SQS trigger. The event trigger is set with batch size of 10k and wait time (Batch Window ie MaximumBatchingWindowInSeconds) of 30 seconds. Queue's visibility timeout is set to almost 16 minutes.

Now that I've set the lambda to reserved concurrency of only 1 and ran a test where I send 100 items to the queue & was hoping to see only one lambda invocation with exactly those 100 items.

Problem was that it separated the items in the queue and invoked the lambda five times instead, causing five packages to be created as part of the lambda's functionality instead of the one package I wanted. (FYI the lambda's output creates packages in s3 of the messages. I want to have fewer packages that are large.)

Now the question: Is this the expected behavior? and if so why is it so when I've set the queue to accumulate up to 10k items and instead it settled for 15.

According to the aws docs the lambda can grab fewer messages than the batchSize if the payload is larger than 256kb but my messages are very small and 100 messages are no where near 256kb. So that can't be the cause.

Suggestions for alternatives to dealing with this issue are also welcome, right now I'm thinking of running an event bridge scheduler that calls lambda with SQS ReceiveMessage api and creates a single package but then I also have to make sure to properly delete the queue afterwards.

I'm a bit clueless here, I'd appreciate any ideas you guys have. Thanks.

like image 802
Midnight_Blaze Avatar asked Apr 13 '21 13:04

Midnight_Blaze


People also ask

Can EventBridge trigger Lambda?

With EventBridge (CloudWatch Events), you can create rules that match selected events in the stream and route them to your AWS Lambda function to take action. For example, you can automatically invoke an AWS Lambda function to log the state of an EC2 instance or AutoScaling group.

Can Lambda listen to multiple queues?

A Lambda function can process items from multiple queues (using one Lambda event source for each queue). You can use the same queue with multiple Lambda functions.

Can Lambda listen to multiple SQS?

Yes you can, a single Lambda function can process messages from more than one SQS queue without a problem.

How to test SQS event source with Lambda?

You just created a Lambda function with an SQS event source. You used AWS KMS to encrypt the messages as they passed between Amazon SQS and Lambda. Then you created a function to push messages to the queue to test the event source.

What is AWS Lambda in sqsevent?

AWS Lambda takes over polling and invoking concurrent Lambda functions to chew through the queue with chunks of messages up to the configured batch size (In our case we configured the batch size to be 10 messages). The Lambda function processes each message in the SQSEvent.

How do I send an SQS message to a lambda function?

Save the following JSON as a file named input.txt . The preceding JSON simulates an event that Amazon SQS might send to your Lambda function, where "body" contains the actual message from the queue. Run the following invoke AWS CLI command.

How to use Amazon SQS and Lambda to solve a security challenge?

To solve this challenge, you decide to use Amazon SQS and Lambda. Amazon SQS is ideal for transmitting a large volume of data, at any throughput, without losing messages or requiring other services to be available. Create an AWS KMS key to encrypt each message body using Amazon SQS server-side encryption (SSE).


1 Answers

Problem was that it separated the items in the queue and invoked the lambda five times instead, causing five packages to be created as part of the lambda's functionality instead of the one package I wanted.

I think that this is probably because there are five SQS pooling threads that lambda uses to pool SQS. From AWS blog:

Lambda service will begin polling the SQS queue using five parallel long-polling connections.

So even though you had reserved concurrency of 1, lambda still uses the 5 threads (you can't control that), and your SQS messages were distributed into these threads. Then, each thread invoked your lambda function, one by one, resulting in observed 5 invocations.

like image 87
Marcin Avatar answered Jun 06 '23 18:06

Marcin