Skip to content
Browse files

Reduce ActiveRecord connection pool from 5 to 2

We're lowering 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).

https://devcenter.heroku.com/articles/concurrency-and-database-connections
  • Loading branch information...
1 parent 94b4079 commit b017d9b17d5776739b59376996dcfe4cb763fd1a @croaky croaky committed Oct 14, 2013
Showing with 5 additions and 2 deletions.
  1. +1 −1 templates/postgresql_database.yml.erb
  2. +4 −1 templates/unicorn.rb
View
2 templates/postgresql_database.yml.erb
@@ -3,7 +3,7 @@ development: &default
database: <%= app_name %>_development
encoding: utf8
min_messages: warning
- pool: 5
+ pool: 2
timeout: 5000
test:
View
5 templates/unicorn.rb
@@ -21,6 +21,9 @@
end
if defined? ActiveRecord::Base
- ActiveRecord::Base.establish_connection
+ config = Rails.application.config.database_configuration[Rails.env]
+ config['reaping_frequency'] = (ENV['DB_REAPING_FREQUENCY'] || 10).to_i
+ config['pool'] = (ENV['DB_POOL'] || 2).to_i
+ ActiveRecord::Base.establish_connection(config)
end
end

0 comments on commit b017d9b

Please sign in to comment.
Something went wrong with that request. Please try again.