In a Rails app on Puma, how should Active Record's connection pool relate to threads and workers, and what breaks when it is too small?
answer
- one pool per process
- at least as many connections as threads
- checkout_timeout defaults to 5 seconds
- ActiveRecord::ConnectionTimeoutError
- server total = workers x pool
basics
~20 sEach Puma process has its own Active Record pool, which must hold at least as many connections as that process has threads. Smaller, and threads wait 5 seconds, then raise ActiveRecord::ConnectionTimeoutError; the server's total is workers times pool.
solid answer
~40 sA Puma thread serving a request checks a connection out of its process's pool when it first queries, and the connection goes back when the request finishes. So the pool size is compared with **threads per process**, not with the whole server: the generated puma.rb comment says pools should provide at least as many connections as threads. If all connections are in use, the next thread waits up to `checkout_timeout` (5 seconds by default) and then gets `ActiveRecord::ConnectionTimeoutError`: "could not obtain a connection from the pool". Workers multiply the total, because each forked worker builds its own pool. Four workers with a pool of 3 can hold 12 connections, and job processes come on top. Rails 8.1 names the setting `max_connections` (the old `pool:` key is deprecated).
go deeper
Recall that each Puma process needs at least as many database connections as it has threads, and that one env var drives both.
Explain per-process pools, the 5-second checkout_timeout, ConnectionTimeoutError, and why workers multiply the total.
Size the database-side total across web workers, job processes and servers, and diagnose timeout bursts against an idle database.
Decide when connection totals force an architectural change, such as a connection pooler or fewer, larger processes, instead of tuning pools.
## Two pools that must line up A Rails app on Puma has two independent pools in every process: - **Puma's thread pool**: the request threads set by `threads` in `config/puma.rb`, 3 by default from `RAILS_MAX_THREADS`. - **Active Record's connection pool**: database connections the process may hold, named `max_connections` in Rails 8.1's database.yml (formerly `pool`). They matter to each other because a request thread that touches the database needs a connection of its own while it works. Active Record leases a connection from the process's pool to the thread and returns it to the pool when the request completes. If the thread pool is larger than the connection pool, some threads will at times have nothing to lease. ## The rule: per process, at least as many connections as threads The comment in the generated `config/puma.rb` states it directly: any library that uses a connection pool should provide at least as many connections as there are threads. That comment still says Active Record's `pool` parameter, but the key it means is now `max_connections`. The generated files already satisfy the rule with headroom: | Generated file | Reads | Fallback when unset | |---|---|---| | `config/puma.rb` | `RAILS_MAX_THREADS` | 3 threads | | `config/database.yml` | `RAILS_MAX_THREADS` | 5 connections | Setting `RAILS_MAX_THREADS` moves both together, and if it is unset the pool of 5 exceeds the 3 threads. Headroom is useful because Puma's request threads are not always the only consumers. Any other thread in the same process that queries the database, such as work the app hands to a background thread, also leases from that pool. ## What failure looks like When every connection is leased and another thread asks for one: 1. The thread waits on the pool's queue for up to **`checkout_timeout`**, 5 seconds unless configured. 2. If no connection comes back in time, Active Record raises **`ActiveRecord::ConnectionTimeoutError`**, a subclass of `ActiveRecord::ConnectionNotEstablished`, with the message "could not obtain a connection from the pool within 5.000 seconds ... all pooled connections were in use". 3. The request fails with a 500. Under load the symptom is bursts of these errors plus requests that stall for about 5 seconds before failing. The pool does not open an extra connection past its limit, and it never hands back nil. It blocks, then raises. ## Workers multiply the total Cluster mode forks workers, and **each worker builds its own pool**. Rails discards any pool inherited from the master after a fork, so a worker never reuses the master's connections. The database therefore sees, from one server, **workers x `max_connections`** connections at most. Four workers with a pool of 3 can open 12 connections per server; ten servers, 120. Add the job processes (Solid Queue workers each have their own pools), consoles and migrations. This total, not the per-process figure, is what you compare with the database's connection limit. It is also why an oversized pool hurts too: setting `max_connections: 50` "to be safe" across 40 worker processes asks for 2,000 connections the database may refuse. ## Seeing the pool at runtime In a console or an instrumented endpoint, `ActiveRecord::Base.connection_pool.stat` returns a Hash with the pool's `size`, how many connections exist, how many are `busy`, `idle` and `dead`, how many threads are `waiting`, and the `checkout_timeout`. A non-zero `waiting` under load, with `busy` equal to `size`, is the pool-too-small signature. Compare `size` with the Puma thread count printed in the boot banner: they come from the same variable unless someone overrode one of them. ## Sizing checklist - Size the pool from **threads per process**, never from threads times workers. - Keep `RAILS_MAX_THREADS` as the single knob for both files, as generated. - Add headroom only for known extra database-using threads in the same process. - Multiply by worker processes and every other process type to get the database-side total. - If the total is too high, reduce threads or workers, or put a connection pooler in front of the database, rather than shrinking the pool below the thread count.
- Why is setting the pool to threads times workers a mistake?Pools are per process, so threads times workers inflates every worker's pool by the worker count. Each worker's threads can never use more than their own thread count of connections, so the extra slots are wasted. If the app ever leaks connections they become real, and the database-side total balloons for no gain.
- Under load, a Rails app logs bursts of ActiveRecord::ConnectionTimeoutError but the database is idle; what do you check first?Compare each process's thread count with its pool size: a pool smaller than Puma's threads, or extra threads in the process that also query the database, starve request threads. Then look for code that holds a connection for a long time, such as a slow external call made while a lease is held. The idle database confirms the bottleneck is inside the Ruby process.
- All Puma workers on one server draw from a single shared connection pool.
- The pool should equal Puma threads times workers.
- A thread that finds no free connection gets nil and fails on its next query.
- pool: is still the current database.yml key in Rails 8.1.
- RAILS_MAX_THREADS only affects Puma and has nothing to do with the database config.
Assumes Rails 8.1, where database.yml names the pool size max_connections and the older pool: key is a deprecated alias.