- Summary Summary
- Site data features and caching strategies Site data features and caching strategies
- The most important thing in making Rails faster The most important thing in making Rails faster
- Features of memcached and Memory Cache Features of memcached and Memory Cache
- Cache that uses both memory and memcached Cache that uses both memory and memcached
- Increase the cache hit rate for memcadched Increase the cache hit rate for memcadched
- Make the cache a regular refresh mechanism Make the cache a regular refresh mechanism
- Notes on caching Notes on caching
This article introduces a caching mechanism using both memory cache and memcached, which is effective when building media sites and their APIs.
- In Ruby on Rails, minimizing an amount of queries to DB as much as possible is important to scale efficiently
- A caching mechanism can be implemented by using both memcached and memory cache with good cache characteristics of both.
Site data features and caching strategies
- Sites have different caching strategies for different characteristics, such as frequency of data updates
- This site
The most important thing in making Rails faster
- Avoid accessing DB as much as possible.
- When it accesses DB, only required columns are retrieved.
- Reduce the creation of Ruby objects as much as possible.
- It is necessary to understand the above basic structure of the cash flow.
Features of memcached and Memory Cache
Both middleware, such as memcached (Redis), and memory-based caching mechanisms have their own characteristics.
- Ability to share caches between processes using the same memcached.
- If you have a lot of access to memcached, it's a bottleneck sometimes.
- Caching complex objects in memcached takes a long time to serialize and deserialize.
- It is preferable to specify this in
Rails.cachebecause it can be shared by multiple processes.
- For an overview of the implementation, I recommend Caching with Rails: An Overview — Ruby on Rails GuidesCaching with Rails: An OverviewThis guide is an introduction to speeding up your Rails application with caching.Caching means to store content generated during the request-response cycle and to reuse it when responding to similar requests.Caching is often the most effective way to boost an applicati…Ruby on Rails Guides
Memory cache (memoist)
- Per-process caches; memory caches are not shared with other processes
- Fast read/write with little to no bottleneck
Cache that uses both memory and memcached
The cache structure is as follows:
- Using memcached and memory cache together to improve performance
- Periodically store the results of asynchronous calculations in memcached. If there is no calculation result, the value is retrieved from DB.
- When memcached is accessed, it is stored in the memory cache and the contents of the memory cache are read the second time around.
- Refresh the memory cache every hour and the memcached every hour.
This allows you to share the cache while limiting the amount of access to the memcached.
In addition, frequently accessed data can take advantage of the fast response of the memory cache.
Increase the cache hit rate for memcadched
- The most important point in this strategy is to properly refresh the memcached, memory cache.
- To increase the cache hit rate of memcached, it asynchronously computes and refreshes the calculation results.
Make the cache a regular refresh mechanism
- Periodically perform batch processing with the rake task and store the calculation results in memcached.
- Refresh the cache periodically at that time.
The process of web application (PUMA) implements a mechanism to clear the cache after a certain time elapses in
application_controller.rb, and it clears the cache periodically.
In the process of an active job, a mechanism to clear the cache is implemented in
application_job.rb after a certain time elapses, and the cache is cleared periodically.
Notes on caching
- The cache key should be unique. If you use an array as a cache key, be careful not to create a sort order or empty elements.