skip to content

After raising a Sidekiq process's concurrency to 25 in a Rails 8.1 app, jobs start failing with ActiveRecord::ConnectionTimeoutError; why, and how do you size it?

level: seniorimportance: should knowfreq 45%

answer

  1. one connection per busy job thread
  2. pool size is per process
  3. Sidekiq reads RAILS_MAX_THREADS, default 5
  4. max_connections in Rails 8.1 database.yml
  5. GVL: threads help I/O, not CPU

basics

~20 s

Each Sidekiq thread that queries needs its own Active Record connection, but the pool still holds the generated five, so waiting threads time out after checkout_timeout. Run Sidekiq with RAILS_MAX_THREADS=25 so concurrency and max_connections match.

solid answer

~40 s

Sidekiq runs jobs on threads inside one process, and Active Record gives each thread that queries its own connection from that process's pool. The generated `config/database.yml` sizes the pool with `max_connections: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>`, while `:concurrency: 25` in `config/sidekiq.yml` (or `-c 25`) changes only Sidekiq's thread count. With 25 threads and 5 connections, waiting threads give up after `checkout_timeout` (5 seconds) with `ActiveRecord::ConnectionTimeoutError`, and the jobs go to the retry set. Fix it by starting Sidekiq with `RAILS_MAX_THREADS=25`, which Sidekiq uses as its concurrency and `database.yml` as its pool, and then check the totals: every process's pool across web and job hosts must fit the database server's connection limit. Then ask whether 25 is useful at all: on CRuby threads overlap I/O, not CPU work.

code

bash · 8 lines
bash
# Web process: Puma threads and pool both from the variable
RAILS_MAX_THREADS=3 bin/rails server

# Job process: Sidekiq concurrency and pool both from the variable
RAILS_MAX_THREADS=10 bundle exec sidekiq -C config/sidekiq.yml

# Check the pool the job environment gets
RAILS_MAX_THREADS=10 bin/rails runner 'puts ActiveRecord::Base.connection_pool.max_connections'   # => 10

go deeper

for a junior

Remember that each Sidekiq thread needs its own database connection and that the pool size in database.yml must be at least the thread count.

for a middle

Explain where each number comes from: Sidekiq's default of 5, RAILS_MAX_THREADS, -c and sidekiq.yml, and max_connections with checkout_timeout in the pool.

for a senior

Diagnose the mismatch from the error pattern, size pools per process type, total connections across the fleet, and choose processes over threads for CPU-heavy jobs.

for a principal

Plan capacity end to end: database connection budgets, poolers in front of the database, and how job concurrency competes with web traffic for the same limit.

