thoughtbot/suspenders — pull request #234 by croaky (2013-10-15 03:28:36+00:00)
Title: Reduce ActiveRecord pool setting to 2 from 5

When increasing concurrency by using a multi-process web server like Unicorn,
we must be aware of the number of connections our app holds to the database
and how many connections the database can accept. Each process requires a
different connection to the database. To accommodate this, ActiveRecord
provides a connection pool that can hold several connections at a time.

We are setting the pool to 2 connections or the value specified in the
`DB_POOL` environment varriable. We are also setting `reaping_frequency`
which is available in Rails 4.

Heroku provides managed Postgres databases. Different tiered databases have
different connection limits. The Starter Tier "Dev" and "Basic" databases are
limited to 20 connections. Production Tier databases
(plans Crane and up) have higher limits.

Once our database has the maximum number of active connections, it will no
longer accept new connections. This will result in connection timeouts from
our application and will likely cause exceptions.

It is possible for a connection to get into a bad or unknown state. Due to
this, Heroku recommends setting the pool of our application to either 1 or 2
to avoid zombie connections from saturating our database.

With Unicorn, this could mean that over time a 3 worker dyno which normally
consumes 3 database connections could be holding as many as 15 connections (5
default connections per pool times 3 workers). To limit this threat, we lower
the connection pool to 2 and enable connection reaping.

`reaping_frequency` is telling ActiveRecord to check to see if connections
are hung or dead every 10 seconds. 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.

https://devcenter.heroku.com/articles/concurrency-and-database-connections