We're using Sneakers gem in production for a big application. And sometimes the load can be so huge that one specific queue may contain over 250_000 messages. And in such cases, the exception
ActiveRecord::ConnectionTimeoutError:
could not obtain a connection from the pool within 5.000 seconds (waited 5.000 seconds); all pooled connections were in use
occurs regularly.
For the database, we are using Amazon RDS based on PostgreSQL 9.6. max_connections
PostgreSQL config value is 3296.
Our database.yml
file:
production:
adapter: postgresql
encoding: utf8
pool: 40
database: <%= ENV['RDS_DB_NAME'] %>
username: <%= ENV['RDS_USERNAME'] %>
password: <%= ENV['RDS_PASSWORD'] %>
host: <%= ENV['RDS_HOSTNAME'] %>
port: <%= ENV['RDS_PORT'] %>
I guess we can increase a pool
value, but I can't find info about how to calculate max possible value so it will not break anything.
Also, a copy of an application for background processing using Sneakers gem lives separately (but uses the same database) and can be configured individually. But right now it has the same database.yml
config. Sneakers gem config file:
production:
heartbeat: 2000
timeout_job_after: 35
exchange_type: :fanout
threads: 4
prefetch: 4
durable: true
ack: true
daemonize: true
retry_max_times: 5
retry_timeout: 2000
workers: 4
We have no problems with connections pool in base runtime application, but ActiveRecord::ConnectionTimeoutError
occurs in workers very often and it is a really big problem.
So, please help me to reconfigure databese.yml
file:
- How to correctly calculate the max possible value for
pool
option if the databasemax_connections
value is 3296? - How to correctly calculate the max possible value for
pool
option when using the Sneakers gem with the configs above? - Or, if my configs are good, how can I avoid
ActiveRecord::ConnectionTimeoutError
in workers?
Thanks in advance.
While waiting for an answer, I kept looking for a solution.
And, I guess, my very base problem was in connection pool size.
On the Sneakers gem issues tracker, I found a comment with the formula for calculating the required number of connections at full load. I changed the code from the comment a bit, so now it makes calculations taking into account the individual settings of each worker:
Summary: for all workers, I need to have a limit of almost 600 connections at maximum load. But I only had 40. For now, I will use the above code. Hope this helps.