skip to content

In a Rails 8.1 app, what does plugin :solid_queue in config/puma.rb do when SOLID_QUEUE_IN_PUMA is set, and when should jobs move to a separate bin/jobs process?

level: seniorimportance: should knowfreq 35%

answer

  1. one container runs web and jobs
  2. supervisor forked after Puma boots
  3. each kills the other if it dies
  4. no phased restarts
  5. split out when servers multiply

basics

~20 s

With SOLID_QUEUE_IN_PUMA set, Puma loads the plugin, which forks the Solid Queue supervisor after boot and stops it with Puma, so one container serves web and jobs. Move jobs to bin/jobs once you add servers or jobs compete with requests.

solid answer

~50 s

The generated `config/puma.rb` contains `plugin :solid_queue if ENV["SOLID_QUEUE_IN_PUMA"]`, and the generated Kamal `deploy.yml` sets that variable, so a default single-server deploy runs jobs in the web container. After Puma boots, the plugin forks a child that starts the Solid Queue supervisor in `fork` mode, which forks the workers and dispatchers; Puma stops it on shutdown or restart. Each side watches the other: if the supervisor dies Puma shuts down, and if Puma goes away the supervisor stops. `solid_queue_mode :async` instead runs everything as threads inside Puma and ignores `processes`, and phased restarts are unsupported because the plugin needs preloading. It suits one server with light jobs. Move jobs to `bin/jobs` on their own process, container or server once you add web servers, when heavy jobs steal CPU and memory from requests, or when web and jobs must scale or restart independently.

go deeper

for a junior

Know that the generated puma.rb can run Solid Queue inside Puma when SOLID_QUEUE_IN_PUMA is set, and that bin/jobs is the standalone alternative.

for a middle

Explain fork versus async plugin modes, the mutual monitoring between Puma and the supervisor, and why phased restarts are unsupported.

for a senior

Decide when to split jobs out of the web container, and plan memory, restarts and scaling for each process type.

for a principal

Weigh operational simplicity against isolation for the whole deployment, and set the point at which job processing gets its own capacity.

## What the generated files wire together A Rails 8.1 app ships three pieces that, together, run jobs inside the web server: ```ruby # config/puma.rb (generated) threads_count = ENV.fetch("RAILS_MAX_THREADS", 3) threads threads_count, threads_count plugin :solid_queue if ENV["SOLID_QUEUE_IN_PUMA"] ``` - The **Puma plugin** ships with the `solid_queue` gem. - The **condition** keeps the plugin off in development, where Solid Queue is usually not configured; loading it there would boot a supervisor that fails. - The generated Kamal **`deploy.yml`** sets `SOLID_QUEUE_IN_PUMA: true`, with a comment saying to split job processing out to a dedicated machine once you use multiple servers. It also carries a commented-out `job` role running `bin/jobs`. So the default single-server deploy of a newsletter platform runs Puma, and inside the same container, the Solid Queue supervisor with its workers and dispatcher. ## What the plugin does at runtime In its default **fork** mode: 1. Once Puma has booted, the plugin **forks** a child process, which starts the Solid Queue supervisor in `fork` mode; the supervisor in turn forks the workers, the dispatcher and the scheduler from `queue.yml`. 2. A background thread in Puma checks every couple of seconds whether that child is still alive. If it has died, Puma **stops itself**. 3. The child checks whether Puma is still its parent. If Puma has gone away, the supervisor **stops too**. 4. When Puma stops or restarts, the plugin sends `INT` to the supervisor and waits for it. The mutual kill switch is deliberate: a container whose web server is up but whose job runner is silently dead would look healthy while digests never go out. Crashing both lets the orchestrator restart the pair. With `solid_queue_mode :async` in `puma.rb`, the supervisor and all its workers run as **threads inside the Puma process**, `processes` is ignored, and the job threads compete directly with request threads. The project recommends `fork`. **Phased restarts are not supported**, because the plugin needs the application preloaded and Puma's phased restarts do not work with preloading. ## Trade-offs | | Puma plugin | `bin/jobs` separately | |---|---|---| | Moving parts | one container, one command | a second process or role to deploy | | Resource isolation | jobs share CPU and memory with requests | separately sized and limited | | Scaling | every web server also runs jobs | web and job capacity scale independently | | Restarts | jobs restart with every web restart | jobs restart on their own schedule | | Failure blast radius | a job that exhausts memory takes the web server down | contained to the job process | ## When to move to `bin/jobs` - **More than one web server.** Every server would start its own supervisor and job processes. Recurring tasks are protected from double enqueueing by a unique index, but job capacity now grows with web capacity whether you want it or not. - **Heavy or long jobs.** Rendering large digests or importing subscriber lists competes with request latency and memory. - **Independent scaling or deploys.** Backlogs that need ten worker processes should not require ten web servers. Moving is mostly configuration: stop setting `SOLID_QUEUE_IN_PUMA` and run `bin/jobs` in its own process, container or server. ## In development Development falls back to `:async`, so neither the plugin nor `bin/jobs` is needed to try jobs locally. To exercise Solid Queue itself, configure it for development (adapter, `connects_to` and a `queue` database) and then either start Puma with the variable set, change the guard to `ENV["SOLID_QUEUE_IN_PUMA"] || Rails.env.development?` as the Solid Queue README suggests, or run `bin/jobs` in a second terminal. In the last case, point `config.solid_queue.logger` at standard output, because the default logger writes to `log/development.log`. ## How `bin/jobs` shuts down `bin/jobs` and the plugin's forked supervisor respond to the same signals: - `TERM` or `INT` starts a graceful shutdown: workers stop claiming and the supervisor waits up to `SolidQueue.shutdown_timeout` (5 seconds by default) before sending `QUIT`. - `QUIT` exits immediately; in-flight jobs are returned to the queue as the processes deregister. - A worker killed without warning cannot release its claimed jobs. The supervisor that sees the fork exit, or another process once the dead one's heartbeat has expired (5 minutes by default), records those jobs as failed instead of re-running them.

  • A memory-hungry digest job keeps getting the web container killed; why, and what changes?
    With the plugin in fork mode, the job processes are children inside the web container, so they share its memory limit; when the container is killed, Puma and Solid Queue die together. Moving jobs to `bin/jobs` in their own container or server lets you size its memory separately, and a job that blows its limit no longer takes requests down with it.
  • Why does the generated puma.rb guard the plugin with SOLID_QUEUE_IN_PUMA instead of always loading it?
    Development and test do not use Solid Queue by default; development falls back to `:async`. Loading the plugin unconditionally would start a supervisor in every `bin/rails server` that has no queue database configured. The environment variable makes running jobs in Puma an explicit deployment choice, which the generated Kamal `deploy.yml` makes for production.

  • The Solid Queue plugin runs jobs on Puma's request threads by default
  • If the Solid Queue supervisor crashes, Puma keeps serving and quietly restarts it
  • The generated puma.rb loads the Solid Queue plugin in every environment
  • Running the plugin on every web server is the recommended way to scale jobs
  • Phased restarts work fine with the Solid Queue Puma plugin