Redis learning notes (VII) database


The redis server saves all databases in the DB array of the redisserver structure in the server state. Each item of the DB array is a redisdb:

struct redisServer{
  //An array holds all databases in the server
  redisDb *db;
  //Number of databases
  int dbnum;

Dbnum: during server initialization, the program determines how many databases should be created according to dbnum, which is determined by the database option configured by the server. The default is 16

In the server, the DB attribute of the redisclient structure of the client state records the current target database of the client. This attribute points to the pointer of the redisdb structure:

typedef struct redisClient{
  //Records the database currently in use by the client
  redisDb *db;
} redisClient;

The redisclient pointer points to one of the elements of the redisserver array, and the pointed element is the target database of the client.





The dict location of redisbd structure stores all key value pairs in the database. We call this dictionary key space.

The key of the key space is the key of the database. Each key is a string object.

The value of the key space is also the value of the database. Each value can be any redis object among string objects, list objects, hash table objects, collection objects and ordered collection objects.


When the redis command reads and writes the database, the server not only performs the specified read and write operations on the key, but also performs some additional maintenance work:

1. After reading a key, the server updates the number of server keyspace hits or keyspace misses based on whether the key exists.

2. After reading a key, the server will update the LRU (last use time) of the key, which is used to calculate the idle time of the key.

3. If the server reads a key and finds that the key has expired, the server will delete the expired key first and then perform the remaining operations.

4. If the client uses the watch command to monitor a key, the server will mark the monitored key as dirty after modifying it, so that the transaction program can notice that the key has been modified.

5. Every time the server modifies a key, it will add one to the value of the dirty key counter, which will trigger the persistence and assignment operations of the server.

6. If the server turns on the database notification function, the server will send the corresponding database notification according to the configuration after modifying the key.


Set expiration time

The command export key TTL sets the key lifetime to TTL seconds

Command pexpire   Key TTL sets the key lifetime to TTL milliseconds

Command expireat key timestamp command sets the expiration time of the key to timestamp seconds timestamp

The command pexpireat key timestamp sets the expiration time of the key to the millisecond timestamp specified by timestamp

1. The express command can be converted to the pexpire command

def EXPIRE(key,ttl_in_sec);
ttl_in_ms = sec_to_ms(ttl_in_sec)

2. Convert pexpire command to pexpireat command

def PEXPIRE(key,ttl_in_ms)
now_ms = get_current_unix_timestamp_in_ms();

3. Convert the express command to the pexpireat command

def EXPIREAT(key,expire_time_in_asc)
expire_time_in_ms = sec_to_ms(expire_time_in_sec)

The expires Dictionary of redisdb structure stores the expiration time of the key where the database is located (expiration Dictionary),

1. An expired dictionary key is a pointer to a key object in the key space.

2. The value of the expiration dictionary is an integer of type long (UNIX timestamp with millisecond precision).


Expiration deletion policy

1. Scheduled deletion: while setting the expiration time, create a timer and delete it immediately upon expiration (memory friendly, CPU unfriendly).

2. Inert deletion. In the next query, query whether it expires and delete it after expiration (memory unfriendly, CPU friendly).

3. It is deleted regularly and executed at regular intervals.

Redis’s deletion strategy uses two types: lazy deletion and periodic deletion.


When the save or bgsave command is executed to generate an RDB file, the program will check the keys in the database, and the expired keys will not be saved to the newly created RDB file. Therefore, the inclusion of expired keys in the database will not affect the newly generated RDB file.


When loading RDB files, if the server runs in the master server mode, the key will be checked when loading RDB files. The unexpired key will be loaded into the database, and the expired key will be ignored. When running in slave server mode, all keys saved in the file are loaded. When master-slave synchronization, the expired keys of the slave server are cleared.


When writing an AOF file, if the expiration key is not cleared, the AOF file will not be affected by the expiration key. After the expiration key is deleted, the program will add a del command to the AOF file to display the record that the key has been deleted.


When AOF is rewritten, the program will check the keys in the database, and the expired keys will not be saved to the rewritten AOF file.


When the server is in replication mode, the expiration key of the server is controlled by the master server: after deleting the expiration key, the master server will send a del command to the slave server. Before the slave service receives the command, the client’s read command will be processed like the unexpired key. The expired key will not be deleted until the Del command is received.


Learn a little every day, there will always be gains.


Note: respect the author’s intellectual property rights. Refer to redis design and Implementation for the content in this article. Just learn here and share with you.


Recommended Today

A detailed explanation of the differences between Perl and strawberry Perl and ActivePerl

Perl is the abbreviation of practical extraction and report language “practical report extraction language”. Application of activestateperl and strawberry PERL on Windows platformcompiler。 Perl   The relationship between the latter two is that C language and Linux system have their own GCC. The biggest difference between activestate Perl and strawberry Perl is that strawberry Perl […]