dsmorey
dsmorey

Reputation: 473

Disabling Connection Pooling in Rails to use 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.

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. Does the addition of PgBouncer mean we should turn off Rails connection pooling?
  2. If so, how does that work, do we just set Pool to 0 in the database.yml?

Thank you

Upvotes: 3

Views: 3505

Answers (1)

jvans
jvans

Reputation: 2915

TLDR; don't change anything

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:

pooling architecture

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.

Upvotes: 8

Related Questions