简体   繁体   English

限制从 AWS SQS 触发的 AWS Lambda 的并发调用(忽略预留并发)?

[英]Limit concurrent invocation of a AWS Lambda triggered from AWS SQS (Reserved concurrency ignored)?

To me this seemed like a simple use case when I started, but it turned out a lot harder than I had anticipated.对我来说,这在我开始时似乎是一个简单的用例,但结果比我预期的要难得多。

Problem问题

I have an AWS SQS acting as a job queue that triggers a worker AWS Lambda. However since the worker lambdas are sharing non-scalable resources it is important to limit the number of concurrent running lambdas to (for the sake of example) no more than 5 lambdas running simultaneously.我有一个 AWS SQS 作为触发工作人员 AWS Lambda 的作业队列。但是,由于工作人员 lambda 共享不可扩展的资源,因此将并发运行的 lambda 的数量限制为(为了举例)不超过很重要5 个 lambda 同时运行。

Simple enough, according to Managing Concurrency for a Lambda Function很简单,根据Managing Concurrency for a Lambda Function

Reserved concurrency also limits the maximum concurrency for the function, and applies to the function as a whole预留并发也限制了function的最大并发数,对function整体适用

However, setting the Reserved concurrency -property to 5 seems to be completely ignored by SQS, with the queue Messages in Flight -property in my case showing closer to 20-30 concurrent executions depending on the amount of messages put into the queue.但是,将Reserved concurrency -property 设置为 5 似乎被 SQS 完全忽略,在我的例子中,队列Messages in Flight -property 显示接近 20-30 个并发执行,具体取决于放入队列的消息量。

Question问题

The closest I have come to a solution is to use a SQS FIFO queue and setting the MessageGroupId to a value of either randomly selecting or alternating between 1-5.我最接近的解决方案是使用 SQS FIFO 队列并将MessageGroupId设置为随机选择或在 1-5 之间交替的值。 However, due to uneven workload this is not optimal as it would be better to have the concurrency distributed by actual workload rather than by chance.但是,由于工作负载不均匀,这并不是最佳选择,因为最好根据实际工作负载而不是偶然分配并发。

I have also tried using the AWS Step Functions as the Map-state has a MaxConcurrency parameter, which seemed to work well on small job queues, but due to each state having an input/output limit of 32kb, this was not feasible in my use-case.我还尝试使用 AWS Step Functions,因为Map-state有一个 MaxConcurrency 参数,这似乎适用于小型作业队列,但由于每个 state 的输入/输出限制为 32kb,这在我的使用中是不可行的-案件。

Has anyone found a better or alternative solution?有没有人找到更好的或替代的解决方案? Are there any other ways Reserved concurrency is supposed to be used?应该使用Reserved concurrency的任何其他方式吗?

Similar相似的

Here are some similar questions I have found, but I think my question is different because I am not interested in limiting the total number of invocation , and (although I have not tried it myself) I can not see why triggers from S3 or Kinesis Steam would behave different from SQS.这是我发现的一些类似问题,但我认为我的问题是不同的,因为我对限制调用总数不感兴趣,并且(虽然我自己没有尝试过)我看不出为什么从 S3 或 Kinesis Steam 触发与 SQS 的行为不同。

According to AWS docs AWS SQS doesn't take into account reserved concurrency.根据 AWS 文档,AWS SQS 不考虑保留并发。 If number of batches to be processed is greater than reserved concurrency, your messages might end up in a dead-letter queue:如果要处理的批次数大于保留的并发数,您的消息可能最终会进入死信队列:

If your function returns an error, or can't be invoked because it's at maximum concurrency, processing might succeed with additional attempts.如果您的 function 返回错误,或者因为它处于最大并发状态而无法调用,则处理可能会通过额外的尝试成功。 To give messages a better chance to be processed before sending them to the dead-letter queue, set the maxReceiveCount on the source queue's redrive policy to at least 5. https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html为了在将消息发送到死信队列之前有更好的机会进行处理,请将源队列的重新驱动策略上的 maxReceiveCount 设置为至少 5。 https://docs.aws.amazon.com/lambda/latest/dg /with-sqs.html

You can check this article for details: https://zaccharles.medium.com/lambda-concurrency-limits-and-sqs-triggers-dont-mix-well-sometimes-eb23d90122e0详情可以查看这篇文章: https://zaccharles.medium.com/lambda-concurrency-limits-and-sqs-triggers-dont-mix-well-sometimes-eb23d90122e0

This issue is resolved today Jan 2023. You can use maximum concurrency as suggested in this blog.此问题已于 2023 年 1 月今天解决。您可以按照此博客中的建议使用最大并发性。 I was using FIFO with groupid as my backend was non-scalable and i wanted to not have any throttling issue as having too many messages on DLQ does not help.我将 FIFO 与 groupid 一起使用,因为我的后端是不可扩展的,而且我不希望有任何节流问题,因为 DLQ 上有太多消息无济于事。

临界强化

https://aws.amazon.com/blogs/compute/introducing-maximum-concurrency-of-aws-lambda-functions-when-using-amazon-sqs-as-an-event-source/ https://aws.amazon.com/blogs/compute/introducing-maximum-concurrency-of-aws-lambda-functions-when-using-amazon-sqs-as-an-event-source/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM