skip to content

Store Backends & Defaults

config.cache_store decides where Rails.cache lives: Solid Cache in a database for new production apps, or memory, files, Redis or Memcached. Interviewers ask what each store costs and how it evicts.

part ofRuby on Railsread it →
on this pageshow

explore

questions

6

In a new Rails 8.1 app, which store does config.cache_store select in development, test and production, and why does each environment differ?

level: juniorimportance: must knowfreq 62%

answer

  1. one line per environment file
  2. development: in-process and bounded
  3. test: stores nothing at all
  4. production: database-backed since 8.0
  5. unset falls back to tmp/cache/

basics

~20 s

A new Rails 8.1 app sets config.cache_store to :memory_store in development, :null_store in test and :solid_cache_store in production; Rails builds Rails.cache from that value at boot, and an app that sets nothing falls back to :file_store in tmp/cache/.

solid answer

~40 s

`config.cache_store` in each `config/environments/*.rb` names the backend behind `Rails.cache` and fragment caching; Rails builds it once at boot with `ActiveSupport::Cache.lookup_store`. The generator writes `:memory_store` in development (fast, per process, 32 MB by default), `:null_store` in test so no test reads a value another one left behind, and, since Rails 8.0, `:solid_cache_store` in production: the `solid_cache:install` step that `rails new` runs rewrites the commented line, adds `config/cache.yml` and points the store at a separate `cache` database. With `--skip-solid` that line stays commented out, so the framework fallback applies: `[:file_store, "#{root}/tmp/cache/"]`. `:redis_cache_store` and `:mem_cache_store` remain options, but they need the `redis` or `dalli` gem in the Gemfile.

code

ruby · 11 lines
ruby
# config/environments/development.rb
config.cache_store = :memory_store

# config/environments/test.rb
config.cache_store = :null_store

# config/environments/production.rb (after solid_cache:install)
config.cache_store = :solid_cache_store

# Passing constructor options
config.cache_store = :memory_store, { size: 64.megabytes }

go deeper

for a junior

Name the three generated stores, one per environment, and say that Rails.cache is built from config.cache_store at boot. Mentioning the tmp/cache file-store fallback shows you know what happens when nothing is set.

for a middle

Explain why test uses :null_store, why development's memory store is per process, and that solid_cache:install, not load_defaults, writes the production line along with config/cache.yml.

for a senior

Talk about what an upgraded app really runs: often the silent file-store fallback. Show you would choose a shared store on purpose and add the client gem and pool settings it needs.

for a principal

Frame the default as a trade between operating one more service and keeping the cache in a database you already run, and say what evidence about latency or size would justify moving to Redis or Memcached.

