samedi 2 juillet 2016

AWS lambda and Java concurrency

It is known that AWS lambda may reuse early created objects of handlers, and it really does it (see 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.


The question is regarding to Java concurrency. If I have class for handler, say:

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

so, will it be thread-safe to access to and work with object variable foo here or not?

In other words: may AWS lambda use same object concurrently for different calls?

EDIT My function is processed on event based source, particular it is invoked by API Gateway method.

EDIT-2 Such kind of question rises when you want to implement some kind of connection pool to external resources, so I want to keep connection to external resource as object variable. It actually works as desired, but I'm afraid of concurrency problems.

Aucun commentaire:

Enregistrer un commentaire