albertyw/albertyw.com

View on GitHub
app/notes/20160626-2158.md

Summary

Maintainability
Test Coverage
Calculating Rails Database Connections

calculating-rails-database-connections

1466978327

I recently ran into a problem with calculating the number of database connections
used by Rails.  It turns out that for a typical production environment, it's
actually hard to find the maximum number of connections that would be made.
MySQL and PostgreSQL also have relatively low default maximum connection limits
(151 and 100, respectively) which means it's really easy to get an error like
"PG::ConnectionBad: FATAL:  sorry, too many clients already."

After some digging, I believe the formula for getting the maximum number of
open connections is by multiplying the "pool" value in config/database.yaml
against the number of processes (workers in Puma).  If you're running sidekiq
or other background job processor, you'll also need to add in the number of
background processors into your web server's processes count.