How we can use JDBC connection pooling with AWS Lambda?

后端 未结 4 1108
我寻月下人不归
我寻月下人不归 2020-12-30 06:39

Can we use JDBC connection pooling with AWS Lambda ? AS AWS lambda function get called on a specific event, so its life time persist even after it finishing one of its call

相关标签:
4条回答
  • 2020-12-30 07:13

    Kudos to the AWS RDS proxy, now you can used pooled MySql and postgrese connections without any extra configs in your Java or other any code specific to AWS Lambda. All you need is to create and Add a Database proxy your AWS Lambda function you want to reuse/pool connections. See how-to here.

    Note: AWS RDS proxy is not included in the Free-Tier (more here).

    0 讨论(0)
  • 2020-12-30 07:17

    No. Technically, you could create a connection pool outside of the handler function but since you can only make use of any one single connection per invocation so all you would be doing is tying up database connections and allocating a pool of which you could only ever use 1.

    After uploading your Lambda function to AWS, the first time it is invoked AWS will create a container and run the setup code (the code outside of your handler function that creates the pool- let's say N connections) before invoking the handler code.

    When the next request arrives, AWS may re-use the container again (or may not. It usually does, but that's down to AWS and not under your control).

    Assuming it reuses the container, your handler function will be invoked (the setup code will not be run again) and your function would use one of N the connections to your database from the pool (held at the container level). This is most likely the first connection from the pool, number 1 as it is guaranteed to not be in use, since it's impossible for two functions to run at the same time within the same container. Read on for an explanation.

    If AWS does not reuse the container, it will create a new container and your code will allocate another pool of N connections. Depending on the turnover of containers, you may exhaust the database pool entirely.

    If two requests arrive concurrently, AWS cannot invoke the same handler at the same time. If this were possible, you'd have a shared state problem with the variables defined at the container scope level. Instead, AWS will use two separate containers and these will both allocate a pool of N connections each, i.e. 2N connections to your database.

    It's never necessary for a single invocation function to require more than one connection (unless of course you need to communicate to two independent databases within the same context).

    The only time a connection pool would be useful is if it were at one level above the container scope, that is, handed down by the AWS environment itself to the container. This is not possible.

    The best case you can hope for is to have a single connection per container. Even then you would have to manage this single connection to ensure the database server hasn't disconnect or rebooted. If it does, your container's connection will die and your handler will never be able to connect again (until the container dies), unless you write some code in your function to check for dropped connections. On a busy server, the container might take a long time to die.

    Also keep in mind that if your handler function fails, for example half way through a transaction or having locked a table, the next request invocation will get the dirty connection state from the container. The first invocation may have opened a transaction and died. The second invocation may commit and include all the previous queries up to the failure.

    I recommend not managing state outside of the handler function at all, unless you have a specific need to optimise. If you do, then use a single connection, not a pool.

    0 讨论(0)
  • 2020-12-30 07:22

    It has caveat There is no destroy method which ensures closing pool. One may say DB connection idle time would handle. What if same DB being used for other use cases like pool maintain in regular machine Luke EC2. As many say, if there is sudden spike in requests, create chaos to DB as there will be always some maximum connection setting at database side per user.

    0 讨论(0)
  • 2020-12-30 07:36

    Yes, the lambda is mostly persistent, so JDBC connection pooling should work. The first time a lambda function is invoked, the environment will be created and it may or may not get reused. But in practice, subsequent invocations will often reuse the same lambda process along with all program state if your triggering events occur often.

    This short lambda function demonstrates this:

    package test;
    
    import com.amazonaws.services.lambda.runtime.Context;
    import com.amazonaws.services.lambda.runtime.RequestHandler;
    
    public class TestLambda implements RequestHandler<String, String> {
    
        private int invocations = 0;
    
        public String handleRequest(String request, Context context) {
            invocations++;
            System.out.println("invocations = " + invocations);
            return request;
        }
    }
    

    Invoke this from the AWS console with any string as the test event. In the CloudWatch logs, you'll see the invocations number increment each time.

    0 讨论(0)
提交回复
热议问题