How to find current connection pool size on heroku
Asked Answered
G

3

28

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.

Geraldgeralda answered 25/1, 2013 at 18:45 Comment(0)
P
60

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.

Pogonia answered 1/2, 2013 at 23:26 Comment(5)
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?Geraldgeralda
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.Pogonia
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.Geraldgeralda
As of Rails 4.1 you can set these values directly in your config/database.yml as per this heroku devcenter article.Husain
On 4.2 you don't have to instance_eval, calling ActiveRecord::Base.connection_pool.size works fine.Craw
P
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
Promising answered 24/7, 2014 at 19:11 Comment(0)
A
0

What you need is.

ActiveRecord::Base.connection_pool.stat

It will return.

{:size=>10, :connections=>3, :busy=>1, :dead=>0, :idle=>2, :waiting=>0, :checkout_timeout=>5.0}

If you interested in Multi-Threaded DB work, Please check my answer here.

Abc answered 6/9, 2023 at 15:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.