Balanced Trees: Why Redis Internal Implementations Use Jump Tables

Summary: Redis uses skiplists as one of the underlying implementations of sorted sets (zsets).

This article is shared from the HUAWEI CLOUD community " 5 minutes to understand the internal implementation of Redis skiplist (skiplist) ", author: Wanmao Society.

Introduction to the Jump Table

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. When the skip table finds the specified node, the average time complexity is , 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 tables and hash tables as internal implementations of sorted sets.

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 skip table in Redis is represented by the zskiplist structure. The zskiplist structure contains a doubly linked list composed of multiple skip table nodes. Each skip table node holds the element member and the corresponding minute. Let's take a look at each one in detail.

zskiplist structure

The skip list is represented by the zskiplist structure, which contains the following properties:

  • header attribute: pointer to the header jump table node.
  • tail attribute: pointer to the tail jump table node.
  • level attribute: Indicates the level of the node with the largest level in the jump table, and the level of the header node is not included.
  • length property: Indicates the total number of nodes in the jump table.

Structure of skip table nodes

Skiplist nodes are represented by the zskiplistNode structure, which contains the following properties:

  • level attribute: an array representing the layer, each item in the array is represented by the zskiplistLevel structure, which contains the following two attributes:
    • forward property: Pointer to other nodes in the direction of the end of the table.
    • span attribute: how many nodes span from the current node to the node pointed to by forward.
  • backward property: A pointer to the previous node of the current node.
  • obj property: 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~

{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326545403&siteId=291194637