编程知识 cdmana.com

Redis design and Implementation: Database

The analysis of this paper is based on Redis 6.0 Version source code
redis 6.0 Source code :https://github.com/redis/redis/tree/6.0

Database in server

Redis The server keeps most of the information in server.h/redisServer.redis The data is stored in redisServer Medium redisDb In structure .

struct redisServer {
    // ...
    redisDb *db; //  Database list 
    // ...
    int dbnum;   //  Number of databases 
    // ...
}
  • db Each of them redisDb Structure represents a database .
  • When initializing the server , The program will depend on the state of the server dbnum Property to determine how many databases should be created .
  • dbnum The value of the property is configured by the server database The choice decides , By default , The value of this option is 16, therefore Redis By default, the server creates 16 A database .

Database key space

Redis Is a key value to the database server , Each database in the server consists of a server.h/redisDb Structural representation .
among ,redisDb Of dict The dictionary property holds all key value pairs in the database , We call this dictionary key space (key space):

typedef struct redisDb {
    dict *dict; 
    // ...
} redisDb;

dict The data in is one-to-one corresponding to the key value pairs we usually operate :

  • dict Of key It's in the database key, String type
  • dict Of value It's in the database value , This value could be zero stringhashzsetsetlist Any one of them

Example

If we're in the database , Execute the following command :

redis > SET str_key str_value
OK
redis > RPUSH list_key a b c
(integer) 3

Two new ones key The structure of is shown in the figure below :

RedisDb  Structure example

It's clear from the example above that Redis How data is organized , Adding, deleting, modifying and checking means to dict The operation of , I won't go into details here .

Key The expiration time of

1. data structure

redisDb Medium expires Property holds all of key The expiration time of , Let's call it Out of date Dictionary Well .

  • Keys in expired dictionaries , It's a pointer , Pointing to real data key, It won't waste space. Save it one more time
  • Values in expired dictionaries , What we save is the specific expiration time point , Timestamps accurate to milliseconds
typedef struct redisDb {
    // ...
    //  Save all  key  The expiration time of 
    dict *expires; 
    // ...
} redisDb;

command TTLPTTL It's all about looking up the expiration date of this expired dictionary , Then subtract the current time , What you get is the time left .

2. Be overdue key Delete policy for

One key When the expiration time is up , How to delete it ?Redis Two strategies are used : Lazy deletion 、 Delete periodically

Lazy deletion

Lazy deletion strategy refers to :key After expiration , No immediate deletion of , It's reading and writing key When , That's right key To delete .
The code is implemented in db.c/expireIfNeeded In the method . all key Before reading and writing , Will call first expireIfNeeded Yes key Inspection , If expired , Delete .

Delete periodically

The periodic deletion policy refers to :Redis Every once in a while , Take a certain amount of from the database at random key Inspection , If expired , Then delete .
The code is implemented in expire.c/activeExpireCycle In the method .

版权声明
本文为[Xiaoxin is also]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201225111218586y.html

Scroll to Top