This article is shared from the HUAWEI CLOUD community " 5 minutes to understand the internal implementation of Redis skiplist (skiplist) ", author: Wanmao Society.
A skiplist is an ordered data structure that maintains pointers to subsequent nodes at different levels at each node to achieve the purpose of quickly accessing a specified node. The average time complexity of the jump table to find the specified node is O(log n), and the worst time complexity is O(N).
Redis uses skiplists as one of the underlying implementations of sorted sets (zsets). When the number of elements of the sorted set is greater than or equal to zset-max-ziplist-entries
(default is 128), or the length of each element member is greater than or equal to zset-max-ziplist-value
(default is 64 bytes), use skip table and hash table as the internal implementation of sorted set .
As an example, we use the zadd
command to create an ordered collection implemented as a skip table:
127.0.0.1:6379> zadd one-more-zset 1 long-long-long-long-long-long-long-long-long-long-long-long-long-long
(integer) 1
127.0.0.1:6379> zrange one-more-zset 0 -1
1) "long-long-long-long-long-long-long-long-long-long-long-long-long-long"
127.0.0.1:6379> object encoding one-more-zset
"skiplist"
复制代码
Implementation of skip table
The jump table in Redis is zskiplist
represented by a structure, which zskiplist
contains a doubly linked list composed of multiple jump table nodes, each of which holds element members and corresponding minutes. Let's take a look at each one in detail.
zskiplist structure
The skip table is represented by a zskiplist
structure, which contains the following properties:
header
Attributes: Pointer to the header jump table node.tail
Attribute: Pointer to the tail jump table node.level
Attribute: Indicates the layer number of the node with the largest layer number in the jump table, and the layer number of the header node is not counted.length
Attribute: Indicates the total number of nodes in the jump table.
Structure of skip table nodes
The skip table node zskiplistNode
is represented by a structure, which contains the following properties:
level
Attribute: An array representing the layer, each item in the array iszskiplistLevel
represented by a structure, which contains the following two attributes:-
forward
Attributes: Pointers to other nodes in the direction of the end of the list.
-
span
Attribute:forward
How many nodes span from the current node to the pointed node.
backward
Properties: A pointer to the previous node of the current node.obj
Attribute: A pointer to a member of the element.score
Attribute: The score corresponding to the current element member.
Graphical skip table
Having said so much, it is relatively abstract and difficult to understand. Let's take an example:
This is the internal structure of a jump table, which has 4 elements, the keys are: Wan, cat, school, society.
Why not use a balanced tree?
The skip table stores elements in a hierarchical linked list in an ordered manner. In most cases, the efficiency of the skip table is comparable to that of a balanced tree, and operations such as lookups, deletions, and additions can be completed in logarithmic expected time, and Compared to a balanced tree, the implementation of a skip table is much simpler and more intuitive. So instead of using a balanced tree in Redis, a skip table is used.
Click Follow to learn about HUAWEI CLOUD's new technologies for the first time~