How to size your Sidekiq connection pool
Every thread that runs a database query needs its own connection from the Rails pool. In a Rails app that means two separate pools to reason about: your web process (Puma) and your worker process (Sidekiq). The pool size is set per process in config/database.yml, and the single most common production error — could not obtain a connection from the pool — happens when a process runs more threads than its pool has connections.
The rule: pool size must cover your threads
For the web process, your pool must be at least your Puma thread count. For the worker process, your pool must be at least your Sidekiq concurrency. On Solid Queue the same idea applies, with the guidance to keep worker threads at or below your pool size minus two — two connections are reserved for the queue's own polling and heartbeat. A good default is to set the pool to your thread count plus a small amount of headroom.
A common trap: the default Rails database.yml sizes the pool from RAILS_MAX_THREADS. If that variable is set to your Puma thread count and your Sidekiq process inherits it, the worker gets a web-sized pool. Size the pool from a variable you set per process — or give it a default large enough for the biggest one.
Don't exceed Postgres max_connections
The second failure mode is global: the sum of all connections across every Puma worker and every Sidekiq process can exceed Postgres's max_connections ceiling (minus the few slots Postgres reserves for superusers — three by default). When that happens, new connection attempts are refused database-wide. If your peak total approaches that ceiling, the usual fix is a connection pooler like PgBouncer in front of Postgres, or lowering per-process concurrency.
Frequently asked questions
How many connections does Sidekiq need?
One per running worker thread. If Sidekiq concurrency is 10, its process needs a pool of at least 10 (plus a little headroom) so no job stalls waiting to check out a connection.
Should Puma and Sidekiq share the same pool size?
They don't have to. They're separate processes with separate pools, so size each to its own thread count — Puma threads for the web pool, Sidekiq concurrency for the worker pool. One database.yml can serve both if the pool is read from an environment variable you set per process.
What causes ActiveRecord::ConnectionTimeoutError?
A thread asked the pool for a connection and none was free within the timeout. Almost always it means the pool is smaller than the number of threads contending for it — a configuration issue, not a database fault.