Note: This article mainly refers to the design and implementation of Redis
1. Database structure
Each REDIS server internal data structure is a redisdb[], the size of the array can be configured in redis.conf ("Database 16", the default is 16), and all our cache operations (Set/hset/get, etc.) are in the Redisdb [] on one of the Redisdb (libraries), the REDISDB default is redisdb[0].
Note :
- The next operation can be selected via "Select 1" on redisdb[1]
- In actual use, we only operate on redisdb[0] , because
- Redis does not get the function that is currently operating on which Redisdb, so it is easy to select multiple times, we do not know which library, and since it is only on the redisdb[0] operation, then "database" can be set to 1,
- After this parameter is set to 1, not only can the original memory of other redisdb to Redisdb[0], in the "periodic delete" policy, we also only scan a redisdb on it.
"Delete periodically" see Nineth. Redis Expiration Policy
2. Principle of reading and writing
In each redisdb, a dict (dictionary) is used to store "Key-value".
Example:
Assume that the following four commands are executed in Redis and no select is executed, that is, the default choice is to operate on redisdb[0]
Set msg "Hello Nana"
Rpush mylist "A" "B" "C"
Hset Book name "Lover"
Hset book author "Nana"
The storage structure is as follows:
3. Maintenance work performed while reading and writing
After reading a key (read-write operation requires reading key),
- Server update cache Hit count and number of misses
- Update the last use time of the key
- Detects if the key is out of date (see Chapter Nineth Redis Expiration policy for details)
- Write counter +1 for persistence
Eighth. Redis database structure and read-write principle