123456789_123456789_123456789_123456789_123456789_

Fork-Worker Cluster Mode [Experimental]

fork_worker (--fork-worker from the CLI) is an experimental cluster-mode configuration option. This mode causes Puma to fork additional workers from worker 0, instead of directly from the master process.

10000   \_ puma 8.0 (tcp://0.0:9292) [puma]
10001       \_ puma: cluster worker 0: 10000 [puma]
10002           \_ puma: cluster worker 1: 10000 [puma]
10003           \_ puma: cluster worker 2: 10000 [puma]
10004           \_ puma: cluster worker 3: 10000 [puma]

The fork_worker option allows your application to be initialized only once for copy-on-write memory savings, and it has two additional advantages:

  1. Compatible with phased restart. With preload_app! disabled, the master process doesn't load the application, so this mode works with phased restart (SIGUSR1 or pumactl phased-restart). When worker 0 reloads as part of a phased restart, it initializes a new copy of your application first, then the other workers reload by forking from this new worker already containing the new preloaded application.

    This allows a phased restart to complete as quickly as a hot restart (SIGUSR2 or pumactl restart), while still minimizing downtime by staggering the restart across cluster workers.

  2. 'Refork' for additional copy-on-write improvements in running applications. Fork-worker mode introduces a new refork command that re-loads all nonzero workers by re-forking them from worker 0.

    This command can potentially improve memory utilization in large or complex applications that don't fully pre-initialize on startup, because the re-forked workers can share copy-on-write memory with a worker that has been running for a while and serving requests.

    You can trigger a refork by sending the cluster the SIGURG signal or running the pumactl refork command at any time. A refork will also automatically trigger once, after a certain number of requests have been processed by worker 0 (default 1000). To configure the number of requests before the auto-refork, pass a positive integer argument to fork_worker (e.g., fork_worker 1000), or 0 to disable.

Usage Considerations

Limitations

Mold-Worker Cluster Mode [Experimental]

mold_worker (--mold-worker from the CLI) is an alternative to fork_worker. Instead of forking from a worker that also serves requests, Puma promotes a worker to a "mold", which stops serving requests and only forks new workers. Because the mold never serves traffic, a slow request, timeout or memory limit can't take it down mid-refork.

10000   \_ puma 8.0 (tcp://0.0:9292) [puma]
10005       \_ puma: mold 2: 10000 [puma]
10011           \_ puma: cluster worker 0: 10000 [puma]
10012           \_ puma: cluster worker 1: 10000 [puma]
10013           \_ puma: cluster worker 2: 10000 [puma]

Workers are forked from the master at boot. Each request threshold (default 1000) promotes the busiest worker to a new mold, retires the previous one, and reforks every worker from the new mold.

mold_worker 1000, 2000, 4000

Usage Considerations