简体   繁体   English

使用 Knex.js 进行的每个数据库查询打开和关闭与 RDS 代理的连接的缺点?

[英]Downsides to opening and closing a connection to RDS Proxy with every DB query made with Knex.js?

The Postgres query builder my Lambda functions use, Knex , uses prepared statements so I'm unable to fully take advantage of RDS Proxy since the sessions are pinned.我的 Lambda 函数使用的 Postgres 查询生成器Knex使用准备好的语句,因此我无法充分利用 RDS 代理,因为会话已固定。 I try to ensure that the lambdas run for as little time as possible so that the pinned session completes as quickly as possible and its connection is returned to the pool.我尝试确保 lambda 运行的时间尽可能短,以便固定的 session 尽快完成并将其连接返回到池中。

I was wondering how I might be able to make the sessions shorter and more granular and thinking about creating and closing a connection to AWS RDS Proxy with each query.我想知道如何才能使会话更短、更细化,并考虑在每次查询时创建和关闭与 AWS RDS 代理的连接。

What performance considerations should I be considering to determine the viability of this approach?我应该考虑哪些性能因素来确定这种方法的可行性?

Things I'm thinking of:我在想的事情:

  • RDS Proxy connection overhead (latency and memory) RDS Proxy 连接开销(延迟和内存)
  • The time that RDS Proxy takes to return a closed connection back to the pool and make it reusable by others (haven't been able to find documentation on this) RDS Proxy 将关闭的连接返回到池中并使其可被其他人重用所花费的时间(无法找到关于此的文档)
  • Overhead of Knex's local connection pool Knex 本地连接池的开销

Using RDS proxy when building applications with Lambda functions is a recommended infrastructure pattern by AWS.在构建具有 Lambda 函数的应用程序时使用 RDS 代理是 AWS 推荐的基础设施模式。 Relational Databases are not built to handle tons of connections, while Lambdas can scale to thousands of instances.关系数据库不是为处理大量连接而构建的,而 Lambda 可以扩展到数千个实例。

RDS Proxy connection overhead (latency and memory) RDS Proxy 连接开销(延迟和内存)

This would definitely increase your latency, but you will see a great improvement in the CPU and memory usage of your database, which would ultimately prevent unnecessary failures.这肯定会增加您的延迟,但您会看到 CPU 和 memory 数据库使用率有了很大改善,这最终会避免不必要的故障。 It's a good trade-off when you can do a lot of other optimizations on the lambda side.当您可以在 lambda 端进行许多其他优化时,这是一个很好的权衡。

The time that RDS Proxy takes to return a closed connection back to the pool and make it reusable by others (haven't been able to find documentation on this) RDS Proxy 将关闭的连接返回到池中并使其可被其他人重用所花费的时间(无法找到关于此的文档)

While working with Lambdas, you should drop the connection to your RDS proxy, as soon as you finish processing your logic without worrying about the time the RDS proxy would take to return the closed connection back.在使用 Lambdas 时,您应该在完成逻辑处理后立即断开与 RDS 代理的连接,而不必担心 RDS 代理返回已关闭的连接所需的时间。 Once the connection is dropped, the RDS proxy keeps it warm in the pool of connections it maintains for a certain duration of time.连接断开后,RDS 代理会在它维护的连接池中将其保持一段时间。 If another lambda tries to make a connection meanwhile, it can share the same connection which is still warm in the pool.如果另一个 lambda 尝试同时建立连接,它可以共享池中仍然热的相同连接。 Dropping the database connection at the right time from your lambda would save you lambda processing time -> money.在正确的时间从您的 lambda 中删除数据库连接将为您节省 lambda 处理时间 -> 金钱。

Overhead of Knex's local connection pool Knex 本地连接池的开销

I would suggest not using Knex local connection pool with lambda as it won't do any good (Keep the pool max to 1).我建议不要将 Knex 本地连接池与 lambda 一起使用,因为它不会有任何好处(将池最大值保持为 1)。 Every lambda execution is independent of another, the pool will never be shared and the connection doesn't persist after the execution completes unless you plan to use it with a serverless-offline kind of local framework for development purposes.每个 lambda 执行都独立于另一个执行,池永远不会共享,执行完成后连接不会持续存在,除非您计划将其与serverless-offline类型的本地框架一起用于开发目的。

Read More about AWS Lambda + RDS Proxy usage: https://aws.amazon.com/blogs/compute/using-amazon-rds-proxy-with-aws-lambda/阅读有关 AWS Lambda + RDS 代理使用的更多信息: https://aws.amazon.com/blogs/compute/using-amazon-rds-proxy-with-aws-lambda/

AWS Documentation on where to use RDS Proxy: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/rds-proxy-planning.html关于在何处使用 RDS 代理的 AWS 文档: https://docs.aws.amazon.com/AmazonRDS/latest/AuroraUserGuide/rds-proxy-planning.html

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

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