skip to content

For a large legacy Rails marketplace's app-server fleet, how would you set a boot-time and memory budget, and which levers would you trade against each other?

level: principalimportance: nice to knowfreq 18%

answer

  1. boot time is a deploy and scaling cost
  2. RSS per warmed worker x workers
  3. Bootsnap precompile and load path size
  4. YJIT memory vs speed
  5. host shape changes the per-core math

basics

~20 s

Set two budgets: time from container start to serving, and resident memory per warmed worker times workers per host. Bootsnap and slimmer load paths cut boot; preloading, jemalloc, YJIT limits and host size move memory; trade them against latency.

solid answer

~50 s

I would first make both budgets measurable. **Boot budget**: seconds from process start to the first successful health check, because it bounds deploy duration and how fast autoscaling adds capacity. **Memory budget**: resident memory of a warmed worker times workers per host, plus the master, with headroom, against RAM per core. Then the levers. For boot: precompiled Bootsnap caches in the image, a smaller `$LOAD_PATH` through current `load_defaults`, and fewer gems. For memory: `preload_app!` sharing, jemalloc, a YJIT `mem_size` cap or YJIT off, fewer threads, and larger hosts that amortize the master. They trade against each other: YJIT costs memory to buy latency, preloading saves memory but rules out phased restarts, and bigger hosts save memory but scale in bigger steps. I would set the targets from the business goal, change one lever at a time on canary hosts, and record the result as the fleet's standing sizing rule.

go deeper

for a junior

Know that boot time and memory per worker are both production costs, and name one tool for each: Bootsnap and jemalloc.

for a middle

Explain how each lever moves boot time or memory, and why memory must be measured on warmed-up workers.

for a senior

Run canary comparisons of one lever at a time and catch boot or memory regressions per release.

for a principal

Own the targets: tie boot and memory budgets to cost and latency goals, choose host shapes, and make the trade-offs explicit and recorded.

## Why this is a budgeting problem In a large legacy marketplace, slow boots and creeping memory are not two bugs. They are two costs that every capacity decision pays: - **Boot time** bounds how long a deploy takes, how quickly a crashed instance is replaced and how fast autoscaling adds capacity during a sale. A 90-second boot means 90 seconds of under-capacity on every scale-up. - **Memory per worker** decides how many workers fit on a host, which decides whether you can use the cores you pay for. There is no single right answer. The job is to pick targets, make them visible and choose which trade-offs the organization accepts. ## Step 1: define budgets you can measure 1. **Boot budget**: time from process start to the first passing health check, measured in production images. Track it per release so regressions show up the day a gem lands. 2. **Per-worker memory**: resident memory of a worker after warm-up under real traffic, not at boot, because copy-on-write sharing erodes and caches fill. 3. **Per-host memory**: master plus workers times per-worker memory plus headroom, compared with the RAM per core of the host type. 4. **Latency and throughput targets** that the budgets must not break, typically p95 latency at peak and requests per second per core. ## Step 2: the levers and what each costs | Lever | Moves | Costs or risks | |---|---|---| | `bootsnap precompile` in the image | boot time down | build time; cache dir must be writable | | current `load_defaults` (autoload paths off `$LOAD_PATH`) | boot time and Bootsnap memory down | an upgrade project in a legacy app | | pruning gems | boot and memory down | engineering time, risk | | `preload_app!` | memory per worker down, worker boot faster | no phased restarts | | jemalloc via `LD_PRELOAD` | fragmentation down | must be verified on non-Docker hosts | | `config.yjit` and `mem_size` | latency down | memory per worker up | | fewer threads per worker | latency down, some memory down | throughput per worker down | | larger hosts | memory per core down | bigger scaling steps, larger blast radius | ## Step 3: make the trade-offs explicit - **YJIT vs memory.** YJIT usually buys real latency, but each worker compiles its own code. Under a tight per-worker budget, cap `mem_size` before turning YJIT off, and decide with measurements rather than defaults. - **Preloading vs restart strategy.** Preloading saves memory and speeds worker start, but forbids Puma's phased restarts. If deploys replace containers, that cost is zero; if servers restart in place, it is real. - **Host size vs elasticity.** The Rails guide's example: 2 servers of 32 processes use less memory per core than 16 servers of 4. But big hosts scale in big steps and lose more capacity when one fails. - **Threads vs processes.** Fewer threads with more processes buys latency with memory; more threads buys throughput with latency. ## Step 4: run it as a process - Change one lever at a time on canary hosts and compare boot time, RSS curves and p95 latency against the control. - Put the budgets in the release checklist: a pull request that adds a heavy gem should show its boot and memory delta. - Re-evaluate when the workload shifts, such as slow API calls moving into jobs or a new Ruby or Rails version changing defaults (current Rails leaves YJIT off in development and test, so CI numbers say little about production with YJIT). - Write down the decision and its reason, so the next team does not re-litigate it from scratch. ## What not to do - Do not budget from boot-time memory; it understates steady state. - Do not fix a memory overrun by restarting workers on a timer before checking the allocator and real leaks. - Do not adopt someone else's worker and thread numbers; the guide itself says no configuration is best for every app. ## A defensible starting position For the marketplace I would ship jemalloc and precompiled Bootsnap everywhere, keep preloading because deploys replace containers, enable YJIT with a `mem_size` cap sized from canary data, and size hosts so one worker per core fits with headroom after warm-up. Then I would spend legacy-cleanup effort where the boot profile says the time goes.

  • Finance wants smaller, cheaper instances while product wants faster checkout; how do you frame the decision?
    Show both on the same chart: cost per request and p95 latency for each host shape and worker or thread layout, from canary data. Smaller hosts lose copy-on-write and master amortization and may not fit one worker per core, so the saving can be smaller than the price tag suggests. Then let the business choose the point on that curve explicitly.
  • Boot time doubled after a dependency upgrade; how would your budget catch and explain it?
    Because boot time is tracked per release, the regression is pinned to that deploy. Booting with `BOOTSNAP_STATS=1` shows whether caches still hit, and comparing the gem list with the previous release finds the new or heavier dependency. The fix is then a decision, such as replacing the gem or loading it lazily, not a surprise found during a traffic spike.

  • Memory per worker measured right after boot is the number to budget with.
  • Turning YJIT off is always the right first move when memory is tight.
  • Boot time only matters to developers, not to production capacity.
  • The same defaults are optimal for every Rails app, so no measurement is needed.
  • Smaller hosts always cost less per request because instances are cheaper.