如何保持所需数量的 AWS Lambda 函数容器温暖 [英] How to keep desired amount of AWS Lambda function containers warm

查看:25
本文介绍了如何保持所需数量的 AWS Lambda 函数容器温暖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,有在 AWS API Gateway 和 AWS Lambda 上实现的 REST API.由于 AWS Lambda 函数在我们调用它时是无服务器和无状态的,因此 AWS 会启动一个容器,其中包含处理我们调用的 Lambda 函数的代码.根据 AWS 文档 完成 lambda 函数执行 AWS不要停止容器,我们可以处理该容器中的下一个调用.这种方法提高了服务的性能 - 只有在第一次调用 AWS 时才花时间启动容器(Lambda 函数的冷启动),并且所有接下来的调用都执行得更快,因为它们使用相同的容器(热启动).

On my project there is REST API which implemented on AWS API Gateway and AWS Lambda. As AWS Lambda functions are serverless and stateless while we make a call to it, AWS starts a container with code of the Lambda function which process our call. According AWS documentation after finishing of lambda function execution AWS don't stop the container and we are able to process next call in that container. Such approach improves performance of the service - only in time of first call AWS spend time to start container (cold start of Lambda function) and all next calls are executed faster because their use the same container (warm starts).

作为提高性能的下一步,我们创建了定期调用 Lambda 函数的 cron 作业(为此我们使用 Cloudwatch 规则).这种方法允许使 Lambda 函数保持温暖",从而避免停止和重新启动容器.IE.当真正的用户调用我们的 REST API 时,Lambda 不会花时间去启动一个新的容器.

As a next step for improving the performance we created cron job which calls periodically our Lambda function (we use Cloudwatch rules for that). Such approach allow to keep Lambda function "warm" allowing to avoid stopping and restarting of containers. I.e. when the real user will call our REST API, Lambda will not spent time to start a new container.

但我们遇到了一个问题——这种方法只允许对一个 Lambda 函数容器进行保温,而来自不同用户的实际并行调用数量可能要大得多(在我们的例子中,有数百个甚至数千个用户).有什么方法可以为 Lambda 函数实现预热功能,不仅可以预热单个容器,还可以预热一些所需数量的容器?

