How do I set the size of the connection pool?

I’m trying to improve the TechEmpower results for Hanami.

The tests that talk to the database are disproportionately slower than the tests that don’t talk to the database.

As the tests are run on a machine with a lot of cores, Puma (with AUTO_CONCURRENCY=1) will create 57 workers with 3 threads per worker.

I suspect the connection pool size needs to be increased, but I can’t seem to find documentation on how to change it.

Thanks for your help on Hanami’s TechEmpower entry, @p81! :heart:

For increasing the connection pool size, I think you might want one of these?

Set max_connections via database URL, e.g.

DATABASE_URL=postgres://localhost:5432/bookshelf_development?max_connections=10

Or by code inside the :db provider:

Hanami.app.configure_provider :db do
  config.gateway :default do |gw|
    gw.connection_options max_connections: 10
  end
end

Both of these are mentioned here in our database guide. If you think something is missing here or there is any way we could make it clearer, I’m always happy to receive feedback :slight_smile:

And please keep us in the loop regarding your TechEmpower experiments. If this adjustment doesn’t work, I’d love to help you figure it out.

Thanks @timriley
I’ve tried both of them, but setting them to either 1 or 16 doesn’t seem to change the performance.

The problem was a memory leak in rom.rb, that has since been fixed.

@p81 Ah, this makes sense! Thanks for following this up, and I’m glad we got this sorted :slight_smile:

If you’re hitting connection pool limits in Hanami and changing max_connections didn’t help, it’s usually not the pool size itself. As you found out, a memory leak in rom.rb was causing problems. The rule of thumb for tuning, though, is that the pool size should be at least as big as the number of threads times the number of workers. This way, every thread can get a connection. But always check for leaks or transactions that last a long time first. No amount of pool tweaking will help if your ROM or adapters are holding onto connections longer than they should.