Redis skip list (skiplist) knowledge points detailed explanation

foreword

For relevant knowledge points about Redis, please refer to my previous articles:

  1. Redis framework from entry to learning (full)
  2. Python operation Redis from entry to proficiency with code (full)
  3. Redis common interview questions (full)
  4. Read the Amazon MemoryDB database based on Redis in one article

The underlying data structure of the Redis data type under popular science (common test points)

type of data value that can be stored operate Application Scenario
string String, Integer or Float Perform operations on the entire string or a part of the string, perform increment or decrement operations on integers and floating-point numbers Do simple key-value cache, counter, shared session and speed limit
list the list Push or pop elements from both ends, trim single or multiple elements, and keep only elements within a range Store some list-type data structures, such as fan lists, article comment lists, etc.
set unordered collection Add, get, remove a single element, check if an element exists in a collection. Compute intersection, union, and difference. Get random elements from a collection Intersection, union, and difference operations, such as intersection, can integrate the fan lists of two people, or the user's favorite tags, etc.
hash An unordered hash containing key-value pairs Add, get, remove a single key-value pair. Get all key-value pairs. Check if a key exists structured data, such as an object
check ordered set Add, get, delete elements. Get elements by score range or member. Computes the rank of a key. Deduplication but can be sorted, such as getting the top users, leaderboards, etc.

1. The underlying structure of zset

zset is mainly an ordered collection

The underlying structure has two structures: compressed list and skip list

data structure describe advantage shortcoming Application Scenario
compressed list The list length, tail offset, and number of list elements are added to the front of the list, and the end of the list is added to the end of the list Conducive to quickly find the first and last nodes It is not conducive to finding other elements, and can only traverse one by one 1. The number of elements stored in the ordered collection is less than 128
2. The length of all elements stored in the ordered collection is less than 64 bytes
jump table On the basis of the linked list, a multi-level index is added to quickly find elements through the jump of the multi-level index position You can quickly find the corresponding elements, use space for practice, and the complexity of practice laughs A string with a large number of elements or a relatively long element

2. Skip table structure

The reason why there is a skip table structure is to solve the complex problem of balancing a binary tree

Hash can be quickly located, but it is not ordered.
If you use a binary search tree, if it is ordered, it will degenerate into a linked
list. Add a balance factor to the linked list to become a balanced binary tree (can be divided into b-tree, b+ tree, red-black tree, etc.)

The difference between skip list and balanced binary tree is as follows:

common ground difference
The time complexity of searching, inserting and deleting is O(logn) It is faster to look up interval elements in a jump table, but it is time-consuming to find the interval elements in a balanced binary tree at the beginning by locating the left endpoint.

Finally, the jump table was chosen because the jump table is easy to implement, so it is used to realize the ordered collection

Basic properties:

  1. It is mainly implemented in the form of single-linked list + index, in the form of exchanging space for time to improve the search speed

  2. The bottom layer mainly consists of two structures

insert image description here

  • zskiplist: save the skip table information (head and tail nodes, the number of layers with the largest number of skip table layers, length)
  • zkiplistnode: Save the skip table node information (for example, level layer: each layer has two attributes, pointer and span, and the span is large and long distance)

Similar to the following structure, it was originally a linked list structure from 1 to 6. If you are looking for the number 5, you need to traverse 5 nodes.
If you build an index every other number, the details are as follows:
insert image description here

Reduce the number of traversal nodes through the first-level index
If you think the first-level index is slow, you can build an index on the basis of the first-level index

When the amount of data is relatively large, the complexity of skip table query, insertion, and deletion is O(logn), and the main idea itself is dichotomy

3. Extend your thinking

Why does zset use jump list instead of red-black tree or binary tree?

Redis directly operates memory instead of disk, so it does not need to read IO, and the speed is faster

  • The jump table does not take up much memory (basically depends on the node data), and the data parameters of the node itself will make the memory density less than that of the B+ tree
  • The implementation of the jump table itself is simpler than other structures (storing nodes of the same level in the same file or map is beneficial to solve the problem of cache locality), and MYSQL uses B+ trees to reduce IO
    and support range queries

The explanation of this issue is relatively clear in combination with the underlying structure of MYSQL.
(The corresponding database finally uses the b+ tree, you can see the reasoning of my article: Detailed analysis of the underlying principles of Mysql + common interview questions (full) )

Guess you like

Origin blog.csdn.net/weixin_47872288/article/details/127533073