Balanced Trees: Why Redis Internal Implementations Use Jump Tables

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 zaddcommand 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 zskiplistrepresented by a structure, which zskiplistcontains 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 zskipliststructure, which contains the following properties:

  • headerAttributes: Pointer to the header jump table node.
  • tailAttribute: Pointer to the tail jump table node.
  • levelAttribute: 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.
  • lengthAttribute: Indicates the total number of nodes in the jump table.

Structure of skip table nodes

The skip table node zskiplistNodeis represented by a structure, which contains the following properties:

  • levelAttribute: An array representing the layer, each item in the array is zskiplistLevelrepresented by a structure, which contains the following two attributes:
    • forwardAttributes: Pointers to other nodes in the direction of the end of the list.
    • spanAttribute: forwardHow many nodes span from the current node to the pointed node.
  • backwardProperties: A pointer to the previous node of the current node.
  • objAttribute: A pointer to a member of the element.
  • scoreAttribute: 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:

Manneko Gakusha

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~

Guess you like

Origin juejin.im/post/7078487936836059143