title: AWS lambda and Java concurrency
body: <p>It is known that AWS lambda <em>may</em> reuse early created objects of handlers, and it really does it (see <a href="https://aws.amazon.com/lambda/faqs/" rel="noreferrer">FAQ</a>):</p>
  
  <blockquote>
    <p><strong>Q: Will AWS Lambda reuse function instances?</strong></p>
    
    <p>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.</p>
  </blockquote>
  
  <hr>
  
  <p>The question is regarding <code>Java</code> concurrency. If I have a class for a handler, say:</p>
  
  <pre><code>public class MyHandler {
      private Foo foo;
      public void handler(Map&lt;String,String&gt; request, Context context) {
         ...
      }
  }
  </code></pre>
  
  <p>so, will it be thread-safe to access and work with the object variable <code>foo</code> here or not?</p>
  
  <p>In other words: may AWS lambda use the same object concurrently for different calls?</p>
  
  <p><strong>EDIT</strong> My function is processed on an event based source, particularly it is invoked by an API Gateway method. </p>
  
  <p><strong>EDIT-2</strong> Such kind of question rises when you want to implement some kind of connection pool to external resources, so I want to keep the connection to the external resource as an object variable. It actually works as desired, but I'm afraid of concurrency problems.</p>
  
  <p><strong>EDIT-3</strong> More specifically I'm wondering: <strong>can instances of handlers of AWS lambda share common heap (memory) or not</strong>? I have to specify this additional detail in order to prevent answers with listing of obvious and common-known things about java thread-safe objects.</p>