## What `config.cache_store` decides **`config.cache_store`** is the application setting that chooses where **`Rails.cache`** keeps its entries. During boot, Rails' `initialize_cache` initializer passes the value to **`ActiveSupport::Cache.lookup_store`**, which turns a symbol such as `:memory_store` into an instance of `ActiveSupport::Cache::MemoryStore` and hands any further arguments to its constructor. By default everything that caches through Rails uses that one object, because Action Controller's own `cache_store` falls back to `Rails.cache`: low-level `Rails.cache.fetch` calls, the view `cache` helper, and anything a gem stores through `Rails.cache`. The value can be: - a **symbol**, such as `:null_store`; - a **symbol followed by arguments**, such as `:memory_store, { size: 64.megabytes }` or `:file_store, "tmp/cache"`; - an **already-built store object**, which Rails uses as it is. ## The generated defaults A plain `rails new` in Rails 8.1 writes one line per environment file: | Environment | Generated line | What it means | |---|---|---| | development | `config.cache_store = :memory_store` | a hash inside each Ruby process, bounded at 32 MB by default and pruned least-recently-used first | | test | `config.cache_store = :null_store` | writes are accepted and thrown away, reads always miss | | production | `config.cache_store = :solid_cache_store` | rows in a database table through the Solid Cache gem | | none set | framework default | `[:file_store, "#{root}/tmp/cache/"]`, files on the local disk | The production line is not in the raw template. The template carries a commented `# config.cache_store = :mem_cache_store`; the **`solid_cache:install`** generator, which `rails new` runs together with `solid_queue:install` unless you pass `--skip-solid`, replaces it with `config.cache_store = :solid_cache_store`, creates `config/cache.yml` and writes `db/cache_schema.rb`. The generated `config/cache.yml` sends production to the database named `cache` in `database.yml`, sets `max_size` to 256 MB and namespaces keys by `Rails.env`. ## Why each environment differs 1. **Development** wants something fast that needs no service. `:memory_store` lives in the server process, so restarting the server empties it, which is what you want while editing code. Whether views use it is a separate switch, toggled with `bin/rails dev:cache`. 2. **Test** wants isolation. A cached value written by one test would leak into the next, so `:null_store` keeps nothing between calls and every test exercises the uncached path. A test that needs real caching behaviour swaps the store for that test. 3. **Production** wants a cache that is **shared** by every server process and **survives deploys**. Since Rails 8.0 the default answer is Solid Cache, which stores entries in a database you already run, so the cache can be far larger than RAM allows and keeps living through restarts, with no Redis or Memcached server to operate. ## The fallback and `--skip-solid` If an environment never assigns `config.cache_store`, Rails uses the default from `Rails::Application::Configuration`: `:file_store` in `tmp/cache/`. That is what a production app generated with `--skip-solid` (or `--skip-active-record`, which implies it) runs on until someone picks a store. It works on one host, but it is not shared between servers and it has no size limit, so it is rarely the right production choice. Note the trap: `ActiveSupport::Cache.lookup_store(nil)` returns a `MemoryStore`, but Rails never passes `nil`, because the configuration default is the file store. ## Choosing something else The other built-in stores are `:redis_cache_store` and `:mem_cache_store`. Their store files `require` a client gem at load time: `RedisCacheStore` needs the `redis` gem (4.0.1 or later) and `MemCacheStore` needs `dalli`. Both build a connection pool for threaded servers such as Puma. Extra arguments are passed straight to the store's constructor, and options in that hash become defaults for every call on the store: - `config.cache_store = :memory_store, { size: 64.megabytes }` raises the in-process limit; - `config.cache_store = :redis_cache_store, { url: ENV["REDIS_URL"] }` points at a Redis server; - `config.cache_store = :mem_cache_store, "cache-1.example.com", "cache-2.example.com"` lists Memcached servers, falling back to `MEMCACHE_SERVERS` and then `localhost:11211`. ## What has changed recently Before Rails 8.0, the generated production file carried only a commented-out suggestion, so production quietly used the file store unless you configured Redis or Memcached. Rails 8.0 made Solid Cache the generated default, and Rails 8.1 keeps it. `config.load_defaults` does not touch the store at all: it is chosen by the generator, not by framework defaults, so an upgraded 7.x app keeps whatever its environment files say.

  • Why does a fresh Rails app crash at boot after switching to `config.cache_store = :redis_cache_store`?
    `active_support/cache/redis_cache_store.rb` begins by activating the `redis` gem (version 4.0.1 or later). A new Rails 8.1 Gemfile has no `redis` gem, because the Solid trio replaced it, so loading the store prints a warning asking you to add it and re-raises the `LoadError`. `:mem_cache_store` behaves the same way without `dalli`.
  • Does bumping `config.load_defaults` to 8.1 move an upgraded app onto Solid Cache?
    No. `load_defaults` changes framework defaults, and the cache store is not one of them. The `:solid_cache_store` line is written by the `solid_cache:install` generator. An upgraded app adds the `solid_cache` gem, runs `bin/rails solid_cache:install`, configures the cache database and then changes `config.cache_store` deliberately.

  • A new Rails 8 app needs a Redis server for its production cache.
  • The test environment uses :memory_store so specs can assert on cached values.
  • Leaving config.cache_store unset turns caching off entirely.
  • config.load_defaults 8.0 switches an upgraded app to Solid Cache.
  • Development's :memory_store is one shared cache for every server process.

Assumes Rails 8.1; Solid Cache has been the generated production store since Rails 8.0, and earlier apps fell back to the file store.

open as a page

A Rails 8.1 citizen-petition site runs on two app servers; after Rails.cache.delete, one server still shows an old signature count from :memory_store. Why, and which store would you pick?

level: seniorimportance: must knowfreq 48%

basics

~20 s

:memory_store lives inside each Ruby process, so every Puma worker on each server keeps its own copy and Rails.cache.delete clears only the process that ran it; a shared store such as Solid Cache on a networked database, Redis or Memcached fixes it.

open as a page

In Rails 8.1 development, what does bin/rails dev:cache actually switch, and why does Rails.cache.fetch keep caching when it is off?

level: middleimportance: should knowfreq 40%

basics

~10 s

bin/rails dev:cache creates or deletes tmp/caching-dev.txt, and development.rb sets config.action_controller.perform_caching from that file, which gates Action Controller caching such as view fragments; config.cache_store stays :memory_store, so Rails.cache.fetch caches either way.

open as a page

How does Solid Cache decide which cache entries to delete, and what do max_age and max_size in config/cache.yml control?

level: middleimportance: should knowfreq 38%

basics

~20 s

Solid Cache evicts first-in first-out by row id, not least-recently-used: writes trigger background expiry that deletes the oldest rows when max_size or max_entries is exceeded, and otherwise deletes only rows older than max_age, which defaults to two weeks.

open as a page

In Rails 8.1, when the database behind Solid Cache or the Redis server behind :redis_cache_store becomes unreachable, what does Rails.cache.fetch do?

level: seniorimportance: should knowfreq 28%

basics

~20 s

Both stores fail safe: a read error is treated as a miss so Rails.cache.fetch runs its block, the write is dropped, and the error goes to the store's error_handler and Rails' error reporter instead of raising, so the app stays up but every request recomputes.

open as a page

In Rails 8.1, what is the per-request local cache that stores such as :solid_cache_store and :redis_cache_store wrap around themselves, and when does it surprise you?

level: seniorimportance: nice to knowfreq 16%

basics

~20 s

Stores that include ActiveSupport::Cache::Strategy::LocalCache keep a per-request, per-thread hash in front of the backend: the first read of a key hits the store, later reads in the same request come from memory, including recorded misses, and writes update both.

open as a page