Table of Contents
- Gem Addition
- Installing Gem
- Let's configure the Solid Cache
- Setting changes
- Cache Sharding
- Enable Encryption
Introduction
SolidCache is a database-backed caching mechanism introduced in Rails 8 as the default prod cache store. SolidCache takes advantage of modern SSD storage in a way that will offer bigger capacity at much less cost than traditional in-memory stores like Redis or Memcached.
Tools before solid cache
Prior to the introduction of SolidCache in Rails 8, other external in-memory caching techniques like Redis, Memcached were the adoptions in a Rails application. Redis, a well-known key-value store, worked with Rails redis-cache-store
, allowing fast writes and reads, but required another addition to the already existing infrastructure. Another caching system was Memcached, which was used via Rails mem_cache_store as a distributed memory caching solution allowing for a straightforward, yet efficient, way of caching frequently accessed data.
Why Use Solid Cache?
Solid Cache recently added into Rails 8, which is database-backed. It stores cached data on disk instead of in memory. Due to the speed of SSD, there is not much difference in performance. Using the database-backed caching mechanism, we can store much more data without running into memory limits. Solid cache gives us a better option to store the content without adding the extra dependency of an external tool.
Installation and Implementation
For installation we need to basic setup of rails, then we will try to install solid cache into database.
How to Install Solid Cache
Solid cache is already installed on rails 8, if you are using lower then rails 8 then please add
1. Gem Addition
# Adding solid cache in the Gemfile
bundle add solid_cache
2. Installing Gem
# Installing the gem and generate default config
bin/rails solid_cache:install
Above Command will configure Solid Cache as your cache store, create a config/cache.yml file, and add a database schema for the cache.
3. Let's configure the Solid Cache
You wll need to set up your cache database. Here is what a default config/database.yml might look like for default database SQLite:
# Default configuration from rails 8
prod:
primary:
<<: *default
database: storage/infosia_prod.sqlite3
cache:
<<: *default
database: storage/infosia_prod_cache.sqlite3
migrations_paths: db/cache_migrate
If you are using external database like mysql or postgres:
# prod environment changes
prod:
primary: &infosia_prod
<<: *default
database: infosia_prod
username: infosia
password: <%= ENV["INFOSIA_DATABASE_PASSWORD"] %>
cache:
<<: *infosia_prod
database: infosia_prod_cache
migrations_paths: db/cache_migrate
We have run rails db:prepare
in environment to create the database and load the cache schema.
4. Setting changes
We can customized through the config/cache.yml file. Here’s an example:
default:
store_options: &default_store_options
max_age: <%= 60.days.to_i %>
namespace: <%= "caching_#{Rails.env}" %>
size_estimate_samples: 1200
prod:
databases: [prod_cache1]
store_options:
<<: *default_store_options
max_entries: <%= 128.gigabytes %>
You can change attributes like the cache’s maximum age, size. For example, you might want to increase the cache size in any environment to handle more data.
5. Cache Sharding
Solid Cache supports sharding as well, we can split the cache across multiple databases. This distribute the load, making your caching even more powerful.
To enable sharding, add multiple cache databases to your database.yml:
prod:
cache_shard1:
database: infosia_prod_cache1
cache_shard2:
database: infosia_prod_cache2
6. Enable Encryption
If we are storing sensitive data of customers, we can encrypt our cache. we just need to add this to your config/cache.yml:
prod:
encrypt: true
This ensures that the cached data remains secure, even if someone accesses the database.
Key Features
- External free caching: SolidCache eliminates the need for Redis, staying on your existing SQL database for caching.
- Multiple backend support: While it doesn't require Redis, it still supports various caching backends like in-memory storage and file storage.
- Multiple Caching: SolidCache supports all traditional Rails caching techniques, including fragment and action caching.
- Simplify infrastructure complexity: By using your database, SolidCache reduces the need for extra caching services and simplifies your architecture.
Solid Cache Example
1. Query Cache
In the controller, we can cache database query results using SolidCache, and the data will be stored in your SQL database instead of Redis:
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Rails.cache.fetch("infosia_articles", expires_in: 6.hours) do
Article.all.to_a
end
end
end
According to above example, SolidCache caches the result of the Article.all query in your database and serves it from the cache for the next 6 hours, eliminating the need for Redis.
2: Cache View Fragments
We can also cache view fragments to avoid rendering frequently reused components from scratch:
<% cache "latest_articles", expires_in: 6.hours do %>
<%= render "articles/article", collection: @articles %>
<% end %>
This caches the recent_articles partial for 6 hours, significantly improving performance.
Step 3: Expire Cache
We can manually expire the cache when the below data changes:
# Clear cache with name infosia_articles
Rails.cache.delete("infosia_articles")
It will clear the cache called with keyname infosia_articles
and new data will be added to the system.
Frequently Asked Questions for SolidCache - FAQ's
1. What is SolidCache in Rails 8?
SolidCache is a new database-backed caching system introduced in default Rails 8 configuration. Solid cache stores data in your db instead of using external services like Redis or Memcached.
2. Why SolidCache instead of other In memory databases?
SolidCache adds caching by removing the need for In memory databases, make low infrastructure complexity, and allowing for greater cache sizes by disk space.
3. How do I configure SolidCache in Rails 8?
Rails 8 sets up SolidCache by default, but you can customize it in config/cache.yml and define a dedicated cache database in config/database.yml.
4. Is SolidCache faster than Redis?
Redis is slightly faster since it's in-memory, but SolidCache offers good performance while being easier to manage and scale without extra infrastructure.
5. Can I use SolidCache in older Rails versions?
Yes, you can install it manually by adding the solid_cache gem and setting up the necessary database configurations.