But we faced with the issue - such approach allow to keep warm only one container of Lambda function while the actual number of parallel calls from different users can be much larger (in our case that's hundreds and sometimes even thousands of users). Is there any way to implement warm up functionality for Lambda function which could warm not only single container, but some desired number of them?

我知道这种方法会影响 Lambda 函数的使用成本,并且可能使用好的旧应用程序服务器会更好,但我认为下一步将比较这些方法及其成本,并且在目前我只想找到加热所需数量的 Lambda 函数容器的方法.

I understand that such approach can affect cost of Lambda function's using and possibly, at all it will be better to use good old application server, but comparison of these approaches and their costs will be the next steps, I think, and in current moment I would like just to find the way to warm desired count of Lambda function containers.

推荐答案

这可能会很长,但请耐心等待,因为这可能会给您提供解决方法,并且可能会让您更好地理解 Lambda 的工作原理?

This can be long but bear with me as this would probably give you workaround and may be would make you understand better How Lambda Works ?

或者,如果您对阅读不感兴趣,您可以跳到底部解决方法".

Alternatively You can Skip to Bottom "The Workaround" if you are not interested in reading.

对于不了解冷启动的人,请阅读这篇博文以更好地理解它.简而言之:

For folks who are not aware about cold starts please read this blog post to better understand it. To describe this in short:

  • 当一个函数第一次被执行时或之后功能代码或资源配置更新,容器将启动以执行此功能.所有的代码和库都将加载到容器中以便它能够执行.该代码将然后运行,从初始化代码开始.初始化code 是在处理程序之外编写的代码.此代码仅运行第一次创建容器时.最后,Lambda处理程序被执行.这个设置过程被认为是冷的开始.
  • 为了性能,Lambda 能够重用创建的容器通过之前的调用.这将避免初始化一个新的容器和代码加载.只有处理程序代码将是执行.但是,您不能依赖以前的容器要重用的调用.如果您还没有更改代码并且不是太时间过去了,Lambda 可能会重用之前的容器.
  • 如果你改变了代码、资源配置或一段时间有自上次调用以来,一个新的容器将是已初始化,您将遇到冷启动.

现在考虑这些场景以便更好地理解:

Now Consider these scenarios for better understanding:

  • 考虑在示例中第一次调用 Lambda 函数.Lambda 将创建一个容器,将代码加载到容器中并运行初始化代码.然后将执行函数处理程序.此调用将经历冷启动.如评论中所述,该功能需要 15 秒才能完成.一分钟后,再次调用该函数.Lambda 很可能会重新使用上一次调用中的容器.此调用不会出现冷启动.
  • 现在考虑第二种情况,其中第二次调用在第一次调用后 5 秒执行.由于前一个函数需要 15 秒才能完成并且还没有完成执行,新的调用将不得不为这个函数创建一个新的容器来执行.因此此调用将经历冷启动.
  • Consider the Lambda function, in the example, is invoked for the first time. Lambda will create a container, load the code into the container and run the initialisation code. The function handler will then be executed. This invocation will have experienced a cold start. As mentioned in the comments, the function takes 15 seconds to complete. After a minute, the function is invoked again. Lambda will most likely re-use the container from the previous invocation. This invocation will not experience a cold start.
  • Now consider the second scenario, where the second invocation is executed 5 seconds after the first invocation. Since the previous function takes 15 seconds to complete and has not finished executing, the new invocation will have to create a new container for this function to execute. Therefore this invocation will experience a cold start.

现在开始你已经解决的问题的第一部分:

关于防止冷启动,这是一种可能性,但不能保证,常见的解决方法只会对Lambda函数的一个容器进行保温.为此,您将使用计划事件(cron 表达式)运行 CloudWatch 事件,该事件将每隔几分钟调用一次您的 Lambda 函数以保持其温暖.

Regarding preventing cold starts, this is a possibility, however, it is not guaranteed, the common workaround will only keep warm one container of the Lambda function. To do, you would run a CloudWatch event using a schedule event (cron expression) that will invoke your Lambda function every couple of minutes to keep it warm.

对于您的用例,您的 Lambda 函数将非常频繁地非常高的并发率被调用.为了避免尽可能多的冷启动,您需要为尽可能多的容器保温,直到达到最高并发.为此,您将需要延迟调用函数,以允许此函数的并发构建并达到所需的并发执行量.这将迫使 Lambda 增加您想要的容器数量.因此,这会增加成本,并且不能保证避免冷启动.

For your use-case, your Lambda function will be invoked very frequently with a very high concurrency rate. To avoid as many cold starts as possible, you will need to keep warm as many containers as you expect your highest concurrency to reach. To do this you will need to invoke the functions with a delay to allow the concurrency of this function to build and reach the desired amount of concurrent executions. This will force Lambda to spin up the number of containers you desire. This, as a result, can bring up costs and will not guarantee to avoid cold starts.

话虽如此,以下是有关如何同时为您的函数保持多个容器温暖的细分:

  • 您应该有一个按计划触发的CloudWatch Events 规则.此计划可以是固定速率或 cron 表达式.例如,您可以将此规则设置为每 5 分钟触发一次.您将然后指定一个 Lambda 函数(控制器函数)作为此规则的目标.

  • You should have a CloudWatch Events Rule that is triggered on a schedule. This schedule can be a fixed rate or a cron expression. for example, You can set this rule to trigger every 5 minutes. You will then specify a Lambda function (Controller function) as the target of this rule.

您的控制器 Lambda 函数随后将为任意数量的并发运行容器调用 Lambda 函数(您希望保持温暖的函数).

这里有几件事情需要考虑:

There are a few things to consider here:

  1. 你将不得不建立并发性,因为如果第一次调用在另一个调用开始之前完成,然后这个调用可以重用以前的调用容器而不是创建新的一.为此,您需要在Lambda 函数(如果该函数由控制器调用)功能.这可以通过将特定的有效载荷传递给具有这些调用的函数.你的 lambda 函数想要保持温暖将检查此有效负载是否存在.如果它这样做然后函数将等待(构建并发调用),如果没有,则该函数可以作为预期.

如果您重复调用调用 Lambda API,您还需要确保不会受到限制.您的拉姆达如果发生这种限制,应该编写函数来处理它并考虑在 API 调用之间添加延迟以避免限制.

You will also need to ensure you are not getting throttled on the Invoke Lambda API call if you are calling it repeatedly. Your Lambda function should be written to handle this throttling if it occurs and consider adding a delay between API calls to avoid throttling.

最终,此解决方案可以减少冷启动,但会增加成本,并且不能保证会发生冷启动,因为在使用 Lambda 时冷启动是不可避免的.如果您的应用程序需要更快的响应时间,那么使用Lambda 冷启动,我建议您考虑将您的服务器放在 EC2 实例上.

这篇关于如何保持所需数量的 AWS Lambda 函数容器温暖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