简体   繁体   中英

Concurrency behavior of AWS Lambda

Suppose I have a code like this.

public class MyHandler {
    private Foo foo;
    public void handler(InputStream request, OutputStream response, Context context) {
       ...
    }
}

Foo foo takes care of creating pooled database connections.

I am trying to understand how this works with AWS Lambda. If I am understanding correctly, foo gets shared across multiple invocations. The first one takes more time because it has to load into the container and subsequent ones would be faster since foo is already initialized. This would be the case until my handler gets kicked out after a period of inactivity.

So, for sequential invocations, would it use the same object, would it have access to /tmp created in earlier function call ?

What about parallel invocations ? Would it duplicate the entire container, because as per the docs, each lambda function should execute in its own container, its own resources and its own /tmp ?

If the handler is going to get kicked out after inactivity, is there a callback function in Java that I can call to close out all opened pooled connections ?

So, for sequential invocations, would it use the same object, would it have access to /tmp created in earlier function call ?

Yes it would use the same object. Yes it would have access to /tmp created in earlier function call.

What about parallel invocations ? Would it duplicate the entire container, because as per the docs, each lambda function should execute in its own container, its own resources and its own /tmp ?

Parallel invocations would occur in separate containers. A single container only handles one invocation at a time. So yes the entire container would be duplicated, and each container has its own /tmp .

If the handler is going to get kicked out after inactivity, is there a callback function in Java that I can call to close out all opened pooled connections ?

No there is no callback you can use to handle this. Your function will be in a suspended state when the container is eventually removed due to inactivity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM