In a Rails 8.1 app, what do Bootsnap's load-path and compile caches save at boot, and why does the generated Dockerfile run bootsnap precompile?
answer
- required from config/boot.rb
- no more $LOAD_PATH probing
- ISeq bytecode and parsed YAML cached
- tmp/cache, never cleaned up
- precompile --gemfile, then app/ lib/
basics
~20 sBootsnap caches where each required file lives on $LOAD_PATH and the compiled bytecode of Ruby files, plus parsed YAML. The Dockerfile runs bootsnap precompile so those caches ship inside the image and containers skip compiling at boot.
solid answer
~40 s`config/boot.rb` requires `bootsnap/setup` right after `bundler/setup`, which calls `Bootsnap.default_setup`. Two caches follow. The **load-path cache** makes `require` and `load` resolve against cached listings of each `$LOAD_PATH` directory instead of probing every directory in turn; it even answers misses with a `LoadError` without touching the disk. The **compile cache** stores the ISeq bytecode of each Ruby file and YAML parsed into MessagePack, keyed on the Ruby version and the source file. Both live in `tmp/cache`. In development the caches warm up on first boot. In a container every boot would be a first boot, so the generated Dockerfile runs `bundle exec bootsnap precompile -j 1 --gemfile` after `bundle install` and `bundle exec bootsnap precompile -j 1 app/ lib/` after copying the code, baking both into image layers.
code
dockerfile · 10 lines# Build stage of the Dockerfile Rails 8.1 generates (excerpt)
RUN bundle install && \
rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
# -j 1 disable parallel compilation to avoid a QEMU bug
bundle exec bootsnap precompile -j 1 --gemfile
COPY . .
# Precompile bootsnap code for faster boot times.
RUN bundle exec bootsnap precompile -j 1 app/ lib/go deeper
Recall that Bootsnap speeds up boot, is required in config/boot.rb, and keeps its caches in tmp/cache.
Explain both caches, what each replaces, how stale entries are detected, and why the Dockerfile precompiles gems and app code at build time.
Diagnose slow boots with Bootsnap's stats, spot cache growth and unwritable cache directories, and know when the cache cannot help.
Treat boot time as a deploy and autoscaling budget: precompiled images, cache placement and how fast a new instance can take traffic.
## The problem Bootsnap attacks Booting a large Rails app means requiring thousands of Ruby files and parsing a lot of YAML. Two costs dominate: - **Finding files.** `require "foo"` makes Ruby try `foo.rb` in every directory on `$LOAD_PATH` until one exists. With hundreds of gem directories, that is thousands of failed filesystem lookups, and a require that misses entirely checks every entry before raising `LoadError`. - **Compiling files.** Each Ruby file is parsed and compiled to an instruction sequence (**ISeq**, Ruby's bytecode) on every boot. YAML files are parsed from text every time. In the slow-booting legacy marketplace app, a plain `bin/rails runner 'nil'` can take many seconds. That hurts deploys, console sessions, test runs and every container that restarts. ## How Rails wires it in A new app gets `gem "bootsnap", require: false` in the Gemfile and this in `config/boot.rb`: ```ruby require "bundler/setup" # Set up gems listed in the Gemfile. require "bootsnap/setup" # Speed up boot time by caching expensive operations. ``` `bootsnap/setup` calls `Bootsnap.default_setup`, which derives the cache directory (`tmp/cache`, or `BOOTSNAP_CACHE_DIR`), enables every cache unless a `DISABLE_BOOTSNAP_*` variable says otherwise, and sets development mode from `RAILS_ENV`. Loading it this early is the point: anything required before it gets no benefit. ## The two caches | Cache | What it replaces | What it stores | |---|---|---| | **Load-path cache** | probing each `$LOAD_PATH` entry on `require` / `load` | the requirable files in each path entry, plus remembered misses | | **Compile cache** | parsing and compiling on load | ISeq bytecode for `.rb` files; YAML as MessagePack (or Marshal) | The load-path cache sorts path entries into two kinds: 1. **Stable** entries, under the Ruby install prefix, `Gem.path` or the Bundler path, are scanned once and assumed never to change. 2. **Volatile** entries, everything else including the app's own directories, are rescanned on each boot and trusted for only 30 seconds. Bootsnap's README still lists a `JSON.load_file` cache, but the code removed it in 1.19 (the `json` gem had become as fast as MessagePack); in 1.26 the `compile_cache_json` option only warns that it has no effect. The compile cache writes one file per source file with a header recording a digest of the Ruby version and compile options, plus the source's size, mtime and content digest. A mismatch makes Bootsnap recompile and overwrite that entry, so upgrading Ruby or editing a file is safe. ## Why the Dockerfile precompiles In development the cache fills itself on the first boot and later boots reuse it. A container starts from its image every time, so without precompilation every new container pays the full compile cost again. The generated Dockerfile therefore runs two commands in its build stage: - `bundle exec bootsnap precompile -j 1 --gemfile` right after `bundle install`, compiling the gems in the Gemfile into the gems layer. - `bundle exec bootsnap precompile -j 1 app/ lib/` after copying the application, compiling the app's own code. `-j` sets the number of parallel compile workers. The template pins it to 1 because parallel compilation can hang under QEMU during cross-architecture image builds. The results sit in `tmp/cache` inside the image, so a fresh container boots warm. ## Operating traps - **Bootsnap never deletes cache entries.** Long-lived servers that deploy in place can accumulate stale files; Bootsnap's own docs blame progressively slower deploys on that and suggest purging `tmp/cache/bootsnap*`. - **The cache directory must be writable**, or boot fails. `BOOTSNAP_READONLY` makes it use the cache without writing to it. - **Network filesystems defeat it.** A cache on a slow mount can make boot slower than no cache. - **The ISeq cache can interfere with coverage reporting**, as Bootsnap's README warns, so check coverage numbers after enabling it in a test run. ## Measuring the effect For the marketplace, measure before and after rather than trusting the general claim: - Time a no-op boot, such as `bin/rails runner 'nil'`, with `DISABLE_BOOTSNAP=1` and without it. The difference is what Bootsnap buys this app. - Boot with `BOOTSNAP_STATS=1`. At exit Bootsnap prints counts of `hit`, `miss`, `stale` and `revalidated` cache events. A fresh container should show mostly hits; mostly misses mean the image skipped `bootsnap precompile` or uses a different cache directory. - `BOOTSNAP_LOG=1` prints each event with its path, which helps find the directories that keep missing. Bootsnap trims boot time. It does nothing for request speed once the app is up.
- The legacy app still runs an old config.load_defaults; how can that make Bootsnap's load-path cache work harder?Before `load_defaults 7.1`, `config.add_autoload_paths_to_load_path` is true, so every autoload directory such as `app/models` is also on `$LOAD_PATH`. Those are volatile entries, rescanned on each boot and indexed in memory. Zeitwerk loads app code by absolute path and does not need them there, so setting it to false trims both the index and the probing.
- How would you check whether Bootsnap is actually hitting its cache in a container?Boot once with `BOOTSNAP_STATS=1` or `BOOTSNAP_LOG=1`; Bootsnap then reports cache hits, misses and stale entries. Many misses on a fresh container mean the image was built without `bootsnap precompile`, or the cache directory differs from the one precompiled. Setting `DISABLE_BOOTSNAP=1` once gives the cold baseline for comparison.
- Bootsnap makes requests faster by caching rendered views and query results.
- bootsnap precompile is the same step as bin/rails assets:precompile.
- Bootsnap prunes old cache files on its own, so tmp/cache never grows.
- Requiring bootsnap/setup at the end of config/application.rb gives the full speedup.
- Upgrading Ruby without clearing tmp/cache makes Bootsnap load bytecode from the old version.
Assumes Rails 8.1's generated Gemfile, boot.rb and Dockerfile with Bootsnap 1.26 (Rails' own lockfile resolves 1.24); Bootsnap removed its JSON parse cache in 1.19.