Redis underlying dictionary
Redis design and implementation , read a book to learn knowledge, take notes, deepen the understanding of knowledge.
A dictionary is a data structure in Redis. In the dictionary, a key corresponds to a value, which is called a key-value pair (the dictionary has multiple key-value pairs). The bottom layer of Redis is developed by C language, and there is no dictionary itself, so Redis encapsulates the data structure of a dictionary.
The bottom layer of the dictionary is implemented by the hash table, so the overall order should be introduced in descending order.
Dictionary data structure:
- Type Type specific function
- Private Private data
- Ht  a hash table
- Rehashidx is -1 if the rehash is not in progress
The structure of a hash table includes:
- Table Hash table array
- Size Size of the hash table
- Sizemask Specifies the sizemask of the hash table, used to calculate index values. Sizemask = size-1
- Used Indicates the used node of the hash table
Hash table node data structure:
- The key key
- V value
- Next Indicates the address of the next node
As shown below:
When we add a key-value pair to the dictionary, we pass in the key and hash it according to the algorithm (MurmurHash2), and then perform the ampersand operation with Sizemask to calculate the added index.
As the operation continues, the number of keys stored in the hash table increases or decreases. In order to keep the load factor within a reasonable range, rehash is performed. Any condition is automatically rehash:
- The server does not run the BGSAVE or BGREWRITEAOF command and the load factor is greater than or equal to 1.
- The server runs the BGSAVE or BGREWRITEAOF command when the load factor is greater than or equal to 5.
- Rehash is performed when the load factor is less than 0.1.
Load factor: the number of nodes the hash table holds/the size of the hash table.
Rehash it is progressive because if ht has too much data, direct transfer to HT  will result in a period of service interruption.
A progressive process:
- If ht is allocated and rehashidx is set to 0, hash is being performed
- During rehash, dictionaries are added, deleted, and modified, and h is also modified once, rehashidx + 1 is modified once
- As the dictionary continues to operate, eventually the key-value pair of H  is migrated to H , and rehashidx is changed to -1 to end the rehash.
Leave a summary of the book at the end
Refueling efforts to point out any problems and timely correction.