Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What are the problems related to Redis?

Shulou Source: shulou.com Published: 2022-06-01 02:04:57 09月25日 Update

This article mainly introduces the relevant knowledge of Redis-related issues, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this Redis-related article. Let's take a look at it.

First, cache penetration

Concept-access to a key that does not exist, the cache does not work, the request will penetrate to the DB, and the DB will hang up when the traffic is heavy.

Existence scene

The design of the system is unreasonable and the cache data is not updated in time.

Malicious attacks such as reptiles

Solution

Using a Bloom filter, using a large enough bitmap to store the key that may be accessed, and the key that does not exist is filtered directly.

If the access key does not exist in DB, write a null value in Redis and set a short expiration time to avoid long-term memory consumption.

Second, cache avalanche

Concept

A large number of key set the same expiration time, resulting in the failure of all caches at the same time, resulting in a large amount of instantaneous DB requests, a sudden increase in pressure, and an avalanche.

Solution

Beforehand: use master-slave replication + Sentinel or Redis cluster.

Business: local cache combines current restriction and degradation.

Afterwards: enable persistent configuration to achieve fast recovery of fast cache.

You can add a random value to the cache when setting the expiration time, so that the expiration time of each key is distributed and does not expire at the same time.

Third, cache breakdown

Concept

Hot key problem, an existing key, at the moment when the cache expires, there are a large number of requests that will break through the DB, resulting in a large number of instantaneous DB requests and a sharp increase in pressure.

Solution

Before accessing the key, use SETNX (set if not exists) to set another short-term key to lock the access of the current key, and then delete the short-term key at the end of the access (mutex lock, only one thread is allowed to build the cache at a time, and other threads wait for the construction to be completed)

Only one thread is allowed to build the cache at a time, and the other threads wait for the build to be cached

4. Database and cache double writes are inconsistent.

Concept

When a data needs to be updated, it is impossible to update the database and cache at the same time, then the problem of data inconsistency must occur when reading the data, and the problem of data inconsistency is definitely not allowed in the system in the field of financial transactions.

Solution

When reading, first read the cache, if there is no cache, read the database, then take out the data and put it into the cache, and return the response.

When updating, update the database before deleting the cache.

Fifth, cache elimination strategy

Concept

Cache 30 gigabytes of data, but Redis itself only supports 10 gigabytes, then use the cache elimination mechanism to retain the most important 10 gigabytes of data.

Elimination strategy provided by Redis

View the maximum memory allowed by Redis, attribute: server.maxmemory

Specify data elimination algorithm: maxmemory-policy allkeys-lru

The number of samples of LRU and minimum TTL algorithm: maxmemory-samples 5

Volatile-lru: select the least used data to be eliminated from datasets with an expiration time set

Volatile-ttl: select expired data to be eliminated from datasets with an expiration time set

Volatile-random: arbitrarily select data elimination from datasets with an expiration time set

Allkeys-lru: select the least used data from the dataset to be eliminated

Allkeys-random: data elimination by randomly selecting data from the dataset

No-enviction: error will be reported when data is added when insufficient memory is available.

This is the end of the article on "what are the Redis-related issues?" Thank you for reading! I believe you all have a certain understanding of "what are the issues related to Redis". If you want to learn more, you are welcome to follow the industry information channel.

Tags: Data cache time problem concept update database solution moment thread solution consistency memory simultaneous time knowledge content pressure short-term strategy Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker Shulou Information OPPO Reno MariaDB