質問
It introduces a cache that uses both memory and memcached.
Update: 

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.
Write a comment

Site data features and caching strategies

Write a comment

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.
Write a comment

Features of memcached and Memory Cache

Both middleware, such as memcached (Redis), and memory-based caching mechanisms have their own characteristics.

memcached

Memory cache (memoist)

Write a comment

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.

Write a comment

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.
Write a comment

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.

Write a comment

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.
Write a comment
この記事が気に入ったら応援お願いします🙏
2
ツイート
LINE
Developer
Price Rank Dev
I use Next.js (React) and Firebase (Firestore / Auth) for development. We are also developing APIs for Ruby on Rails and GraphQL. Our team members are 6 Vietnamese and Japanese engineers.