1

Can I disable Ruby on Rails connection pooling completely?

And would this be okay considering PgBouncer already handles database connection pooling?

mu is too short
  • 426,620
  • 70
  • 833
  • 800
Basil Musa
  • 8,198
  • 6
  • 64
  • 63

1 Answers1

3

No.

PgBouncer advertises itself to clients as a Postgres server and then manages connections to the actual Postgres server. We don't need to get into any further detail than that for PgBouncer -- from the Rails side of things, the PgBouncer instance is the Postgres server, so let's explain why starting from there.

In Rails there are two primary factors to consider for concurrency: the number of inbound client requests that can be made to its web server and the size of the connection pool for the database.

If your connection pool size is 1 then your application is effectively single-threaded: every time an inbound client request is made that must make a database query, the request must check out a connection to the database from the pool. When the pool size is 1 and the number of concurrent inbound requests is greater than 1, every request after the first request must be put on hold while the first request completes its query and returns the connection to the pool. If the first request takes a long time to complete then subsequent requests may timeout while waiting for an available connection from the pool.

So from the Rails side, you want to have a connection pool with a size larger than 1 to allow for concurrency. If you set it to a size of 1 then it doesn't matter how you have PgBouncer configured; Rails will always make at most one connection to the database and all threads must share that single connection.

You can read more about connection pools in Rails at ActiveRecord::ConnectionAdapters::ConnectionPool < Object.

anothermh
  • 9,815
  • 3
  • 33
  • 52
  • Thanks for your detailed answer. But I was asking if connection pooling on Ruby can be totally disabled? and how? I understand that size of 1 is not good. – Basil Musa Jul 14 '20 at 11:25
  • 1
    Can you? No, it's too deeply integrated into Rails. Can you do effectively that by setting the pool size to 1? Yes. Should you? No. (keep in mind that if you COULD, then every single call to the database would make a new connection and have to handle the build up/tear down of the connection causing every connection to be that much slower) – anothermh Jul 14 '20 at 17:00
  • About single-threadedness, and very much to invite refutation – I'm not entirely sure I have this right: My impression is that with a single-threaded server like Unicorn, you *could* set the pool size to 1, if you don't kick off your own threads making queries. Heroku appears to recommend 1 or 2, to account for zombie connections: https://devcenter.heroku.com/articles/concurrency-and-database-connections#calculating-required-connections – Henrik N Mar 23 '21 at 17:09