# Solid Cache in Rails 8: Setup, Sizing and Production Pitfalls

> Solid Cache Rails guide: how the database-backed cache works, how to set it up in Rails 8, how to size and expire it, and what to watch in production.

- URL: https://railsdev.io/blog/solid-cache-rails
- Published: 2026-11-07
- Updated: 2026-11-11
- Author: Hendra Nicholas (Rails Full-Stack Developer & AI Engineer)

---

Solid Cache Rails support gives your app a cache store that keeps cached data in your SQL database (PostgreSQL, MySQL or SQLite) instead of in memory or in Redis. Solid Cache is a drop-in `ActiveSupport::Cache::Store`, so `Rails.cache.fetch`, fragment caching and Russian doll caching work the same way. It is the default cache in new Rails 8 apps.

This guide shows how Solid Cache works, how to add it to an existing app, how to size and expire it, and where it behaves differently from Redis or Memcached. It is the cache companion to my guide to [Solid Queue in Rails 8](/blog/solid-queue-rails). If your cache is one reason a page is slow, my article on [scaling a Ruby on Rails application](/blog/scaling-ruby-on-rails) puts caching in the wider picture.

> I ran the Ruby examples in this article against Solid Cache 1.0.10 on Rails 8.1.4.1 and Ruby 3.3.6, with SQLite as the cache database. I did not run PostgreSQL for this article. The PostgreSQL notes and the sharding and encryption options come from the official README, not from a run I did.

## What Solid Cache is

Rails caching has always had one trade-off: a fast cache must fit in RAM, and RAM is expensive. Memcached and Redis keep everything in memory, so teams keep the cache small and short-lived.

