A ticket-resale Rails 8.1 app on a 4-core server, running the generated config/puma.rb, keeps one core busy; why, and what do you change?
answer
- single mode is one process
- one process, one core of Ruby
- WEB_CONCURRENCY=4 or auto
- auto honours the CPU quota
- WEB_CONCURRENCY=1 still forks a worker
basics
~20 sWith WEB_CONCURRENCY unset, Puma runs a single process, and its 3 threads take turns running Ruby under the GVL, so roughly one core works. Setting WEB_CONCURRENCY=4 (or auto) forks four workers, each with its own threads.
solid answer
~40 sThe generated `config/puma.rb` has no `workers` line, and Puma's own default is 0 workers, so the app runs in **single mode**: one Ruby process with 3 threads. On CRuby those threads overlap only while waiting on I/O. Ruby code runs on one thread at a time per process, so one process tops out near one core. To use all four, set `WEB_CONCURRENCY=4` (or `auto`) in the deploy environment. Puma then starts in **cluster mode**: a master forks four workers, each with its own 3 threads, for 12 requests in flight. The costs: about four times the memory, minus what preloading shares; four separate connection pools; and `auto` counts what `Concurrent.available_processor_count` reports, which honours a container's CPU quota and rounds down.
code
bash · 5 lines# 4 workers x 3 threads = up to 12 requests in flight on a 4-core box
WEB_CONCURRENCY=4 RAILS_MAX_THREADS=3 bin/rails server
# or let Puma ask concurrent-ruby how many processors are available
WEB_CONCURRENCY=auto bin/rails servergo deeper
Remember that one Puma process uses about one core for Ruby, and that WEB_CONCURRENCY sets how many worker processes Puma forks.
Explain single versus cluster mode, why threads alone cannot use more cores on CRuby, and what WEB_CONCURRENCY unset, 1, a number and auto each produce.
Show the operational side: CPU quotas and auto rounding down, memory and connection totals per server, and verifying the mode from Puma's boot log.
Frame worker counts as a host-shape decision: whether the fleet runs fewer large boxes or many small containers, and what that does to memory and connection budgets.
## The symptom The ticket-resale site goes on sale, the load test drives traffic, and `top` on the 4-core box shows one Ruby process pinned near 100% CPU while three cores idle. Response times climb, yet the server looks mostly unused. Nothing is broken. The app is running exactly what Rails generated. ## Why one process means about one core - **Single mode is the default.** Rails 8.1's `config/puma.rb` sets threads but never calls `workers`. Puma's own `workers` default is 0, and `WEB_CONCURRENCY` is unset, so Puma logs `Puma starting in single mode...` and serves everything from one process. - **Threads in one CRuby process do not run Ruby in parallel.** The GVL lets one thread execute Ruby code at a time. The 3 threads still help: while one waits on the database or an HTTP call, another runs. But the process as a whole can use about one core for Ruby work. (How the GVL works belongs to Ruby's own concurrency material; here it is just the reason the ceiling exists.) - **More threads do not fix it.** Raising `RAILS_MAX_THREADS` to 12 mostly adds threads waiting for the same lock. An I/O-heavy app may gain a little throughput, but latency suffers and the process still cannot get past about one core. ## The fix: cluster mode through WEB_CONCURRENCY Puma reads `WEB_CONCURRENCY` itself, so no code change is needed: 1. Set `WEB_CONCURRENCY=4` in the server's environment (your deploy tool's env settings, a systemd unit, the container's env). 2. Puma now boots in **cluster mode**. A **master** process loads the configuration and forks four **workers**. Each worker is a full Rails process with its own 3-thread pool, so up to 12 requests run concurrently and up to four of them execute Ruby at the same instant. 3. With more than one worker, Puma also turns **`preload_app!`** on by default. The master boots the app once and the workers start as forks of it, sharing unchanged memory pages. | `WEB_CONCURRENCY` | Mode | Processes serving requests | |---|---|---| | unset or empty | single | 1 | | `1` | cluster, with a warning | 1 worker plus an idle master | | `4` | cluster | 4 workers plus the master | | `auto` | cluster | `Concurrent.available_processor_count`, rounded down | ## Traps in the settings - **`WEB_CONCURRENCY=1` is not the same as unset.** It starts cluster mode with a single worker and prints `Running Puma in cluster mode with a single worker is often a misconfiguration`. You pay for a master process and gain nothing. The Rails template's comment says to set the variable only for 2 or more workers. - **`auto` asks concurrent-ruby, not the hardware.** `Concurrent.available_processor_count` honours a cgroup CPU quota, so a container capped at 1.5 CPUs on a 64-core host gets 1 worker. A quota under one CPU rounds down to 0 and silently leaves Puma in single mode, where cluster-only hooks never run. Shared or misreported vCPUs can also mislead it; set an explicit number when in doubt. - **Memory scales with workers.** Each worker holds its own Ruby heap. Preloading shares pages until they are written, but four workers still cost far more than one process with more threads. Check that the box has the RAM before raising the count. - **Database connections scale with workers.** Each worker has its own Active Record pool, so four workers with pools of 3 can open 12 connections from this server alone, plus any job processes. ## How many workers, and how many threads For this box, one worker per core is the usual starting point. Whether to lean toward throughput (more threads per worker) or latency (fewer threads, possibly more than one process per core) is a sizing decision to make from load tests. Puma's settings only carry out that choice. Then confirm in the boot log that Puma says `cluster mode` and lists `Workers: 4`, and watch per-core CPU under load. ## Checking the result - The boot banner now prints a `Master PID` instead of a plain `PID`, and one `Worker N (PID: ...) booted` line per worker. - `ps` shows the master and four children; each child's resident memory is what you budget for. - Under the same load test, all four cores should now carry Ruby work and tail latency should fall. - If one core is still hot, check that the environment variable actually reaches the Puma process, for example that it is set for the container rather than only in a shell profile.
- The same app runs in a container capped at 0.5 CPU with WEB_CONCURRENCY=auto, and a before_worker_boot hook never fires; why?`auto` uses `Concurrent.available_processor_count`, which honours the cgroup quota and returns 0.5. Puma converts that to an integer, 0, and 0 workers means single mode. In single mode no worker is ever forked, so cluster-only hooks such as `before_worker_boot` never run. Set an explicit worker count if the hook matters, or accept single mode on a sub-core container.
- After switching to four workers, what else on the server has to grow?Memory, because each worker has its own heap even with preloading; and database connections, because each worker has its own Active Record pool, so total connections become workers times pool size. If Solid Queue also runs on the box, its processes need connections too. Check the database's connection limit before the change, not after the errors.
- Raising RAILS_MAX_THREADS to 12 will put all four cores to work.
- WEB_CONCURRENCY=1 behaves exactly like leaving the variable unset.
- WEB_CONCURRENCY=auto always counts every core the host machine has.
- Cluster mode needs preload_app! in puma.rb before WEB_CONCURRENCY has any effect.
- All Puma workers on a server share one Active Record connection pool.
Assumes Rails 8.1's generated config/puma.rb, which has no workers line, on Puma 8.0, which reads WEB_CONCURRENCY including the value auto.