Lambda 节流低于并发限制

Lambda throttling below concurrency limit

我们使用 Lambda 为通过新闻媒体网站访问的 APIs(通过 API 网关)提供动力,接收波动但高的流量负载。我们开始遇到限制,因此我们将并发限制提高到 2000。但是,我们每天仍然会遇到多次限制。

奇怪的是,在 CloudWatch 指标中,当我们受到限制时,并发请求的峰值约为 600 或更低。以这个 CloudWatch 图表为例:

有没有人遇到过这种情况?你认为为什么会这样?我们能做些什么呢?

更多信息

此外,这张图片还显示了同一时间段内的总调用次数和平均持续时间。很难知道什么是因果关系(由于节流而持续时间延长,反之亦然,因为一些 lambda 确实调用了其他 lambda)。请查看相应的坐标轴,因为刻度非常不同。

这似乎很熟悉。我们遇到了完全相同的问题,我们感到困惑,因为我们的并发限制已经增加,但不幸的是,这并不是无服务器应用程序无限可扩展性的神奇修复。

我的猜测是您 运行 不在 ENI(弹性网络接口)范围内,因为每个 lambda 函数在初始化之前都需要一个。默认限制为 350 个并发连接的 ENI。

你的 600 个并发 lambas 每分钟分组一次,所以我想它们中的几个在一分钟内重叠,因此超过 350 个。

要对此进行调查,请进入您的 API 网关的全局设置,并为其提供一个 IAM 角色 arn,该角色 arn 可以访问将日志发送到 CloudWatch。 然后进入个人 API 网关 api 并启用详细日志记录。

当 API 网关尝试调用 lambda 函数时发生的任何错误都应该显示在这里而不是被掩盖(默认情况下)。

如果错误看起来有点像:

{
    "Message": "Lambda was not able to create an ENI in the VPC of the Lambda function because the limit for Network Interfaces has been reached.",
    "Type": "User"
}

如果是这种情况,您需要请求提高 ENI 的限额。

我认为这与 Lambda 并发突发限制有关。

基本上,在突如其来的负载下,您可以并发 运行 的 Lambda 函数的实例数量是有限制的,并且这个限制不同于每个区域的整体 Lambda 并发限制。

您可以在此处找到更多相关信息:

https://docs.aws.amazon.com/lambda/latest/dg/scaling.html

相关部分:

AWS Lambda dynamically scales function execution in response to increased traffic, up to your concurrency limit. Under sustained load, your function's concurrency bursts to an initial level between 500 and 3000 concurrent executions that varies per region. After the initial burst, the function's capacity increases by an additional 500 concurrent executions each minute until either the load is accommodated, or the total concurrency of all functions in the region hits the limit.