[Solid Cache](https://github.com/rails/solid_cache) takes the other side of that trade-off. Its README explains the idea: modern SSDs have access times close enough to RAM for most caching, so you can keep a much larger cache on disk. A bigger cache means a higher hit rate and fewer expensive recomputations. Each entry is one row in a `solid_cache_entries` table, and the cache is a normal Active Record-backed store.

It is maintained under the Rails organization and released under the MIT licence. As of November 7, 2026, the latest release on RubyGems is 1.0.10, published November 8, 2025, with more than 26 million downloads. It depends on Active Record, Active Job and Railties 7.2 or newer, so it also works on apps that have not reached Rails 8 yet.

## Why it is the default in Rails 8

A new Rails 8 app configures Solid Cache, [Solid Queue](/blog/solid-queue-rails) and Solid Cable out of the box. The reason is fewer moving parts. If you cache with Redis, you run a Redis server, secure it, size its memory, and decide what happens when it restarts empty. With Solid Cache the cache lives in the database stack you already run, back up and monitor.

That matters most for small teams and early-stage products, where one less service is one less thing to page someone about. I make the same argument in [why Ruby on Rails is still a great choice for SaaS startups](/blog/is-ruby-on-rails-still-relevant).

## Installing Solid Cache in an existing app

A new Rails 8 app already has all of this. For an older app, the README gives these steps:

```bash
bundle add solid_cache
bin/rails solid_cache:install
```

The installer made three changes when I ran it on a fresh Rails 8.1 app:

- It created `config/cache.yml`.
- It created `db/cache_schema.rb` (or `db/cache_structure.sql` if your app uses SQL schema files).
- It changed `config/environments/production.rb` to use `config.cache_store = :solid_cache_store`.

It did not add a `cache` database to `config/database.yml` in my run, even though the README lists that as step 3. You need to do it yourself. Here is the development setup I used to test:

```yaml
# config/database.yml
development:
  primary:
    adapter: sqlite3
    database: storage/development.sqlite3
  cache:
    adapter: sqlite3
    database: storage/development_cache.sqlite3
    migrations_paths: db/cache_migrate
```

```yaml
# config/cache.yml
development:
  database: cache
  store_options:
    max_size: <%= 256.megabytes %>
    namespace: <%= Rails.env %>
```

The README says to run `db:prepare` in production so the cache table gets created. In my test, `bin/rails db:prepare` loaded `db/cache_schema.rb` into the cache database.

For PostgreSQL, the shape is the same. Add a `cache` entry that points at its own database, and set `database: cache` in the production block of `cache.yml`. The generated `cache.yml` already does the second part. Check `db/cache_schema.rb` too. It creates one table with these columns, which shows what each entry costs:

```ruby
# db/cache_schema.rb (generated)
create_table "solid_cache_entries", force: :cascade do |t|
  t.binary "key", limit: 1024, null: false
  t.binary "value", limit: 536870912, null: false
  t.datetime "created_at", null: false
  t.integer "key_hash", limit: 8, null: false
  t.integer "byte_size", limit: 4, null: false
  t.index ["byte_size"]
  t.index ["key_hash", "byte_size"]
  t.index ["key_hash"], unique: true
end
```

Keys are hashed into `key_hash` for lookups. Values are stored as binary, with a limit of 512 MB per value, though you should never get near that.

## Using it: the cache API does not change

Your existing cache calls keep working. This is what I ran after setup, with a separate process for the persistence check:

```ruby
Rails.cache.class
# => SolidCache::Store

Rails.cache.write("greeting", { text: "hello" }, expires_in: 1.second)
Rails.cache.read("greeting")   # => {:text=>"hello"}
sleep 1.1
Rails.cache.read("greeting")   # => nil, the entry has expired

Rails.cache.write("counter", 1, raw: true)
Rails.cache.increment("counter", 2) # => 3

Rails.cache.read_multi("counter")   # => {"counter"=>3}
```

I also checked that a value written in one `bin/rails runner` process was readable from a second process. That is the main difference from the default development `:memory_store`, which lives and dies with one process.

In views, fragment caching is unchanged:

```erb
<%# app/views/products/_product.html.erb %>
<% cache product do %>
  <%= render "details", product: product %>
<% end %>
```

The cache key is based on the record's `cache_key_with_version`, so a changed `updated_at` creates a new entry. The old entry is never read again and ages out on its own.

## Sizing and expiring the cache

Solid Cache has no memory limit, so you set the limits that fit your disk. These options go under `store_options` in `config/cache.yml`:

| Option | What it does | Default |
| --- | --- | --- |
| `max_age` | Oldest an entry can get before it is expired | Two weeks |
| `max_size` | Cap on total cache size | No limit |
| `max_entries` | Cap on number of entries | No limit |
| `namespace` | Prefix for keys, so apps or environments can share a database | None |

The generated file sets `max_size` to 256 MB and leaves a commented `max_age` example of 60 days. The README advises against setting `max_age` to `nil` unless you use `max_entries` instead. Without any limit, the table only grows.

Expiry is not a TTL sweep. The README describes it as first-in, first-out and approximate. Writes trigger expiry in batches, and the README says it starts when writes reach 50% of the batch size. By default, expiry runs on a background thread. You can switch to an Active Job with `expiry_method: job`, and tune `expiry_batch_size` (default 100) and `expiry_queue`.

Two practical points follow from this:

1. **Entry counts and sizes are estimates.** Don't alert on exact numbers. Watch the table's size on disk instead.
2. **`expires_in` is still honored on read.** In my test, an expired entry returned `nil` immediately. Table cleanup is a separate, slower concern.

## Differences from Redis and Memcached

| | Solid Cache | Redis or Memcached |
| --- | --- | --- |
| Where data lives | SQL table on disk | Memory |
| Typical cache size | Large (gigabytes) | Limited by RAM |
| Survives a restart | Yes | Redis can, Memcached does not |
| Extra service to run | No | Yes |
| Read latency | Database round trip | Usually lower |
| `delete_matched` | Not supported | Supported by Redis |

The one that surprises people is `delete_matched`. I called it on `SolidCache::Store` and got `NotImplementedError`. If your app clears groups of keys by pattern, change that code before you switch. Use versioned keys or `cache_key_with_version` so stale entries are simply never read again.

On latency: a database read is slower than a memory read, and I did not benchmark either on production hardware, so I won't give numbers. For most Rails apps the cost is small compared with the work a cache hit avoids, such as a slow query or a rendered partial. If you cache thousands of tiny values per request, measure before you move.

## Fitting it into an existing app

Moving from Redis or Memcached is mostly a configuration change, because the API is the same. A safe path:

1. Add the gem and run the installer in a branch.
2. Search the code for `delete_matched`, `Rails.cache.redis` or any call that reaches into the Redis client.
3. Deploy with Solid Cache and a cold cache. Expect a burst of misses and higher database load for a while.
4. Watch database CPU and connection use for a few days, then shrink or remove Redis if nothing else needs it.

The cold start is the real cost of switching. Plan the deploy for a quiet period. If you are also on an older Rails version, do the version upgrade first. My [Rails 8.1 upgrade guide](/blog/rails-8-1-upgrade) covers that path.

Moving a cache is easy to get right, but cache bugs show up as slow pages or stale data under real traffic. If you would like a second pair of eyes on a Rails app with performance problems, I offer [Rails consulting](/services/ruby-on-rails-consulting), including code audits and performance tuning.

## Production notes

### Use a separate database

The README's default setup gives the cache its own database entry. This keeps cache writes out of your main database's transactions and lets you size, back up and tune it separately. A cache does not need backups, so you can skip them for that database.

If you leave out `database`, `databases` and `connects_to`, Solid Cache uses the `ActiveRecord::Base` connection pool. The README notes that cache writes then join any surrounding transaction. A rolled-back transaction would also roll back its cache writes, which is rarely what you want.

### Plan connections

Each Puma thread that touches the cache needs a connection to the cache database, in addition to the primary. Keep the cache pool in `database.yml` in line with `RAILS_MAX_THREADS`, and check that your database server allows the total. I cover the same pool sizing for jobs in the [Solid Queue guide](/blog/solid-queue-rails).

### Encryption and sharding

The README documents two options for larger or more sensitive setups, which I did not test:

- `encrypt: true` encrypts values and requires Active Record Encryption to be configured. The README says the custom serializer only works with binary columns.
- `databases` lets you list several database connections, and keys are spread across them with consistent hashing. The older `clusters` option is deprecated in favor of `databases`.

### Index size limits

The generated migration assumes index entries of up to 1024 bytes. If your database cannot handle that, the README says to edit the migration and set `max_key_bytesize` to match.

### Deploying

If you deploy with Kamal, the cache database is one more thing your `bin/rails db:prepare` step has to reach. My [Kamal for Rails 8 guide](/blog/kamal-rails) shows how that deploy flow handles the database and secrets.

### Checklist before you switch

- [ ] The cache has its own database entry in `database.yml`.
- [ ] `max_size` or `max_age` is set so the table cannot grow forever.
- [ ] No code depends on `delete_matched`.
- [ ] The connection pool matches your thread count.
- [ ] You have a plan for the cold-cache period after the first deploy.

## Strengths and limitations

Solid Cache is a good fit when you want a large cache, a short list of services, and a team that already runs a database well. It is a weaker fit when you need the lowest possible read latency, or when the app already depends on Redis features such as pattern deletes.

It also makes the database stack more important. A cache on the same server as your primary will compete for disk and CPU during a miss storm, so size the hardware with that in mind.

The other half of the Solid trio: [Solid Queue in Rails 8](/blog/solid-queue-rails).

## Conclusion

Solid Cache replaces the "cache must fit in RAM" rule with "cache can fit on your SSD". For most Rails 8 apps, that means a bigger cache, one less service, and the same `Rails.cache` API. Give it its own database, set `max_size` or `max_age`, remove any `delete_matched` calls, and measure before you decide it is too slow.

I help teams upgrade and tune Rails apps: moving to Rails 8, replacing Redis with the Solid stack, and finding the queries and caches behind slow pages. If you are planning that change, [send me a short brief and book a discovery call](/#contact).

## Frequently asked questions

### Is Solid Cache better than Redis for Rails?

It depends on what you need. Solid Cache gives you a larger cache and no extra service. Redis gives lower read latency and features like pattern deletes. For most apps, the simpler operations of Solid Cache are worth it, but measure if your cache is on a very hot path.

### Does Solid Cache work with PostgreSQL?

Yes. The README lists SQLite, MySQL, PostgreSQL and Trilogy as supported databases. I ran my examples on SQLite, so check the PostgreSQL setup in a staging environment first.

### Which Rails version does Solid Cache need?

The gem depends on Active Record, Active Job and Railties 7.2 or newer, according to its RubyGems metadata. Rails 8 apps include it by default.

### How do I clear the Solid Cache?

`Rails.cache.clear` clears it. The `clear_with` option controls whether it uses `truncate` or `delete`. Avoid pattern-based clearing, since `delete_matched` is not supported.

### Does Solid Cache expire old entries automatically?

Yes, but approximately. It removes the oldest entries when you pass `max_age`, `max_size` or `max_entries`, and the README says expiry is first-in, first-out and runs in batches. Entries with `expires_in` also stop being returned once they expire.

### Can Solid Cache share the main database?

It can: without a `database` option it uses the main connection pool. I recommend a separate database instead, so cache writes are not part of your app's transactions and you can size it on its own.

### Do I still need Redis after switching?

Only if something else uses it. Sidekiq needs Redis, for example, while [Solid Queue](/blog/solid-queue-rails) does not. Check what else talks to Redis before you remove it.

## References

- [rails/solid_cache on GitHub](https://github.com/rails/solid_cache): README with installation and configuration options.
- [solid_cache on RubyGems](https://rubygems.org/gems/solid_cache): release history and dependencies.
- [Caching with Rails](https://guides.rubyonrails.org/caching_with_rails.html): the official guide to cache stores and fragment caching.
