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

What is the Redis deletion policy and eviction policy?

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

This article mainly introduces what the Redis deletion strategy and eviction strategy are, which can be used for reference by friends who need it. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.

Expired data

Let's first look at the three key values, which are sex, name, and age.

The instruction set for these three values is set name kaka setex age 10024 setex sex 10 1.

In redis, we can use ttl to get the status of a key. Let's use ttl to get the status of the above name, age, and sex, respectively.

You can see that there are three values, which are-1775-2.

So what is the information given by these three values?

-1 indicates permanently valid data 775. This value is set on age, and the instruction used is setex age 1000 24, indicating the remaining valid time.-2 indicates expired data, or deleted data, or undefined data.

Storage structure of expired data when we set up a timed name, redis stores a memory address 0x10101, and then redis opens up a space to store timed key, but the storage method is the memory address and expiration time corresponding to key.

Well, the redis deletion strategy we are talking about today is the deletion of this part of the data. Scheduled deletion

Timing deletion is to write a timer, and then when the key time expires, the timer task immediately deletes the expired key.

Advantages: it is conceivable that key will be deleted when it expires, which must be the most friendly to memory and save memory.

Cons: the single-threaded nature of redis is that all commands are executed in a certain order. The pressure to delete cpu when the key value expires will increase, which will directly affect the response time of the redis server and IO.

To delete regularly is to trade time for space.

When the scheduled deletion is performed, the data corresponding to the key value will be deleted, and it will also be deleted directly in the expired memory area.

Lazy deletion

In this picture, when the key value expires, it will not be deleted directly, so when will it be deleted? Keep looking down.

When we use lazy deletion, the data will not be deleted automatically when it expires, so the way he deletes it is that the next time he gets the key value, he will make a judgment to determine whether the key has expired, and delete will be performed if it expires.

That is to say, when get name is executed again, a function expirelfNeeded () is used to determine whether the key is out of date. Nil is returned for expired ones, and then deleted from memory.

Pros: it reduces some CPU performance, and only deletes it when you have to delete it.

Disadvantages: it must be that there is a lot of memory pressure, for example, some hot news, after the hot spots, almost no one visits, no one accesses this key, and it will occupy a certain amount of memory space for a long time.

That is to say, this way is to trade space for time.

Delete periodically

In the above, we mentioned two kinds of deletion methods, one is regular deletion, the other is lazy deletion. One is to trade space for time. One is to trade time for space. Both options are more extreme. So let's take a look at the implementation that is deleted on a regular basis.

First, let's take a look at the storage space of redis. There are 16 storage spaces by default. There is a configuration parameter database in redis.conf that is controlled by this parameter. Each database has its own expired partition, in which storage is the data address and data expiration time.

Mode of realization

When redis starts, it takes the value of reading hz under server, which defaults to 10. This value can be viewed directly in the terminal using info server.

Then hz under server will be executed every second for serverCron () polling.

Continue to use databasesCron to access information one by one from redis's 16 libraries

During the visit, activeExpireCycel is executed to detect each expires [*] one by one, and the time of each execution is the parameter 250ms / server hz.

When each expirs [*] is tested one by one, ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC will be randomly selected for testing.

If key times out, directly delete the number of key deleted in the key round > ACTIVE_EXPIRE_CYCLE_SLOW_TIME_PERC*25% continue to cycle the process if the number of key deleted in the round

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