2
votes

i have a custom setup of Ruby on rails and using puma as a web server (backed by Nginx - socket)

The database I am connection to is a rds medium (so 296 connections limit). My puma setup is threads 1:32 and 4 workers. With a connection pool of 128.

I have a high load 300 requests/sec and every lets say 1000 requests a longer calculation is made that takes about 3 seconds (getting all the events, making some calculations and updating them).

I am getting the error

ActiveRecord::ConnectionTimeoutError: could not obtain a database connection within 5.000 seconds (waited 5.016 seconds)

But if I look at the rds database only 43 connections are opened. My memory is like 2000 MB out of 7000 MB (the 2 core processors are at 100%) I am wondering why do I get a connection timeout even if all my connections are not opened (and of course is the puma configuration ok)?

Thank for your help!

EDIT:

In my puma.rb I have:

on_worker_boot do
  ActiveRecord::Base.connection_pool.disconnect!

  ActiveSupport.on_load(:active_record) do
    config = Rails.application.config.database_configuration[Rails.env]
    config['reaping_frequency'] = ENV['DB_REAP_FREQ'] || 10 # seconds
    config['pool']              = ENV['DB_POOL'] || 128
    ActiveRecord::Base.establish_connection
  end
end
1
check your database pool settings in database.yml - Mohammad AbuShady
No pool settings just adapter, encoding, host, port, username, password, database, su_password, su_username. - Marko Jurinčič
I'm not sure if there's a default value or if nil = unlimited - Mohammad AbuShady
The default size is 5, but I have added my puma.rb config for the 128 pool size. - Marko Jurinčič
well i have no idea then, guess you'll need to wait for another person to answer - Mohammad AbuShady

1 Answers

0
votes

As mentioned here in the rails configuration guide about database pooling, when all the db connections are exhausted, ActiveRecord will wait for one to free up, I assume the number you increased is the http connection limit, not the db connection limit,
You could edit your database.yml and increase you connection limit to say 296 which is the limit of the rds instance

production:
  adapter: mysql2
  database: /path/to/sock
  pool: 296
  # username, password, etc