Setting ActiveRecord's connection pool size on Heroku with Puma and Sidekiq
UPDATE: A better way to set the connection pool size.
Watsi has used Puma since the beginning. Even though we run on CRuby 1.9.3 on Heroku (so we don’t get any actual parallelism) I just love how simple & effective Puma is. One of the few options I set with Puma is the thread min & max.
Since ActiveRecord’s default connection pool size is 5, one might (and we did) see errors that the pool is not big enough when running Puma, or Unicorn, with more than 5 threads/workers. How might we bump that up?
change the database.yml
If you have control over your database.yml
file, you can just change
the pool size.
… or establish a new AR connection
I thought I could establish a new connection
and pass in an option, but unfortunately that doesn’t work. I wish I could pass options
all the way to the ConnectionResolver
class but I can’t. Plus,
connection_url_to_hash
just looks at the query string for options like pool
anyway. Kind of a bust.
So, we’re on Heroku, and our only option is to mangle the DATABASE_URL
.
We could get the DATABASE_URL
(Don’t do this!)
But how would we ever handle a change to our DATABASE_URL
by Heroku themselves?
Sounds like trouble. Heroku doesn’t recommended this (just asked during
YC office hours) so we need to change the DATABASE_URL
in a safe way.
What about Sidekiq?
Yes, we also want to set the connection pool for Sidekiq to the total number of workers, like Mike recommends in the wiki.
How we do it
Let’s take a look at our config/application.rb
With a commit
added in puma 2.0.0.b6
we can now get at the max_threads
setting. Puma only responds to
cli_config
when it’s started from the command line, so we’re cool.
For Sidekiq, we do the same thing in config/initializers/sidekiq.rb
Don’t mangle Heroku’s DATABASE_URL!
That being said, inside your app it’s not so bad to tack on a query param. I did it the safest way I knew how. If you know of a better way, please share!
Hope that helps
This has been deployed for a few weeks now and we don’t see any connection pool issues anymore. If you have a way you do it, please tweet @jc00ke.
- Jesse