middyjs / middy

🛵 The stylish Node.js middleware engine for AWS Lambda 🛵

Home Page:https://middy.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ApproximateReceiveCount limit on SqsPartialBatch

zb-sj opened this issue · comments

Is your feature request related to a problem? Please describe.

SqsPartialBatch should implement retry logic to stop the SQS event from repeating forever.

Describe the solution you'd like

It could limit ApproximateReceiveCount like this:

    for (const [idx, record] of Object.entries(Records)) {
      const { status, reason } = response[idx]
      if (status === 'fulfilled') continue
      if (record.attributes.ApproximateReceiveCount >= maxApproximateReceiveCount) {
        logger(`Some good message`, record)
      }
      batchItemFailures.push({ itemIdentifier: record.messageId })
      if (typeof logger === 'function') {
        logger(reason, record)
      }
    }

OR

Even better to provide retry strategy function to customize

Describe alternatives you've considered

I thought of creating separate middleware to catch rejection but thought an official @middy/sqs-partial-batch-failure package would be a better fit.

I'm willing to implement this myself and open a PR if you agree on this feature :)

I think using indempotency and/or a configured dead-letter que would be a better approach, making this unneeded if I'm understanding correctly.

Thanks for the suggestion.
I'm not experienced in idempotency in lambda, but doesn't it require stateful db or something? I believe it's a bit overkill for such a purpose.
Please let me know if I'm not following correctly.

You are correct, the recommendation is DynamoDB. Check out https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/idempotency/ from the AWS Powertools team.

This gist is what I'm using in my work for those
who come across this.

Actually, I agree with your suggestion about DLQ and maxReceiveCount, this was not about idempotency.
I was confused because the console labeled it Maximum Receives.

But I had to implement this on lambda side anyway, since SQS is not managed by me. 😢