How does Solid Cache decide which cache entries to delete, and what do max_age and max_size in config/cache.yml control?
answer
- oldest row id goes first
- reads never refresh an entry
- expiry runs only after writes
- max_age defaults to two weeks
- generated max_size is 256 MB
basics
~20 sSolid 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.
solid answer
~50 sSolid Cache stores each entry as a row in `solid_cache_entries` and evicts **FIFO** by `id`: reads never update anything, so a hot entry ages out as fast as a cold one. Expiry is driven by writes. Each write adds a fractional chance of an expiry task, about one per 50 writes with the default `expiry_batch_size` of 100, run on a background thread (`expiry_method: :thread`) or as a `SolidCache::ExpiryJob` (`:job`). The task first asks whether the cache is full: more rows than `max_entries`, or an estimated size, sampled from `byte_size`, above `max_size`. If so it deletes a batch of the oldest rows; if not, it deletes only old rows created before `max_age` ago. In the gem `max_age` defaults to `2.weeks`; the generated `cache.yml` leaves its `max_age` line commented, sets `max_size: 256.megabytes` and namespaces keys by `Rails.env`.
code
yaml · 10 lines# config/cache.yml (generated, with max_age switched on)
default: &default
store_options:
max_age: <%= 30.days.to_i %>
max_size: <%= 2.gigabytes %>
namespace: <%= Rails.env %>
production:
database: cache
<<: *defaultgo deeper
Remember that Solid Cache keeps entries as database rows and removes the oldest ones first, using max_age and max_size from config/cache.yml.
Explain FIFO by id versus LRU, the write-triggered expiry tasks, and the difference between the gem's two-week max_age and the commented line in the generated file.
Size the cache on purpose: raise max_size beyond the generated 256 MB, decide on a retention ceiling, consider expiry_method: :job, and watch the table's growth after deploys that change many keys.
Weigh FIFO's weaker hit rate on hot keys against a cache many times larger than RAM allows, and decide what latency and retention numbers would push the team towards an in-memory store instead.
## Solid Cache in one paragraph **Solid Cache** is the gem behind `config.cache_store = :solid_cache_store`, the production default in a new Rails 8.x app. It is an `ActiveSupport::Cache::Store` that keeps every entry as a row in a database table, `solid_cache_entries`, usually in a separate cache database. Because disks are cheap compared with RAM, the design bet is a **much larger cache that lives longer**, with a simpler eviction rule than an in-memory store would use. ## FIFO, not LRU An LRU (least recently used) cache must record every read so that it can evict what nobody has touched lately. Solid Cache deliberately does not. Its README and the Rails caching guide call it a **FIFO (first in, first out)** cache: - a read is a plain `SELECT`; nothing about the row changes; - rows are deleted from the low end of the `id` sequence, which is also creation order; - the number of rows can be estimated cheaply from the difference between the highest and lowest `id`. The cost is lower hit efficiency for very hot keys, which age out on schedule even when they are read constantly. The gem compensates by keeping entries much longer than a RAM cache could, and a hot key that is evicted is simply recomputed and written again as a new row at the young end. ## When expiry runs Solid Cache has no scheduler. **Expiry is triggered by writes**: 1. Every write calls `track_writes`, which adds `2 / expiry_batch_size` expiry tasks per written entry, rounding by chance. With the default `expiry_batch_size` of 100 that is one task per 50 writes on average, so deletion outpaces insertion when the cache must shrink. 2. The task runs on a background thread (`expiry_method: :thread`, the default) or is enqueued as **`SolidCache::ExpiryJob`** on `expiry_queue` (`:default`) when `expiry_method: :job`. 3. An idle cache never expires anything, because nothing triggers a task. That is by design: no writes means no growth. ## What the task deletes Each task picks candidates from the oldest rows by `id`, takes three times the batch size and samples down, so two concurrent tasks rarely fight over the same rows. Then: | Condition | Rows deleted | |---|---| | `max_entries` set and the id range exceeds it | up to `expiry_batch_size` of the oldest rows, whatever their age | | `max_size` set and the estimated size exceeds it | up to `expiry_batch_size` of the oldest rows, whatever their age | | not full, `max_age` set | only the oldest rows whose `created_at` is older than `max_age` | | not full, `max_age: nil` | nothing | The size estimate samples the `byte_size` column; `size_estimate_samples` (10,000 by default) controls how many rows it looks at. Per-entry expiry still exists alongside this. An entry written with `expires_in:` becomes a miss once that time passes, and a read that finds it expired deletes it; FIFO eviction is about the rows nobody comes back for. ## The settings and their defaults The options live under `store_options:` in `config/cache.yml` (or `config/solid_cache.yml`, or a path in `SOLID_CACHE_CONFIG`): - **`max_age`**: maximum age of an entry in seconds. Gem default `2.weeks.to_i`. The generated file shows `max_age: <%= 60.days.to_i %>` commented out, so a new app runs on the two-week default. The README warns against `nil` unless `max_entries` bounds the table. - **`max_size`**: an upper bound on the total `byte_size`. Gem default `nil`; the generated file sets `256.megabytes`, which teams usually raise because the whole point is a big cache. - **`max_entries`**: an upper bound on the row count, default `nil`. - **`namespace`**: prefixes keys; the generated file uses `Rails.env`. - **`expiry_batch_size`**, **`expiry_method`**, **`expiry_queue`**: how much each task deletes and where it runs. Top-level keys beside `store_options` select the database (`database: cache` in the generated production section) and turn on `encrypt: true`. ## What interviewers listen for They want to hear that Solid Cache trades LRU precision for cheap bookkeeping, that deletion is write-driven and batched, that `max_age` exists as a retention ceiling as much as a size control (the generated comment calls it a way to fulfil retention policies), and that the defaults differ between the gem and the generated file.
- What does `expiry_method: :job` change, and when would you choose it?Instead of deleting rows on a background thread inside the web process, Solid Cache enqueues `SolidCache::ExpiryJob` on `expiry_queue` (`:default` unless set). Choose it when you want deletes off the request processes, for example to keep their database connections free, and you already run a job backend such as Solid Queue.
- Why might a Solid Cache table keep growing past the limit you expect?Without `max_size` or `max_entries`, only `max_age` bounds the table, so a burst of unique keys stays until it is two weeks old. The size check is also an estimate from sampled `byte_size` values, and expiry only runs after writes, so the table can overshoot briefly before batches catch up.
Solid Cache works like a warehouse that clears pallets strictly in the order they arrived: how often staff pulled goods from a pallet makes no difference to when it leaves.
- Solid Cache evicts the least recently read entries first.
- Reading an entry in Solid Cache resets its age.
- Solid Cache runs a cron-like sweeper even when nothing is written.
- Without max_age in cache.yml, Solid Cache entries never expire by age.
- max_size is enforced exactly on every write before the row is inserted.
Assumes Solid Cache 1.0 with Rails 8.1; the generated cache.yml leaves max_age commented, so the gem's two-week default applies.