简体   繁体   English

使用RDS在AWS Lambda中建立连接池?

[英]Connection Pooling in AWS Lambda with RDS?

I need effective MySQL database connection in AWS Lambda (Using Node Js). 我需要在AWS Lambda中使用有效的MySQL数据库连接(使用Node Js)。

Which is not creating connection/pool for every request, instead reuse it. 这不是为每个请求创建连接/池,而是重用它。

One Solution I got like opening connection outside AWS lambda handler. 我得到的一个解决方案就像在AWS lambda处理程序之外打开连接 But the problem with this case if we not end the connection, we end up with timeout result. 但是如果我们不结束连接这个问题,我们最终得到超时结果。 eg 例如

"use strict";
var db = require('./db');
exports.handler = (event, context, callback) => {
    db.connect(function (conn) {
        if (conn == null) {
            console.log("Database connection failed: ");
            callback("Error", "Database connection failed");
        } else {
            console.log('Connected to database.');
            conn.query("INSERT INTO employee(name,salary) VALUE(?,?)",['Joe',8000], function(err,res){
                if(err) throw err;
                else {
                    console.log('A new employee has been added.');
                }
            });
            db.getConnection().end();
            callback(null, "Database connection done");
        }
    });
};

The most reliable way of handling database connections in AWS Lambda is to connect and disconnect from the database within the invocation itself which is what your code is already doing. 在AWS Lambda中处理数据库连接的最可靠方法是在调用本身内连接断开数据库,这是您的代码已经在做的事情。

There are known ways to reuse an existing connection but success rates for that vary widely depending on database server configuration (idle connections, etc.) and production load. 已知的方法可以重用现有连接,但成功率因数据库服务器配置(空闲连接等)和生产负载而有很大差异。

Also, in the context of AWS Lambda, reusing database connections does not give you as much performance benefit due to the way how scaling works in Lambda. 此外,在AWS Lambda的上下文中,由于Lambda中的扩展方式,重用数据库连接并不会给您带来太多的性能优势。

In an always-on server app for example, concurrent and succeeding requests use and share the same connection or connection pool. 例如,在永远在线的服务器应用程序中,并发和后续请求使用并共享相同的连接或连接池。

In Lambda however, concurrent requests are handled by different servers , with each of them having their own connection to the database. 但是,在Lambda中,并发请求由不同的服务器处理,每个服务器都有自己的数据库连接。 10 concurrent requests will spin 10 separate servers connecting to your database. 10个并发请求将旋转10个连接到您的数据库的独立服务器。 Reusing connections or connection pools won't be of any help here. 重用连接或连接池在这里没有任何帮助。

To solve your problem, use: 要解决您的问题,请使用:

context.callbackWaitsForEmptyEventLoop = false;

The reason a timeout is happening is because the event loop is not empty as a result of the code outside of the handler. 发生超时的原因是因为处理程序外部的代码导致事件循环不为空。 This change allows to callback to immediately end the lambda's execution. 此更改允许回调以立即结束lambda的执行。 Your full code would look something like this: 您的完整代码看起来像这样:

var db = require('./db');
exports.handler = (event, context, callback) => {

    context.callbackWaitsForEmptyEventLoop = false;

    db.connect(function (conn) {
       // .. rest of your code that calls the callback
    });
}

For more information, check this the blog post by Jeremy Daly. 有关更多信息,请查看Jeremy Daly撰写的博客文章。

https://www.jeremydaly.com/reuse-database-connections-aws-lambda/ https://www.jeremydaly.com/reuse-database-connections-aws-lambda/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM