简体   繁体   English

AWS RDS 减少了来自 Lambda 的连接时间

[英]AWS RDS reducing connection time from Lambda

I'm calling my AWS Rds database from within an AWS lands function.我正在从 AWS lands 函数中调用我的 AWS Rds 数据库。 I'm successfully able to call the database just fine, but I'm trying to reduce the execution time.我能够成功调用数据库就好了,但我正在尝试减少执行时间。 I'm finding that 95% of the time is spent creating the connection to the AWS RDS proxy endpoint.我发现 95% 的时间都花在创建与 AWS RDS 代理端点的连接上。 Is there anyway to speed this up?有没有办法加快这个速度?

The code below typically runs in 500-700ms.下面的代码通常在 500-700 毫秒内运行。 mysql.createConnection() takes between 400-600ms to complete. mysql.createConnection()需要mysql.createConnection()才能完成。 The query itself is <50ms and the JWT verification is <20ms.查询本身 <50ms,JWT 验证 <20ms。

Index.js索引.js

'use strict';

const constants = require("./constants");
const OktaJwtVerifier = require('@okta/jwt-verifier');
const Database = require('./database');
const verifier = new OktaJwtVerifier({
    'issuer': 'https://preview.okta.com/oauth2/default'
});
let db = new Database({
    "host": process.env["rds_proxy_endpoint"],
    "user": process.env["db_user"],
    "database": process.env["database"],
    "password": process.env["db_pass"]
});

let start;
let end;
exports.handler = async(event) => {
    let response = {};
    console.log("Starting JWT Validation");
    start = new Date();
    await verifier.verifyAccessToken(event.token, 'api://default').catch(err => {
        console.log("Invalid Token");
        response.statusCode = constants.HTTPSTATUS_UNAUTHORIZED;
        response.body = err.userMessage;
    });
    end = new Date() - start;
    console.log("JWT Verification Time: %dms", end);
    
    let params = ["string"];
    
    return await db.execute("CALL GetUsersGames(?)", params);
};

Database.js数据库.js

'use strict';

const mysql = require('mysql2/promise');
const constants = require('./constants');

let start;
let end;
module.exports = class Database {
  constructor(config) { 
    this.config = config;
  } 
  
  async execute(proc, params){
    let response = {};
    try {
      console.log("Creating connection...");
      start = new Date();
      let connection = await mysql.createConnection(this.config);
      end = new Date() - start;
      console.log("Connection Execution Time: %dms", end);
      start = new Date();
      const rows = await connection.execute(proc, params);
      end = new Date() - start;
      console.log("Query Time: %dms", end);
      //console.log(JSON.stringify(rows));
      response["statusCode"] = constants.HTTP_OK;
      response["body"] = JSON.stringify(rows[0][0]);
    } catch(err){
        console.log("ERROR: " + err);
        response["statusCode"] = constants.HTTP_INTERNAL_SERVER_ERROR;
        response["body"] = "Internal Server Error";
    }
    return response;
  }
};

aws has sth called aws rds proxy in lambda console. aws 在 lambda 控制台中有一个叫做 aws rds 代理的东西。 check it out一探究竟

https://aws.amazon.com/blogs/compute/using-amazon-rds-proxy-with-aws-lambda/ https://aws.amazon.com/blogs/compute/using-amazon-rds-proxy-with-aws-lambda/

Creating a db connection is high I/O operation.创建数据库连接是高 I/O 操作。 That is the reason in any production environment we always create connection Pool.这就是在任何生产环境中我们总是创建连接池的原因。 Any application servers like JBoss etc. supports connection pool as jboss feature itself.任何像 JBoss 等应用服务器都支持连接池作为 jboss 特性本身。 Even hibernate does support.即使是休眠也支持。

In serverless scenario;在无服务器场景中; u can create connection and keep it in-memory like Redis/Memcached.你可以像Redis/Memcached一样创建连接并将其保存在内存中。 I would prefer redis.我更喜欢redis。 They are provided as a service through ElastiCache.它们通过 ElastiCache 作为服务提供。

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

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