Distributed caching with Ruby on Rails

Viral Patel
3 min readDec 14, 2021
Distributed caching with Redis and Ruby on Rails

Latency is zero — one of the fallacies of distributed computing

Why do you need caching?

Ignoring the impact of network latency in your application can be a huge mistake and especially for applications that is looking to scale well on-demand. With cloud infrastructure in place, now-a-days it feels like scaling problems can be solved by just increasing number of replicas and provisioning more servers. That might solve your availability problems but it cannot solve performance problems. No matter how much horizontally you scale your application servers and data stores, your application can still suffer from packet loss the transportation can cost, increasing amount of traffic and plus bandwidth costs.

Distributed caches are especially useful in environments with high data volume and load. It can speed up your application by storing most frequently accessed data.

Why use Redis?

Read this guide on how Redis can be used LRU cache solution. Redis offers following set of features which enables developers to implement a solid distributed caching solution that is highly reliable and preferment.

Sub-millisecond latency

By storing data in-memory they can read data more quickly than disk based databases.

Data partitioning

Redis allow you to distribute your data among multiple nodes. This allows you to scale out to better handle more data when demand grows.

Data Replication

Redis lets you create multiple replicas of a Redis primary. This allows you to scale database reads and to have highly available clusters.

Transactions

Redis supports transactions which let you execute a group of commands as an isolated and atomic operation.

Configuring Redis as Rails cache store

Since Rails 5.2, Redis cache store comes in with built-in support in Rails and it is very easy to setup.

  1. Install redis gem
gem 'redis'

2. Add the configuration in the relevant config/environments/*.rb file

config.cache_store = :redis_cache_store, { 
url: ENV['REDIS_URL'] { 'redis://example.com:6379/4' }
}
config.action_controller.perform_caching = true

Optionally, you can install `hiredis` gem

gem 'hiredis'

Built in Redis cache store support will automatically use hiredis for better preferment client.

Redis alternative

Memcached based cache store is also an amazing alternative if you don’t want to use Redis. Memcached has all it takes to implement distributed caching solution but it does not offers additional set of rich features that Redis has but for majority of the applications it wouldn’t be a problem.

Challenges of using caching in your application

One of the hardest problems to solve in computer programming is cache invalidation

Thankfully, Rails community do the best work for cache invalidation is handled properly and also gives developers flexibility to incorporate their own caching logic for very niche use cases. Read this rails guide for more detailed implementation of cache stores in Rails and other alternatives of redis.

Let me know by commenting below if this post is helpful. I wish to go into more detail of discussing the ins and outs of implementing distributed caching solution.

--

--