Get the App
SLTechnology News&Howtos  ›  Database  › 

How to implement Multi-level caching in redis

Shulou Source: shulou.com Published: 2022-06-01 19:02:00 10月03日 Update

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!

Tags: Merchandise data cache user order page queue database success level query front end cycle inventory time message consumer details consumption eyeball Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Redmi NVidia Shulou Technology Xiaomi