AWS Lambda容器重用如何工作? [英] How AWS Lambda container reuse works?

查看:118
本文介绍了AWS Lambda容器重用如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过链接, (于2014年12月发布),也称为

AWS Lambda容器重用的工作日为2016年5月,请分享任何详细信息的特定链接吗?下面,关于这个AWS Lambda容器重用,我几乎没有疑问.

考虑用例:

Lambda函数名称"processMessageLambda"在必须处理消息时会收到请求,并从POST REST API(从该AWS Lambda函数连接到AWS API Gateway)接收该消息.

现在,此"processMessageLambda"处理消息并将其存储到数据库.

因此从逻辑上讲,它执行以下操作: 连接到数据库,存储消息并关闭连接. (在正常情况下,它可以正常工作.)

如果请求到达说每秒-10,并且每个lambda函数需要30秒才能执行,那么它实际上会打开许多​​数据库连接.

问题1:我们可以在这种情况下使用连接池"(例如boneCP)吗?因为"processMessageLambda"的调用次数将是每秒一百个还是每秒十个?

(请参阅:容器重用的简单示例-它按其说的那样工作,但是会发生很多请求,比如说-每秒10个请求)

问题2::如果可以使用连接池,那么将如何重新使用此AWS Lambda容器?

如果考虑到一种情况:

让我们认为此Lambda函数接收的请求是每秒10个,在这种情况下-将创建10个此Lambda函数的不同容器,或者将创建一个lambda函数的容器,并将其与所有这10个请求一起使用?

Q-3:如果将创建10个不同的lambda函数容器,则意味着将使用10个数据库连接,以便在进一步的请求中重用这10个容器?<​​/p>

问题4:您能否从开发人员的角度解释一下,它实际上对aws lambda容器的重用是如何工作的,或者开发人员在重用aws lambda容器时会如何考虑? >

Q-5:如果容器重用已经到位,开发人员如何维护变量的状态,以便开发人员知道哪个变量将被重用?

解决方案

我构建并维护了多个无服务器应用程序.需要记住的一件事对我有帮助:两个Lambda函数存在于不同的宇宙中.这立即回答了几个问题:

1:不,您只能按Lambda进行连接池.如果您启用100个Lambda函数,则您有100个连接.

2:-

3:未指定容器重用的确切算法.下一个请求 可以使用现有容器或新容器.没有办法告诉.阅读博客文章以了解更多信息.

4:imo的最佳方法是考虑容器根本不被重用,如果需要的话,添加的规则是始终在暂存空间中使用唯一的(随机)文件名(/tmp文件夹).当我尝试保持连接打开以供重用时,连接超时,然后重新使用.这导致数据库连接问题.现在,我只是在每次调用期间打开和关闭.

5:使代码保持无状态(模块除外,永远不要使用全局变量),如果需要将文件存储在暂存空间中,请始终使用唯一的(随机)名称.这两个一般的经验法则使我免于遇到很多问题.

I have already gone through this link , (It was published in Dec-2014),Also referred this and this

How the AWS Lambda container reuse works as on May-2016, May you please share any specific link which tells in details ? Below I have few questions all around this AWS Lambda container reuse.

Consider a use case :

A Lambda function name "processMessageLambda" receives request when it has to process a message, and that message it receives from the POST REST API( from AWS API Gateway, as this lambda function connected with).

Now this 'processMessageLambda' process the message and store it to database.

So logically it does the following : connect to database, store the message and shutdown the connection. (It works fine in normal case.).

If requests arrive say - 10 per second , and each lambda function takes 30 seconds to execute then it actually opens many database connections.

Q1: May we use 'connection pooling' on this case(e.g. boneCP) ? as numbers of calls to "processMessageLambda" would be like hundred per second or Ten per second ?

(refer :simple example of container reuse - It works as it says, but what will happen many request would arrive say - 10 request per seconds )

Q-2: If its possible to use the connection pooling, then how this aws lambda container would be reUsed ?

If consider a case :

Lets consider that requests received by this Lambda function are Ten per second, on this case - 10 different container of this lambda function would be created or single container of lambda function would be created and that would be used with all these 10 requests ?

Q-3: If 10 different container of lambda function would be created then that means 10 database connections would be used , so those 10 container would be reUsed on further requests ?

Q-4: May you please explain from a developer point of view, that how it actually aws lambda container reuse works or how a developer think about it while reusing the aws lambda container ?

Q-5: If container reuse already in place,How a developer need to maintain the state of variables so a developer know which variable would be reused ?

解决方案

I build and maintain multiple serverless applications. One thing to remember which helps me is: two Lambda functions live in different universes. This immediately answer a few questions:

1: No, you can only do connection pooling per Lambda. If you fire 100 Lambda functions, you have 100 connections.

2: -

3: The exact algorithm behind container re-use is not specified. The next requests may use an existing container or a new container. There is no way to tell. Read this blog post for more info.

4: The best way imo is to think about it that containers are not re-used at all, with the added rule to always use unique (random) filenames in scratch space if you need it (the /tmp folder). When I tried to keep the connection open for re-use, the connection timed out and THEN got re-used. This resulted in database connection issues. Now I just open and close during each invocation.

5: Keep your code stateless (except modules, NEVER use global variables) and always use a unique (random) name if you need to store files in scratch space. These two general rules of thumb save me from lots of issues.

这篇关于AWS Lambda容器重用如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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