Use Redis as cache to speed up queries

Tram Ho

1. What is Redis?

Redis is an open source (BSD licensed) used as a database, cache, or message broker.

Some of the features of Redis:

  • Is a NoSQL database, stores data in KEY-VALUE form
  • Storing data in RAM, making data retrieval extremely fast.
  • Supports many basic data structures such as Hash, List, Set, Sorted Set, String, …
  • Supports Pub / Sub messaging mechanism.

Thanks to the feature that helps to reduce the query time, Redis is very powerful in being used as a cache for web applications.

2. Use Redis in caching data

2.1. Caching tactics

2.1.1. Cache Aside:

  • When the application needs to read data from the database, it checks to see if the cache contains the data it needs.
  • If yes (cache hit) then returns the data to be queried.
  • If the data is not available in cache (cache miss) then the application will fetch the data from the database.

2.1.2. Read Through:

Similar to cache aside but here, fetching data from the database when cache miss is cache miss (usually supported by independent cache library or provider).

2.1.3. Write Through:

Data is written to cache and then saved to database. When used in conjunction with the read through method, the data will be consistent, not using cache invalidation techniques.

2.1.4. Write Back:

The application saves everything in the cache, then after a certain delay the cache will save everything to the database. This tactic is often used for heavy applications, but the downside is that if an error occurs before the cache saves data to the database, the data stored in the cache will be lost.

2.2. Cache invalidation

When the data in the database is changed, the data in the cache is old and inaccurate. At this time, you need to update or remove expired data in the cache, this process is called cache invalidation.

=> Solution:

  • You can set the lifetime (TTL) for each cache data, depending on the frequency of data changes, the frequency of data being queried, the importance of the data, …
  • In order to avoid the situation where many users are using the application, at the same time the cache expires at the same time causing the server to suddenly suffer a heavy load, it is possible to set the lifetime of each data differently by setting the TTL to a random value. in a certain amount.

2.3. Demo

  • The getRepos function:
  • Cache function: This is the middleware that checks to see if the data is being stored in Redis.
  • Here, using the cache aside strategy, if checking in the cache there is no data to fetch, the application will switch to fetching data via API.
  • TTL is set to be a random value in the range (3600, 4200).

Result

Query 1:

After the first query with the username entered “dantokoro”, we see the repos running takes 823ms. After being queried for the first time, the results will be saved to the redis server.

Second query:

Re-querying with the username “dantokoro” as above, we see that the query time is nearly 20 times faster because instead of retrieving data from api, the application fetches data from redis.

Check data on Redis:

Open Redis CLI, type the command KEYS * to get all the keys being saved, see Redis is saving the key as dantokoro.

Conclusion

From the demo above, we can clearly see the effect of Redis in speeding up data retrieval. Using Redis as a cache is suitable for systems that have data that is frequently accessed by users. In addition to being used as caches, Redis has countless other useful applications that we should learn more.

Link github for the above demo: https://github.com/dantokoro/demo-redis.git (This link includes the demo of Redis application to save the session, you can refer to more).

Thanks for watching

References

  1. https://medium.com/vunamhung/redis- what-what-find- understand-on-determy-data-material-redis-60dd267f53ad
  2. https://neopatel.blogspot.com/2012/04/adding-jitter-to-cache-layer.html
  3. https://codeahoy.com/2017/08/11/caching-strategies-and-how-to-choose-the-right-one/
  4. https://github.com/jankleinert/redis-session-demo
Share the news now

Source : Viblo