- 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.
Summary
- 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 is a media site where it doesn't matter if the information displayed in the view is several hours old.
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.
memcached
- 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.cache
because it can be shared by multiple processes. - For an overview of the implementation, I recommend as it is written in an easy to understand manner.
Memory cache (memoist)
- Per-process caches; memory caches are not shared with other processes
- Fast read/write with little to no bottleneck
- is one of the good libraries for memory cache.
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
memcached
- Periodically perform batch processing with the rake task and store the calculation results in memcached.
- Refresh the cache periodically at that time.
Memory Cache(memoist)
The process of web application (PUMA) implements a mechanism to clear the cache after a certain time elapses in after_action
of 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 after_perform
of 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.