## Two numbers that must agree A **Sidekiq process** runs jobs on a fixed number of threads, its **concurrency**. Sidekiq's default is **5**; if the `RAILS_MAX_THREADS` environment variable is set, Sidekiq uses that instead; and `-c 25` or `:concurrency: 25` in `config/sidekiq.yml` overrides both. **Active Record** keeps a **connection pool per process**, and a thread must check a connection out of it to run a query. The generated `config/database.yml` sizes that pool from the same variable: ```yaml # config/database.yml as Rails 8.1 generates it (PostgreSQL) default: &default adapter: postgresql encoding: unicode max_connections: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> ``` In Rails 8.1 the key is `max_connections`; the older name `pool` is still read, and a configuration that sets both to different values is rejected. The design intent is that one variable sizes both the threads and the pool, so they match by default. ## What goes wrong at 25 Raising concurrency in `config/sidekiq.yml` changes only the thread count. Now 25 threads share 5 connections: 1. Five jobs check out connections and start querying. 2. The other twenty threads block in the pool waiting for one to be returned. 3. A thread that waits longer than **`checkout_timeout`** (5 seconds unless configured) raises `ActiveRecord::ConnectionTimeoutError`. 4. The exception escapes `perform`, so Sidekiq puts the job in the **retry set**, and the error rate climbs exactly when the queue is busiest. The errors are intermittent because they depend on how many threads hit the database at once and for how long; jobs that hold a connection through long transactions or slow queries make them worse. ## Sizing it properly Work through it from the job host outwards: - **Match pool to threads per process.** Start Sidekiq with `RAILS_MAX_THREADS=25 bundle exec sidekiq` and drop the hard-coded concurrency, so the thread count and `max_connections` come from the same number. If web and job processes need different values, set the variable per process type rather than globally. - **Count capsules.** Additional capsules declared in an initializer add their own threads; the process needs connections for the sum of all capsules' concurrency. - **Count the whole fleet.** The database server has its own connection limit. Total demand is roughly the sum over every process of its pool: web processes (Puma's generated config uses `RAILS_MAX_THREADS`, default 3) plus every Sidekiq process. Four job hosts at 25 plus six web processes at 5 is already 130 connections. - **Redis is handled for you.** Each capsule gets a Redis pool sized to its concurrency, plus a small internal pool, so Redis connections need no matching setting. ## Is 25 even useful? On CRuby, the **Global VM Lock** lets only one thread run Ruby code at a time. Threads still overlap when they wait on I/O: Redis, the database, an HTTP API, object storage. So the right concurrency depends on the work: | Job profile | Effect of more threads | Better lever | |---|---|---| | Mostly waiting on network or disk | throughput rises until CPU saturates | moderate concurrency, watch CPU | | Mostly Ruby computation | little gain, more memory and contention | more processes, roughly one per core | | Mixed | depends on the ratio | separate queues and processes per profile | Sidekiq's own README notes that real applications rarely need concurrency above 10. For a stock-photo site's thumbnail pipeline, the fetch-from-storage part is I/O-bound and the resize part is CPU-heavy, so CPU rather than thread count becomes the limit, which is an argument for running more processes at modest concurrency rather than one process at 25. ## A worked sizing example Suppose the photo site runs two job hosts, each with two Sidekiq processes at 10 threads, and four Puma processes at 3 threads: 1. Job side: 2 hosts x 2 processes x 10 connections = 40. 2. Web side: 4 processes x 3 connections = 12. 3. Add headroom for consoles, migrations, cron-style tasks and anything else that connects, and compare the total with the database server's limit. If the total is close to the limit, scale job capacity by adding processes only after raising the server limit or putting a connection pooler in front of the database. ## A quick diagnosis checklist - Compare the thread count the Busy tab shows for the process with `ActiveRecord::Base.connection_pool.max_connections` inside the Sidekiq process. - Check `RAILS_MAX_THREADS` in the job host's environment, not just the web host's. - Watch the database's active connection count against its limit after scaling out. - Look for jobs holding transactions open around slow external calls; they pin connections for the whole call.

  • Why not just set max_connections to 100 everywhere and stop worrying?
    Because the pool is per process, so 100 multiplies by every web and job process and can exceed the database server's connection limit, which then fails new connections for everyone. Connections also cost memory on the server. Size each process's pool to its own thread count and add capacity with more processes only after checking the server-wide total.
  • The jobs process sets RAILS_MAX_THREADS=10 but config/sidekiq.yml still says :concurrency: 25. What runs?
    Twenty-five threads with a pool of ten. The file's concurrency overrides the environment variable for Sidekiq, while `database.yml` still reads the variable, so the mismatch returns. Either remove `:concurrency:` from the file and let the variable drive both, or raise the variable to match the file.

  • Saying Sidekiq threads share one database connection like a single request
  • Believing :concurrency: in sidekiq.yml also resizes the Active Record pool
  • Claiming Sidekiq's default concurrency is 25
  • Assuming more threads always speed up CPU-bound jobs on CRuby
  • Sizing the pool for one process and ignoring the database server's total limit

Assumes Rails 8.1, whose generated database.yml names the pool size max_connections (pool: is the older key) and reads RAILS_MAX_THREADS with a default of 5.