Redis's main data structure linked list Redis uses a C language that does not have a built-in data structure, so Redis has built its own linked list implementation. One of the underlying implementations of the list key is the linked list. A list key contains a large number of elements, and all the elements in the list are long strings, redis uses the linked list as the underlying implementation of the list key. Except chain
Redis's main data structure linked list Redis uses a C language that does not have a built-in data structure, so Redis has built its own linked list implementation. One of the underlying implementations of the list key is the linked list. A list key contains a large number of elements, and all the elements in the list are long strings, redis uses the linked list as the underlying implementation of the list key. Except chain
Redis main Data Structure
Linked List
Redis uses a C language that does not have a built-in data structure, so Redis has built its own linked list implementation. One of the underlying implementations of the list key is the linked list. A list key contains a large number of elements, and all the elements in the list are long strings, redis uses the linked list as the underlying implementation of the list key. In addition to the linked list key, the Redis server uses the linked list to store the status information of multiple clients and uses the linked list to construct the client output buffer.
Eg: redis> LLEN integers
(Integer) 1024
The integers list key contains a total of 1024 integers from 1 to 1024. The underlying implementation of the integers list key is a linked list. each node in the linked list stores an integer.
Each linked list node is represented by a listNode structure. Each node has a pointer to the front node and the back node. The Linked List Implementation of Redis is a double-ended linked list.
Each linked list is represented by a list structure with the header node pointer, table tail node pointer, and chain table length.
Because both the front node of the table header node and the back node of the table end node point to NULL, the Linked List Implementation of Redis is a non-circular linked list.
Dictionary
Dictionary, symbol table, or ing, stores the abstract data structure of key-value pairs
Redis built its own dictionary. The dictionary uses a hash table as the underlying implementation. Each dictionary has two hash tables, one for normal use and the other for rehash only. A hash table can contain multiple hash table nodes, and each hash table node stores a key-value pair in the dictionary. Redis uses the MurmurHash2 algorithm to calculate the hash value of the key.
Two or more keys are allocated to the same index of the hash table array. These keys conflict. The hash table of Redis uses the link address method to resolve key conflicts.
As operations continue, the key-value pairs stored in the hash table gradually increase or decrease. In order to keep the load factor of the hash table within a reasonable range, you can perform the rehash (re-hash) operation to expand or contract a hash table. You need to rehash all key-value pairs contained in the existing hash table to the new hash table, in addition, the rehash process is not completed in one time, but progressively completed.
Skip table
A skip table is an ordered data structure. It maintains multiple pointers to other nodes in each node to quickly access the node.
Redis> ZRANGE fruit-price 0 2 WITHSCORES
All data in the fruit-price ordered set is stored in a hop table, and each hop table node stores the price information of a fruit, all fruits are sorted in the jump table from low to high by price.
Redis uses a jump table as one of the underlying implementations of the sorted set key. If an ordered set contains a large number of elements, or the element members in an ordered set are long strings, redis uses the jump table as the underlying implementation of the sorted set key. Redis only uses the Skip table in two places. One is to implement the ordered set key, and the other is to use it as the internal data structure in the cluster node.
Redis's hop table implementation consists of two structures: zskiplist and zskiplistNode. zskiplist is used to save the hop table information (such as the header node, table tail node, and length ), zskiplistNode indicates the Skip table node.
Integer Set
An integer set is one of the underlying implementations of a set key. When a set contains only integer elements and the number of elements in the set is small, Redis uses the integer set as the underlying implementation of the set key.
Redis> SADD numbers 1 3 5 7 9
The compressed list is a sequential data structure developed to save memory.
Redis> RPUSH 1st 1 3 5 10086 "hello" "world"
The compressed list is a sequential data structure consisting of a series of specially encoded continuous memory blocks.
The compressed list is used as one of the underlying implementations of the list key and hash key.
The compressed list can contain multiple nodes. Each node can save an array of bytes or an integer.
Adding a new node to the compression list, or deleting a node from the compression list, may lead to a chained update operation, but the probability of this operation is not high.