java - AWS lambda 和 Java 并发

标签 java multithreading amazon-web-services concurrency aws-lambda

众所周知,AWS lambda 可能重用早期创建的处理程序对象,并且确实做到了(参见 FAQ):

Q: Will AWS Lambda reuse function instances?

To improve performance, AWS Lambda may choose to retain an instance of your function and reuse it to serve a subsequent request, rather than creating a new copy. Your code should not assume that this will always happen.


问题是关于 Java 并发的。如果我有一个处理程序的类,请说:

public class MyHandler {
    private Foo foo;
    public void handler(Map<String,String> request, Context context) {
       ...
    }
}

那么,在这里访问和使用对象变量 foo 是否是线程安全的?

换句话说:AWS lambda 是否可以同时为不同的调用使用同一个对象?

EDIT 我的函数在基于事件的源上进行处理,特别是由 API Gateway 方法调用。

EDIT-2 当您想实现某种与外部资源的连接池时,就会出现此类问题,因此我想将与外部资源的连接保持为对象变量。它实际上可以按预期工作,但我担心并发问题。

EDIT-3 更具体地说,我想知道:AWS lambda 处理程序的实例是否可以共享公共(public)堆(内存)?我必须指定这个额外的细节,以防止答案列出关于 java 线程安全对象的明显和众所周知的事情。

最佳答案

May AWS lambda use same object concurrently for different calls?

Can instances of handlers of AWS lambda share common heap (memory) or not?

一个强有力的,明确的NO。 AWS Lambda 的处理程序实例甚至无法共享文件(在 /tmp 中)。

AWS Lambda 容器可能被重复用于两个或多个同时存在的 Lambda 函数调用,因为这会破坏隔离要求:

Q: How does AWS Lambda isolate my code?

Each AWS Lambda function runs in its own isolated environment, with its own resources and file system view.

how lambda functions work官方描述中的“AWS Lambda如何运行我的代码?容器模型”部分状态:

After a Lambda function is executed, AWS Lambda maintains the container for some time in anticipation of another Lambda function invocation. In effect, the service freezes the container after a Lambda function completes, and thaws the container for reuse, if AWS Lambda chooses to reuse the container when the Lambda function is invoked again. This container reuse approach has the following implications:

  • Any declarations in your Lambda function code remains initialized, providing additional optimization when the function is invoked again. For example, if your Lambda function establishes a database connection, instead of reestablishing the connection, the original connection is used in subsequent invocations. You can add logic in your code to check if a connection already exists before creating one.

  • Each container provides some disk space in the /tmp directory. The directory content remains when the container is frozen, providing transient cache that can be used for multiple invocations. You can add extra code to check if the cache has the data that you stored.

  • Background processes or callbacks initiated by your Lambda function that did not complete when the function ended resume if AWS Lambda chooses to reuse the container. You should make sure any background processes or callbacks (in case of Node.js) in your code are complete before the code exits.

如您所见,在尝试利用容器重用时,绝对没有关于 Lambda 函数的多个并发调用之间的竞争条件的警告。唯一的注意是“不要依赖它!”。

关于java - AWS lambda 和 Java 并发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38016683/

相关文章:

java - MATLAB java.util 是否有非 HashMap /集?

java - 无法获取文本文件的 URI

java - 在android中安排多个异步任务

curl (35) 安全卡或 token 不存在

amazon-web-services - 获取 s3 存储桶上的资源 URI

java - 如何找出固件中嵌入的 GZIP 部分的大小?

java - RxJava 异步订阅

multithreading - Go 服务器在发送 INT 信号后挂起

python - 让线程执行不超过24小时然后死亡

python - AWS lambda : Python function with Pandas dependency