0

We are having a NodeJs Lambda which is triggered by an SQS FIFO Queue. The SQS Queue Settings are as follows:

    "sourceQueue": {
      "Type": "AWS::SQS::Queue",
      "Properties": {
        "QueueName": "dev-lambdaboilerplate-sourcequeue.fifo",
        "VisibilityTimeout": 30,
        "MessageRetentionPeriod": 1209600,
        "FifoQueue": true,
        "RedrivePolicy": {
          "deadLetterTargetArn": {
            "Fn::GetAtt": [
              "sourceQueueDLQ",
              "Arn"
            ]
          },
          "maxReceiveCount": 4
        }
      }
    },
    "sourceQueueDLQ": {
      "Type": "AWS::SQS::Queue",
      "Properties": {
        "QueueName": "dev-lambdaboilerplate-sourcequeue-DLQ.fifo",
        "MessageRetentionPeriod": 1209600,
        "FifoQueue": true
      }
    }
  }

The Lambda Trigger setting is as follows:

"SqspollerEventSourceMappingSQSSourceQueue": {
  "Type": "AWS::Lambda::EventSourceMapping",
  "DependsOn": [
    "IamRoleLambdaExecution"
  ],
  "Properties": {
    "BatchSize": 10,
    "EventSourceArn": {
      "Fn::GetAtt": [
        "sourceQueue",
        "Arn"
      ]
    },
    "FunctionName": {
      "Fn::GetAtt": [
        "SqspollerLambdaFunction",
        "Arn"
      ]
    },
    "Enabled": true,
    "FunctionResponseTypes": [
      "ReportBatchItemFailures"
    ],
    "ScalingConfig": {
      "MaximumConcurrency": 10
    }
  }
}

We are getting two messages into SQS in the below mentioned order:

  1. First Message - The details are as below:
"body": "First Message",
"attributes": {
    "ApproximateReceiveCount": "1",
    "SentTimestamp": "1729084272552",
    "SequenceNumber": "18889389647482863616",
    "MessageGroupId": "groupid1",
    "SenderId": "testid",
    "MessageDeduplicationId": "dedupid1",
    "ApproximateFirstReceiveTimestamp": "1729084272552"
},
  1. Second Message - The details are as below:
"body": "Second Message",
"attributes": {
    "ApproximateReceiveCount": "1",
    "SentTimestamp": "1729084273420",
    "SequenceNumber": "18889389647705071616",
    "MessageGroupId": "groupid1",
    "SenderId": "testid",
    "MessageDeduplicationId": "dedupid2",
    "ApproximateFirstReceiveTimestamp": "1729084302591"
},

Both the messages are having the same group id and ideally should be processed in the same order. These two messages are picked in the below order by the Lambda:

  1. First Lambda Trigger - First Message gets picked (and it errors out and the message is placed back in the queue)
  2. Second Lambda Trigger - First and Second Message gets picked together. The First Message gets errored out and the Second message gets succeeded. Since the First Message errored out, it goes back to the queue
  3. Third Lambda Trigger - First Message gets picked and it gets processed.

This is resulting in First Message being processed after the Second Message. Is there any way, the messages to be ordered sequentially? As per AWS documentation, the second message should not be picked until the first message is processed. Any idea what is going wrong here?

4
  • 2
    During the 2nd Lambda invocation, I'm assuming that both messages are presented to the Lambda function and you're saying that the 1st fails to process but the 2nd succeeds. Why is your Lambda function processing the 2nd if the 1st failed? Commented Oct 16, 2024 at 14:02
  • 3
    You need to change the logic in your Lambda function to track the group ID of failed messages in a given batch, and fail all subsequent messages in that batch with the same group ID. AWS can't handle that for you, once the list/batch of messages is passed into your function, it's up to you to write the code to handle that correctly. Commented Oct 16, 2024 at 14:29
  • @MarkB - As per the AWS Documentation - docs.aws.amazon.com/AWSSimpleQueueService/latest/…, it says - "Messages within the same message group are processed in order, and only one message from each group is processed at a time to maintain this order." Does this mean that only one message from each group is processed at a time? Commented Oct 16, 2024 at 15:30
  • 3
    Your quote is just from a general description of what "FIFO" means. The next section is more important: "Concurrency within message groups: Only one Lambda instance processes messages for a particular message group ID at a time. This ensures that messages within a group are handled sequentially." That means only one concurrent invocation will have messages from that group ID at a time, but that invocation may have multiple messages in its batch from that group ID, so you still have to handle it correctly in your code. If you can't do that, you may want to set the batch size to 1. Commented Oct 16, 2024 at 15:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.