AWS Lambda 函数的横向扩展不足以处理 SQS 消息

AWS Lambda function does not scale out enough to process SQS messages

我有一个订阅 SQS 队列来处理消息的 lambda。留言量很大

问题:队列增长很快,lambda函数没有横向扩展来足够快地处理消息。并发的 lambda 执行仅上升到 20 到 25,即使我还有 950 或更多未使用的 lambda 执行的剩余配额。为什么不启动更多的 lambda 来更快地处理我的队列?这是可配置的吗?

这是我的应用程序中的一个问题,因为我使用的是标准 SQS 队列,它不提供排序保证。因此,有时我会看到不幸的消息在队列中排了几个小时,而有些消息在不到一分钟的时间内就得到了处理。 (顺便说一句,我很震惊队列可以以如此随机的顺序处理。即使没有顺序保证,我也不会想到它会这么糟糕)。

关于 SQS,您没有说明您使用的是哪个区域,但 SQS 在某些区域确实有 FIFO 选项。

FIFO queues are available in the US East (N. Virginia), US East (Ohio), US West (Oregon), EU (Ireland), Asia Pacific (Sydney), and Asia Pacific (Tokyo) regions. FIFO queues have all the capabilities of the standard queue.

关于 Lambda 并发性,听起来您正在使用的子网中的 IP 地址已用完 运行。这仅在您使用 VPC 时适用。

If your function connects to VPC based resources, you must make sure your subnets have adequate address capacity to support the ENI scaling requirements of your function. You can estimate the approximate ENI capacity with the following formula:

Concurrent executions * (Memory in GB / 3 GB)

Where:

Concurrent execution – This is the projected concurrency of your workload. Use the information in Understanding Scaling Behavior to determine this value.

Memory in GB – The amount of memory you configured for your Lambda function.

You can set the concurrent execution limit for a function to match the subnet size limits you have.

参考资料

https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/FIFO-queues.html

https://docs.aws.amazon.com/lambda/latest/dg/concurrent-executions.html

问题出在 lambda 函数的内存分配上。我天真地将它保留为 128MB 的默认值。将其更改为 2048MB 完全解决了这个问题。 lambda 现在可以毫无问题地跟上大量 SQS 消息。