Skip to main content
AWS Lambda Concurrency when SQS FIFO Queue as Trigger

AWS Lambda Concurrency when SQS FIFO Queue as Trigger

·4 mins· loading ·
AWS Serverless Lambda SQS Concurrency
Pubudu Jayawardana
Author
Pubudu Jayawardana
Cloud Engineer, AWS Community Builder

It is a very common pattern that you use a Lambda function as the consumer of a SQS FIFO queue. Also SQS FIFO is one of the few services that supports message ordering.

In this blog post, I am going to discuss how Lambda concurrency is determined when a SQS FIFO queue is configured as the source of the function.

Message Group ID
#

Message group id is the property of a message that indicates the group within a FIFO queue it belongs to. All the messages that are in the same group will be processed one by one as per the order they are added to the queue.

When processing, sequences of the messages that are in different groups are independent from one another. So, messages with different group ids might be processed irrelevant of the order they are added to the queue.

Lambda concurrency vs Message Group ID
#

Lambda concurrency is how many Lambda executions can be run at a given time. It is interesting how the lambda concurrency is calculated when it comes to SQS FIFO queue as the source.

  1. First, SQS FIFO queue looks through the first 20,000 messages that are available in the queue.

  2. Then, it gets the distinct number of message group ids that those 20,000 messages belong to.

  3. Then, Lambda service initialise a Lambda execution per message group id.

  4. So that concurrency is set based on the no of message group ids available in the 20,000 messages.

For an example:

  1. Let’s assume there are more than 20,000 messages in the queue that belong to 3 message group ids and messages with all 3 message ids included in the first 20,000 messages.

  2. Then, Lambda service polls the first 20,000 messages.

  3. Since there are 3 distinct message ids within this 20,000 messages, at this point, Lambda concurrency will be set to 3.

  4. Likewise after each poll, Lambda service calculates the concurrency based on the available message group ids.

Image: Messages in queue with different group ids
Image: Messages in queue with different group ids

Test this yourself
#

I have created a sample application to test this scenario. You can deploy it to your AWS account using CDK with Python.

  1. Clone the repository: https://github.com/pubudusj/sqs-fifo-lambda-concurrency-test

  2. Run pip install -r requirements.txt to install necessary dependancies.

  3. Run cdk deploy to deploy the stack.

  4. This stack consists of a SQS FIFO queue, a consumer Lambda function and another Lambda function to put some sample messages into the SQS queue for testing.

  5. When deployed, the consumer Lambda function has the SQS FIFO queue as the source but it is not yet enabled.

  6. Once deployed, first run the message generator Lambda function. This will add 60,000 messages that belong to 3 message groups into the SQS queue.

  7. Once all messages are in the queue, enable the SQS source of consumer Lambda function using AWS console or change the CDK code here.

  8. This will start consuming the messages in the SQS queue.

  9. Once all the messages are processed, go to the Lambda’s CloudWatch metrics and see the ‘Total concurrent executions’ graph.

  10. You can see the maximum concurrency that was set during the processing is only 3.

Image: Concurrent executions
Images: Concurrent executions

Conclusion
#

As you see, message group id directly affect the Lambda concurrency when processing messages in a SQS FIFO queue. Try to avoid inserting more messages with the same message group ID as this may lead to large backlog of messages.

Therefore, as per your business requirement, using a suitable group ID is the key to maximize concurrency when processing data from an SQS FIFO queue. However, if you need a set of messages to be processed sequentially, they must be assigned to the same group. Keeping the number of messages within each group as small as possible ensures that the messages are processed in order while allowing for higher parallelism across different groups.

Useful Links #

SQS FIFO Queue Developer Guide -  https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/FIFO-queues.html

High throughput for FIFO queues -  https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/high-throughput-fifo.html

Related

Self healing Serverless App with Lambda Destinations and EventBridge
·6 mins· loading
AWS Lambda Serverless EventBridge EventBridge Pipe SQS
In this blog post, explains how a Lambda based Serverless application reacts to the errors and attempts to re-drive messages to the origin in a controlled manner.
Understanding Lambda Concurrency
·3 mins· loading
AWS Serverless Lambda Concurrency
This blog post explains the behaviour of Lambda when they run simultaneously.
Load Data From S3 to Postgres using Step Functions
·5 mins· loading
AWS Serverless Lambda Step Functions Postgres
In this post, I discuss how we can use Step Functions Distributed Maps to parallelise the csv data loading to a database.