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 solve the problem of redis breakdown

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

Share

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

This article will explain in detail how to solve the problem of redis breakdown. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Breakdown: means that a single key cannot be found in the cache and is queried in the database, so that there is no problem if the amount of data is small or concurrency is not large.

If the database has a large amount of data and high concurrency, it may cause the database to crash under too much pressure.

Note: this refers to the high concurrency of a single key!

Solution:

1) through the double check mechanism of synchronized+: a key allows only one thread to query, blocking other threads

In the synchronization block, continue to judge and check to ensure that it does not exist before checking the DB.

For example:

Private static volaite Object lockHelp=new Object (); public String getValue (String key) {String value=redis.get (key,String.class); if (value== "null" | | value==null | | StringUtils.isBlank (value) {synchronized (lockHelp) {value=redis.get (key,String.class); if (value== "null" | value==null | | StringUtils.isBlank (value) {value=db.query (key)) Redis.set (key,value,1000);} return value;}

Cons: blocking other threads

2) set value to never expire

This approach can be said to be the most reliable, the safest, but takes up space, consumes a lot of memory, and cannot keep the data up-to-date, which needs to be done according to the specific business logic.

Personally, I think if you want to keep the data up-to-date and try it this way, it's for reference only:

Start a scheduled task or use TimerTask to set the time, each time more than these values are queried to update the cache, of course, the premise is that it will not put too much pressure on the database (this is important)

3) use mutex (mutex key)

A common practice in the industry is to use mutex. To put it simply, when the cache expires (it is determined that the value obtained is empty), instead of going to load db immediately, you should first use some operations of the cache tool with the returned values of successful operations (such as Redis's SETNX or Memcache's ADD) to set a mutex key, and then perform the load db operation and reset the cache when the operation returns success; otherwise, retry the entire get cache method.

SETNX, which stands for "SET if Not eXists", is set only when it does not exist, and can be used to achieve the effect of a lock. The expiration time of setnx was not implemented in previous versions of redis2.6.1, so here are two version code references:

Public String get (key) {String value = redis.get (key); if (value = = null) {/ / indicates the cache value expires / / sets the 3min timeout to prevent the del operation from failing. The next cache expiration cannot be load db if (redis.setnx (key_mutex, 1,3 * 60) = = 1) {/ / indicates a successful setting value = db.get (key) Redis.set (key, value, expire_secs); redis.del (key_mutex); return value;} else {/ / this time means that other threads at the same time have been load db and set back to the cache. At this time, you can sleep (10) by retrying to get the cache value. Get (key); / / retry}} else {return value;}} the solution to the redis breakdown problem is shared here. I hope the above content can be of some help to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

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