Last Updated: February 25, 2016
· croaky

Lower the Default ActiveRecord Connection Pool for Heroku Postgres

In Suspenders, thoughtbot's Rails template, we lower the ActiveRecord connection pool maximum from the default of 5. A basic Rails app with 2 Heroku web dynos that each have 3 Unicorn workers, plus two Delayed Job background workers could have up to 40 database connections if ActiveRecord or our code is causing hung connections.

While the Rails 4 reaper will close dead connections every 10 seconds, limiting the total to 1 per process (8 in this case), we still maintain some Rails 3 apps. Also, while it is likely that over time our application may have a few connections that hang, if something in our code is causing hung connections, the reaper will not be a permanent fix to the problem.

Depending on the pricing plan, production Heroku Postgres databases have a maximum of 60 to 500 connections. That sounds like a lot, but it's likely that before the max is hit, we'll run into some other resource contention like CPU, IO throughput, or memory. Database throughput will hit a local max and then degrade as number of connections increase.

2 is a good default as opposed to 1 because New Relic's auto-EXPLAIN feature also adds 1 connection per Unicorn (or background job).

Read more in Heroku's Dev Center article.