Home  >  Article  >  System Tutorial  >  Detailed explanation of the internal working mechanism of Redis

Detailed explanation of the internal working mechanism of Redis

WBOY
WBOYforward
2024-03-10 09:13:09449browse

Detailed explanation of the internal working mechanism of Redis

Redis database(How Redis represents a database and how database operations are implemented)

When the Redis server is initialized, redis.h/REDIS_DEFAULT_DBNUM (hereinafter abbreviated as N) databases will be created, and the database IDs are from 0 to N-1. All databases are saved in the redis.h/redisServer.db array. .

On the client side, you can switch through the "SELECT" command, where the program directly uses redis.h/redisServer.db[number] to switch. However, some internal programs, such as AOF programs, replication programs and RDB programs, need to know the number of the current database. If there is no id field, the program can only point to the currently used database and all databases in the redisServer.db array. Compare the pointers to figure out which database you are using.

Redis database structure:
typeof  struct   redisDb{
int id ;   // 数据库的id

dict *dict ; // 保存着该数据库的所有键值对 也被称为键空间

dict *expires ; // 保存着键的过期时间

…..

} redisDb ;
 Redis 是一个键值对 字典表,同样Redis数据库存储形式也是键值对 字典表
 键是字符串
 值可以是字符型、list 列表、 hash、集合以及 有序集合其中之一
Key space operations for Redis database operations such as adding, deleting, modifying, and querying:

New:
Redis will add a key-value pair to the key space dictionary, where the key is a string and the value is any value type.
delete:
Redis will delete the key-value pair of the corresponding key in the key space dictionary
renew:
Redis will release the value object corresponding to the previous key in the key space dictionary and let the key point to the new value object
Inquire:
Redis will query the value object of the corresponding key in the key space dictionary:
The key does not exist, return NULL
The key exists and is of the correct type, returning the correct value
Key exists but is of incorrect type, return type is wrong
Other operations:
In addition to the key value operations shown above, there are many commands for the database itself, which are also completed by processing the key space:
FLUSHDB Delete all key-value pairs in the keyspace
RANDOMKEY Returns a random key from the key space
DBSIZE Returns the number of key-value pairs in the key space
Exists check whether the given key is existed in the key space
RENAME In the key space, rename the given key

Key expiration time

In the Redis database, the expiration time of all keys is stored in the expires dictionary of the RedisDb structure. The key is a pointer to a key in the dict dictionary (key space), and the value is the modified expiration time. Use long long type representation.

Redis has four commands to set the key's survival time (how long it can survive) and expiration time (when it expires):
EXPIRE sets the key's lifetime in seconds;
PEXPIRE sets the key lifetime in milliseconds;
EXPIREAT sets the expiration UNIX timestamp of the key in seconds;
PEXPIREAT Sets the key's expiration UNIX timestamp in milliseconds.

Although there are so many different units and different forms of setting methods, the value of the expires dictionary only saves the "expired UNIX timestamp in milliseconds". That is to say, through conversion, the effects of all commands are finally the same. The effect is the same as the PEXPIREAT command.

Clearing expired keys

定时清除:
在创建KEY的时候创建一个定时任务,在KEY到期时定时任务会被触发,第一时间清除过期KEY。
此种操作对内存最友好,不会有垃圾数据占用内存情况存在
缺点是会造成很大的服务器负载,特别是CPU负载高的时候,CPU很大一部分负载用在了删除不必要的KEY上了

惰性清除:
放任键空间的键不管,每次查询KEY的时候先去校验KEY是否过期,过期则删除,不过期则正常返回相应的VALUE。
此种操作对CPU最友好,这种策略仅限于当前KEY,相关不必要的KEY不会造成CPU负载
缺点是:容易造成内存空间浪费,特别是当系统中存在大量过期KEY且很少被用到,这十分影响非常依赖于内存大小Redis的性能

定期删除:
由定时脚本cron定时对expires的键扫描判断是否有过期的KEY存在,如存在,将其删除掉。
这是一种折中方案,既不会过多消耗CPU,又可以定时清楚惰性删除忽略到的不必要的内存消耗

Redis采用的“惰性清除”和“定期清楚”相结合的方式,其中定期删除模式是在规定的时间限制内,尽 可能地遍历各个数据库的 expires 字典,随机地检查一部分键的过期时间,并删除其中的过期键。

伪代码如下:

def activeExpireCycle():
# 遍历数据库(不一定能全部都遍历完,看时间是否足够)
for db in server.db:
# MAX_KEY_PER_DB 是一个 DB 最大能处理的 key 个数 # 它保证时间不会全部用在个别的 DB 上(避免饥饿) i=0                                    while (i 
<p>Redis 过期键删除的主从同步问题(Redis的机制是由主节点统一控制)</p>
<p>如果服务器是主节点,当它删除一个过期键之后,会显式的向所有附属节点发送一条DEL命令<br>
如果服务器是附属节点,当它判断到当前KEY已经过期,会将该键过期的消息发送给主服务器,主服务器删除后向所有的从服务器节点发送DEL命令。</p>
<p>从服务器节点不自主的对键进行删除是为了保持和主服务器数据的绝对一致性,即当一个过期键还存在主服务器上,这个键在所有的从服务器上也不会被删除。</p>

The above is the detailed content of Detailed explanation of the internal working mechanism of Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete