In Puma cluster mode for a Rails app, what does preload_app! change, and why can a preloaded cluster not do a phased restart?
answer
- boot once in the master, then fork
- on by default above one worker
- SIGUSR1 falls back to a hot restart
- before_fork vs before_worker_boot
- phased needs prune_bundler, not preload
basics
~20 spreload_app! boots the Rails app once in Puma's master and forks workers from it, sharing memory and speeding worker start. A phased restart re-forks workers from that master, which still holds old code, so Puma falls back to a hot restart.
solid answer
~50 sWithout preloading, each worker boots Rails itself after the fork. With `preload_app!` the **master** loads the app first and every worker starts as a fork of it: workers boot faster and share unchanged memory pages. Puma 8 turns it on by default once there is more than one worker (unless `prune_bundler` is set). The cost is that the master now holds the app's code. A **phased restart** (`SIGUSR1` or `pumactl phased-restart`) replaces workers one at a time while the master keeps running, so new workers would be forked with the old code. Puma refuses it: it logs that phased restart is unavailable and does a **hot restart** instead, re-execing the master and every worker at once. Rolling restarts onto a new release need `preload_app!` off and `prune_bundler` on. Use `before_fork` to close sockets in the master, and `before_worker_boot` to reopen clients or restart threads in each worker.
code
ruby · 11 lines# config/puma.rb additions for a preloaded cluster
preload_app! # already the default once WEB_CONCURRENCY > 1
before_fork do
# master, before forking: close sockets opened during boot
end
before_worker_boot do
# each worker, after the fork: reopen non-Active Record clients,
# restart background threads a gem started at boot
endgo deeper
Know that preload_app! loads the app once in the master before forking workers, and that phased restarts replace workers one at a time.
Explain which hook runs where, why Puma enables preloading above one worker, and why phased restart falls back to a hot restart when preloading.
Choose between preloading and phased restarts for the deploy model in use, and handle boot-time threads and sockets across the fork.
Weigh memory density against in-place rolling deploys across the platform, and decide where restart strategy belongs: Puma or the deploy layer.
## Two ways to start workers In **cluster mode** Puma runs a **master** process that forks **workers**, the processes that actually serve requests. The question is when the Rails application gets loaded: | | Without preloading | With `preload_app!` | |---|---|---| | Who boots Rails | each worker, after the fork | the master, once, before forking | | Worker start time | a full Rails boot each | a fork of an already booted app | | Memory | every worker has private copies | unchanged pages shared until written | | Master holds app code | no | yes | | Phased restart | available | falls back to a hot restart | In Puma 8 `preload_app!` is **on by default when there is more than one worker** and `prune_bundler` is off, so a Rails app that sets `WEB_CONCURRENCY=4` is already preloading. You call `preload_app!` explicitly mostly to make the choice visible, or `preload_app! false` to turn it off. (Why forked processes share pages, and why that sharing erodes, belongs to Ruby's process material; what matters here is that preloading is what makes it possible.) ## The hooks around the fork Forking copies open file descriptors, including sockets, into the child, and it does not copy background threads. Puma's cluster hooks exist for that: - **`before_fork`** runs in the master before it forks workers. Close sockets or clients opened during boot so children do not share them. - **`before_worker_boot`** runs inside each worker right after it is forked. Reopen non-Active Record clients and restart any background threads a gem started during boot. Puma 8 keeps `on_worker_boot` only as a deprecated alias that warns. - **`before_worker_fork`** runs in the master just before each individual worker is started; `before_refork` belongs to the separate `fork_worker` mode. Active Record needs no hook in current Rails. After a fork, Rails discards connection pools inherited from the parent, and each worker opens fresh connections on first use. The old advice to call `ActiveRecord::Base.establish_connection` in `before_worker_boot` is a leftover from older versions. When preloading, Puma also logs a warning listing any threads started during app boot, a hint that something needs restarting in `before_worker_boot`. ## Why preloading and phased restarts conflict Puma has three restart operations: 1. **Hot restart** (`SIGUSR2`, `pumactl restart`, or `plugin :tmp_restart` noticing `tmp/restart.txt`): the whole process re-execs. No memory survives, one app version runs at a time, and clients connecting during the gap see extra latency. 2. **Phased restart** (`SIGUSR1`, `pumactl phased-restart`): cluster mode only. The master stays up and replaces workers one by one, waiting for each new worker to boot before stopping the next old one, so capacity never drops to zero. 3. **Refork**, specific to `fork_worker` mode. A phased restart forks new workers from the master that is still running. With preloading, that master holds the old release, so new workers would come up on old code. Puma's cluster therefore declines the request, logs `phased-restart called but not available, restarting normally`, and performs a hot restart. The boot banner says as much: with preloading it lists hot restarts as available and phased as not. To get real rolling restarts you give up preloading: - turn `preload_app!` off so each worker loads the app after its own fork, - enable `prune_bundler` so workers can pick up a new Gemfile.lock, - run more than one worker, so capacity stays up while each is replaced, - accept that gems loaded by the master, Puma itself included, cannot change this way. ## What preloading does to memory over time The sharing is real but not permanent. Pages stay shared only until a worker writes to them, and Ruby's garbage collector and normal request work write to many pages. So the saving is largest right after boot and shrinks as workers age. That is still why preloaded clusters fit more workers on a box than non-preloaded ones, and why worker memory is measured after warm-up, not at boot. ## Choosing in practice The trade is memory and boot speed against in-place rolling deploys. Container deploys usually start a new container and switch traffic to it (how the deploy tool does that is its own topic), so they never ask the old Puma for a phased restart and can keep preloading. Long-lived servers that restart Puma in place are where phased restarts, and therefore no preloading, earn their cost.
- Your team needs rolling in-place restarts on long-lived servers; what do you change in config/puma.rb, and what do you give up?Turn preloading off with `preload_app! false` and enable `prune_bundler`, keeping at least two workers. Each worker then boots Rails itself, so memory sharing between workers is lost and every worker boot takes a full app load. Gems the master loads, Puma included, still need a hot restart to change.
- Puma logs a warning about threads started during app boot in a preloaded cluster; why does that matter?Forking copies memory but not threads, so a thread started in the master (a metrics flusher, a client's monitor) simply does not exist in the workers. Its work silently stops there. Restart such threads in `before_worker_boot`, or configure the gem to start them lazily after fork.
- preload_app! makes each worker load the app after it has been forked.
- You must call ActiveRecord::Base.establish_connection in before_worker_boot or workers share sockets.
- SIGUSR1 to a preloaded cluster rolls workers onto the new release one at a time.
- before_fork runs inside each worker right after it is forked.
- on_worker_boot is the current hook name in Puma 8.
Assumes Puma 8.0 under Rails 8.1: preload_app! defaults on above one worker, on_worker_boot is a deprecated alias of before_worker_boot, and Rails discards inherited connection pools after fork.