Postgresql setup with lambda

39 Views Asked by At

I am trying to use lambda with postgresql and also using hiberntate for connection and orm.

I want to understand this, whenever that lambda function is called, as new lambda container is started everytime(LMK if its not the case), will hibernate set its configurations again and again(probably taking a lot of overhead)?

Also I did a small experiment to understand this,where lambda is just connecting to rds postgres db and querying using user id and I did 1000 lambda calls sequentially (waiting for lambda to finish while adding time stamps in code to get in logs)

public String handleRequest(DataSourceInput input, Context context) {
        
        long startTime = System.nanoTime();
        if(input == null || CommonUtil.isInputNull(input.sql) || CommonUtil.isInputNull(input.operation)) return "Input empty!";
        List<User> users = DataSourceProperties.hibernateOrm(input.sql,input.operation); // obtaining session and querying using userID
        long endTime = System.nanoTime();
        long timeTaken = (endTime-startTime)/1000000;
        long timestamp = System.currentTimeMillis();
        logger.info("Postgres Test Query|||timeTaken:{} ms|||query:{}|||startTime:{}",timeTaken,input.sql,timestamp);
        return users.toString();
    }

Note: Time provided here is "timeTaken" of the code provided above,not the lambda execution time.

Now, when I queried the logs, it seems that data is averaging on 5ms to execute the whole process, but sometimes it takes approx 24 SECONDS to run , through which I concluded, not everytime a new container is created, but when it is created, hibernate takes a lot of overhead to start the functionality

Am I right in thinking this? or is there something I am missing?

1

There are 1 best solutions below

2
Mark B On

What you are seeing is the difference between an AWS Lambda cold start, where the entire Lambda container is created and initialized in order to service the request, and a warm start, where a previously initialized container is used again to process another request. This behavior is described in this AWS Blog post.

If the cold starts are too slow, you can look into Lamba Provisioned Concurrency to keep a certain number of instances of your function warm at all times. Of course this means you are paying a lot more for those running instances, which defeats one of the benefits of serverless computing.


In general I would not recommend Hibernate for a serverless ephemeral environment like AWS Lambda.