27

We have a rails 3.2(.11) app with many dynos running on the heroku bamboo stack, connecting to a MySQL RDS server. There seem to be some issues with our current database connections, so we are trying to debug exactly how many connections each dyno is spinning up. I know I can set the size of a connection pool in the DATABASE_URL config on heroku, but can't seem to find out how many connections are currently being used by default.

Two main questions:

1) How can I find the size of the connection pool used by heroku?

2) Is there any reason why a dyno would need a connection pool size greater than 1? My understanding is that rails can only execute 1 request at a time so one database connection should be all that is needed as far as I can see.

cmwright
  • 3,406
  • 5
  • 26
  • 33

2 Answers2

57

To check the pool size, start a heroku console heroku run rails c, and run:

ActiveRecord::Base.connection_pool.size

Some webservers such as Puma are multithreaded, so the DB pool size matters. You can also run a multi-threaded worker such as Sidekiq, which also will be affected by the pool size.

Note that Heroku will ignore your database.yml file. To set the pool size you can append ?pool=25 to the DATABASE_URL in your heroku app's configuation.

siegy22
  • 4,295
  • 3
  • 25
  • 43
mpoisot
  • 7,761
  • 4
  • 27
  • 21
  • That works perfectly, thanks. I can see why something multithreaded needs a larger connection pool, but we're running thin on these dynos so 1 connection should be plenty right? – cmwright Feb 05 '13 at 15:17
  • Correct, Thin only needs one DB connection. I doubt your problems are with Thin's pool setting, unless you're doing some threading. I would check to see what the max connections setting is for you DB, and try to increase that. – mpoisot Feb 05 '13 at 19:52
  • Our problem is that on restart we get frozen array errors randomly (about 5% of the time) and the only way to fix the dyno is to restart it at that point. We've traced this down to somehow getting a stale mysql connection on startup. Hoping that bringing down the connection pool size should relieve this solution, and seeing as we don't need to the connections anyways it will cut some fat. – cmwright Feb 05 '13 at 20:07
  • 2
    As of Rails 4.1 you can set these values directly in your `config/database.yml` as per [this heroku devcenter article](https://devcenter.heroku.com/articles/concurrency-and-database-connections#connection-pool). – markquezada Jun 25 '15 at 00:54
  • 4
    On 4.2 you don't have to instance_eval, calling `ActiveRecord::Base.connection_pool.size` works fine. – Duke Aug 14 '15 at 15:11
10

This information is available via an interface in Rails https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_handling.rb#L98-L106 it is in Rails 3+

ActiveRecord::Base.connection_config
# => {:adapter=>"postgresql", :encoding=>"utf8", :pool=>5, :host=>"localhost", :database=>"triage_development"}

You can use this to get the current pool size without needing to eval or relying on the existence of an unexposed instance variable however in rails 3 it may return nil if it hasn't been explicitly set

ActiveRecord::Base.connection_config[:pool]
# => 5
Schneems
  • 14,918
  • 9
  • 57
  • 84