在 Rails 中禁用连接池以使用 PgBouncer [英] Disabling Connection Pooling in Rails to use PgBouncer

查看:55
本文介绍了在 Rails 中禁用连接池以使用 PgBouncer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个访问大型 PostgreSQL 数据库的 Ruby on Rails 4.2.8 项目.我们将使用 PgBouncer 添加一个用于连接池的新服务器.

We have a Ruby on Rails 4.2.8 project that accesses a large PostgreSQL database. We are going to add a new server for connection pooling using PgBouncer.

既然 PgBouncer 会处理 db 连接池,我们是否需要关闭 Rails 自动连接池?我们的 database.yml 中没有配置任何内容,因此我认为使用的是默认值(池)5.

Since PgBouncer will handle the db connection pooling, would we need to turn off Rails automatic connection pooling? We do not have anything configured in our database.yml, so I would think that the default (Pool) of 5 is being used.

  1. 添加 PgBouncer 是否意味着我们应该关闭 Rails连接池?
  2. 如果是这样,它是如何工作的,我们是否只需设置 Pool到 database.yml 中的 0?

谢谢

推荐答案

TLDR;不要改变任何东西

TLDR; don't change anything

rails 中的池化与 PGBouncer 中的池化不同.rails 连接池是该进程中任何线程可用的一组连接,通常只有 1 个. rails 池中的每个连接都将连接到您的 postgres 数据库或 PGBouncer,如果它位于 postgres 前面.在大型 rails 应用程序中,您将在每台服务器上运行多个 rails 进程,并在负载均衡器后面运行多个服务器.像这样:

The pooling in rails is different than the pooling in PGBouncer. The rails connection pool is a group of connections available to any thread within that process, usually just 1. Each connection in your rails pool will have a connection to your postgres database, or PGBouncer if that's sitting in front of postgres. In a large rails app, you'll run multiple rails processes on every server and multiple servers behind a load balancer. Something like this:

如果没有 PGBouncer,与 postgres 的每个连接都会创建一个新的 postgres 进程.在规模上,您需要限制运行的 postgres 进程的数量,这样您就不会最大化 CPU 和内存.PGBouncer 汇集来自所有进程和所有服务器的所有 rails 池的连接,并在它们之间高效切换.

Without PGBouncer, every connection to postgres creates a new postgres process. At scale you'll want to limit the number of postgres processes that run so you don't max out CPU and memory. PGBouncer pools connections from all of your rails pools across all processes and all servers, and efficiently switches between them.

这篇关于在 Rails 中禁用连接池以使用 PgBouncer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