Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Multi-level caching in redis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

From coarse to fine:

Take the product detail page as an example

1. Page level cache

(sometimes called url-level cache, manually rendering the page to redis (key= product id,val="product details page, later request this page again, get it from redis, return text/html directly to the front, and can tell the browser to cache the page locally, the disadvantage is that the page data is not updated in time)

2. Object level cache

(For example, commodity data,(key= commodity id, val="commodity data found in database") store commodity information queried once in redis, and when requesting commodity data change for the second time, priority is given to redis query)

The idea behind redis caching is:

1. Check whether redis has data first

1.1 Return if you have.

1.2 If not, query the database, store it in redis, and then return

2. Pay attention to the life cycle of cache.

If the cycle is permanent, then redis can easily collapse, it's just a matter of time.

Use of redis in seconds:

1. Pre-store the inventory of second kill commodities in redis first

2. After the spike starts, pre-reduce the inventory in redis and reduce it to 0. The spike ends (redis is single-threaded).

3. In order to reduce the pressure on redis, you can put the spike request submitted by the user into mq (such as RabbitMq).

For example, there are only 10 products in total, and there are a total of 100,000 users eyeing them. They all submit instant kill requests at almost the same time.

3.1 You can put the request in the message queue and return it to the front end as "queued";

3.2 Consumers ("consumers" of the queue) fetch data from the message queue at a fixed rate, create orders into the database (that is, orderly create 10 orders, database 0 impact),

3.3 Although it may be the top 10 users, if the order creation fails for some reason, the user's spike request will be placed at the end of mq, and then orders will be created for other users in the queue.

3.4 Create a successful order request, remove the request from mq, and send a short message to the user,"Congratulations, second kill successful!" "

3.5 After successfully creating 10 orders (i.e., the second kill is over), return "second kill failed" to other users in the queue.

The above is the detailed content of redis multilevel cache introduction, more please pay attention to other related articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report