Skip to content

Heroku configuration

David Albert edited this page Jul 28, 2014 · 1 revision

Database connections

Heroku limits the number of database connections an app can have. For the database that we're currently using (hobby-dev), the limit is 20. This affects how we configure Puma and the Active Record connection pool.

Here is our current Heroku configuration:

  • 2 web dynos
  • 1 delayed_job worker dyno
  • 2 Puma workers per web dyno (this means 3 Unix processes per web dyno; 1 master, and 2 Puma workers)
  • 2 threads per Puma worker

We also have one WebSocket handling thread per worker.

We want to have one available database connection per thread. Our per-process connection pool max connection limit is 3 (2 threads per worker, plus 1 thread for the WebSocket handler). Eventually, after the app has booted and has handled a bunch of requests, we expect each worker process to have 3 database connections open. This makes 12 connections for all of our web dynos. Our delayed_job worker has one additional connection.

Additionally, because we have configured Puma to preload our app before forking its worker processes, each Puma master process has one database connection that is doing nothing, bringing us up to a grand total of 15 database connections when we reach steady-state. Puma does not currently provide a before_fork hook to let us disconnect from the database in the master processes.

Clone this wiki locally