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?
answer
- each process holds its own copy
- delete reaches one process only
- file store is per host disk
- SQLite cache file lives on one host
- shared: Solid Cache, Redis, Memcached
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.
solid answer
~40 s`ActiveSupport::Cache::MemoryStore` is a hash inside one Ruby process. Two servers, each possibly running several Puma workers, mean several independent caches, so a `Rails.cache.delete` or `increment` in the admin's request changes one of them and the others keep serving their copy until it expires. `:file_store` has the same problem one level up: it is shared by processes on one host but not across hosts. The fix is a store both servers reach. In Rails 8.1 the default is `:solid_cache_store`, but only if its cache database is a networked PostgreSQL or MySQL database: the generated SQLite setup puts `production_cache.sqlite3` on one host's disk. Otherwise use `:redis_cache_store` or `:mem_cache_store`, which add a service but answer from memory. For a petition site I would put Solid Cache on the existing PostgreSQL server unless measured read latency says otherwise.
code
ruby · 6 lines# config/environments/production.rb
# Before: one private cache per Puma worker on each server
# config.cache_store = :memory_store
# After: one cache every process reads and writes
config.cache_store = :solid_cache_storego deeper
Remember that :memory_store lives inside one Ruby process, so two servers or two Puma workers mean separate caches that do not see each other's deletes.
Compare the scope of each store: process, host, or everything that reaches a shared database or cache server, and say which ones survive a restart.
Spot the SQLite trap in the default Solid Cache setup, choose a shared store from what the deployment already runs, and check that counters use an atomic increment on the chosen store.
Argue the cost of another stateful service against cache latency, and decide which measurements, such as cache read time as a share of response time, would justify adding Redis or Memcached.
## Why the servers disagree The petition site caches a signature count with something like `Rails.cache.fetch([petition, "signatures"], expires_in: 5.minutes)`, and an admin action calls `Rails.cache.delete` when a petition closes. With **`config.cache_store = :memory_store`**: - **`MemoryStore` is per Ruby process.** Its own documentation says that several server processes, as in Puma's clustered mode, cannot share cache data. - **Two servers** means at least two such caches; Puma workers multiply it further, since each forked worker has its own hash. - `delete`, `write` and `increment` change only the process that ran them. Every other process keeps its entry until `expires_in` runs out or the 32 MB LRU prune drops it. So the bug is not the delete call. It is a store whose scope is smaller than the deployment. ## How far each store reaches | Store | Scope | Bounded by | Notes | |---|---|---|---| | `:memory_store` | one process | `size:` (32 MB default), LRU prune to three quarters | fastest, lost on restart | | `:file_store` | one host's disk | nothing; `cleanup` removes expired files | survives restarts | | `:solid_cache_store` | everyone who reaches the cache database | `max_age`, `max_size`, `max_entries`, FIFO | no extra service, a query per read | | `:redis_cache_store` | everyone who reaches Redis | Redis' own memory settings | in memory, needs a dedicated Redis | | `:mem_cache_store` | everyone who reaches the Memcached servers | Memcached's memory limit | in memory, `dalli` client spreads keys across servers | ## The Solid Cache trap on two servers A new Rails 8.1 app defaults to SQLite, and the generated `database.yml` gives the production `cache` database the file `storage/production_cache.sqlite3`. A file on one host's disk is exactly as unshared as `:file_store`. For two servers, Solid Cache needs its cache database on a server both hosts reach, typically a `cache` entry on the PostgreSQL or MySQL server the app already uses, with `database: cache` in `config/cache.yml`. With a networked database, every process reads and writes the same `solid_cache_entries` table, so a delete is seen everywhere at once. ## Choosing between the shared stores 1. **Solid Cache on the existing database** adds no new service and no new failure domain beyond the database you already back up and monitor. Reads are an indexed query, slower than RAM but usually small next to the work being cached, and the cache can be gigabytes because it lives on disk. Expiry is FIFO and write-driven, and like Memcached it does not implement `delete_matched`. 2. **Redis** answers from memory and supports `delete_matched` through `SCAN`. It needs a Redis instance dedicated to caching with an eviction policy; the store's own notes warn that Redis will not expire cache entries by default and copes badly with mixed queue-and-cache usage. Add the `redis` gem. 3. **Memcached** is in-memory, evicts within its own memory limit and spreads keys across several servers through `dalli`, but it does not implement `delete_matched` in Rails. For counters, check the operation, not just the store. `increment` on Redis or Memcached uses the server's atomic increment, and Solid Cache wraps it in a transaction that locks the existing row, so concurrent increments of an existing counter are not lost. On `:memory_store`, each process counts on its own. ## A reasonable answer for this site A petition site has spiky reads when a petition goes viral, modest writes, and probably one PostgreSQL server already. I would: - move production to `:solid_cache_store` with a `cache` database on that PostgreSQL server, sized with `max_size` well above the generated 256 MB; - keep `expires_in` on the signature count short, because a few minutes of staleness is acceptable but a closed petition should not stay open for long; - reconsider Redis only if measured cache read latency becomes a visible share of response time. The interviewer is listening for the scope argument first, the SQLite trap second, and a trade-off grounded in what the app already runs, not a reflex towards Redis.
- Would `:file_store` on a directory shared by both servers over the network solve the problem?It would share data, but `FileStore` has no size limit and relies on the filesystem for consistency, and a network filesystem adds latency and locking behaviour Rails does not manage. Rails ships stores designed for sharing, so a shared directory is a workaround rather than a design.
- Why does the counter drift on `:memory_store` even on a single server?Puma's clustered mode forks several workers, and each worker has its own `MemoryStore`. A request in worker A increments A's copy; worker B never sees it. Only a store outside the processes, such as Solid Cache, Redis or Memcached, gives all workers one counter.
- Rails replicates :memory_store entries between servers automatically.
- Rails.cache.delete broadcasts the deletion to every Puma worker.
- The default Solid Cache setup is shared across servers even on SQLite.
- Solid Cache cannot be used without a Redis server behind it.
- :file_store is a shared cache for every server in a deployment.