I have always learnt in theory that creating new connection to databases is costly operation. So we should keep open connection pool and use it for db operations.
When considering AWS lambda. Suppose lambda function wants to operate on db, then we need to create a connection to db. After operations, db needs to be closed. If simultaneously 100s of lambda function are executing then 100s of db connection open/close are being done. Which is theoretically bad pattern.
If this is so then is it inappropriate to use AWS lambda when db operations are involved?
Why do you need DB connection is closed, not reusing it in Lambda function?
Each Lambda function running means a Container and they will be alive for a while after running(several 10min and if you call it in succession, it will keep alive).
Being alive means that each Lambda function keeps the memory area after it is running so that it can reuse them.
For example, If you define a global variable in Lambda function like below.
(Though it is python code, I think you can understand because it is as simple as enough)
variable = 10
def lambda_function(event, context):
global variable
print(variable)
variable += 1
If the Lambda function is called at every 1 sec, it will print like the below.
10
11
12
13
14
.
.
.
As you can see, every Lambda function called is using the same global variable.
What if the global variable is DB connection? You can reuse them not re-opening connection at every lambda-call.
However, as you said if 100 Lambda functions are simultaneously executed, 100 connections will be opened since each Lambda concurrency means different Containers respectively, having different memory area.
But finally, 100 connections will be reused for sequential 100 simultaneous execution.
---------- Edit ----------
I agree with @Arun's comment. My answer will be useful when traffic is steady and increasing and decreasing gradually so that the connections can be reused enough and closed by server-side's keep-alive. Radical increase and decrease of traffic could waste DB connection not closing appropriately.
---------- Edit ---------- 2019-12-17
AWS announced the new feature RDS proxy though it is preview yet.
If you connect RDS through RDS proxy, it will manage DB connection pool(Lambda <-> RDS) for you.
For detail, reference this link
connection pooling working fine on Serverless i.e.
const mysql = require('mysql');
const pool = mysql.createPool({
host: {Your Host},
user: {Your Username},
password: {Your Password},
database: {Your Database},
port: 3306
});
exports.main = function main (req) {
let query = ""
return new Promise(function(resolve, reject){
return resolve({
statusCode: 200,
body: "Success"
});
})
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With