The following column Redis Tutorial will give you a detailed explanation of the jump table of the Redis data structure. I hope it will be helpful to friends in need!
Preface
The jump list is an ordered data structure that maintains multiple pointers to other nodes in each node. To achieve the purpose of quickly accessing nodes. In this way, it may be difficult for us to understand, we can first recall the linked list.
1. Review jump table
1.1 What is a jump table
For a singly linked list, even if the data stored in the linked list is ordered, if we want To find certain data in it, you can only traverse the linked list from beginning to end. In this way, the search efficiency will be very low, and the time complexity will be very high, which is O(n).
If we want to improve the search efficiency, we can consider building an index on the linked list. Extract one node from every two nodes to the previous level, and we call the extracted level the index.
At this time, we assume that we want to find node 8. We can traverse in the index layer first. When we traverse to the node with a value of 7 in the index layer, we find that the next node is 9, then we need to The node 8 being searched for must be between these two nodes. We descended to the linked list level and continued traversing to find the node 8. Originally, we needed to traverse 8 nodes to find node 8 in a singly linked list, but now with the first-level index, we only need to traverse five nodes.
From this example, we can see that after adding a layer of index, the number of nodes that need to be traversed to find a node is reduced, which means that the search efficiency is improved. For the same reason, add another level. index.
We can see from the picture that the search efficiency has improved again. In our example, we have very little data. When there is a large amount of data, we can add multi-level indexes, and the search efficiency can be significantly improved.
A structure like this linked list plus multi-level index is a jump list!
2. Redis jump table
Redis uses jump table as one of the underlying implementations of ordered set keys. If an ordered set contains a large number of elements, or when the member of the element in the ordered set is a relatively long string , Redis will use a jump table as the underlying implementation of the ordered set key.
Here we need to think about a question - why does Redis use a jump table to implement it when there are a large number of elements or the members are relatively long strings?
From the above we can know that the jump list adds a multi-level index to the linked list to improve the efficiency of search, but it is a space-for-time solution, which will inevitably bring about a problem - the index is It takes up memory. The original linked list may store very large objects, but the index node only needs to store key values and a few pointers, and does not need to store objects. Therefore, when the node itself is relatively large or the number of elements is relatively large, its advantage is It will inevitably be magnified, while the shortcomings can be ignored.
2.1 Implementation of skip table in Redis
The skip table of Redis is defined by two structures, Detailed explanation of jump table of Redis data structure and skiplist. The Detailed explanation of jump table of Redis data structure structure is used to represent the skip table node, and the zskiplist structure is used to save the jump. Information related to table nodes, such as the number of nodes, pointers to the head node and tail node, etc.
The above figure shows an example of a skip list. The leftmost one is the skiplist structure, which contains the following attributes.
header: points to the header node of the jump table. The time complexity of locating the header node through this pointer program is O(1)
tail: Points to the tail node of the jump table. The time complexity of locating the tail node of the table through this pointer program is O(1)
level: Record the current jump table, The number of layers of the node with the largest number of layers (the number of layers of the header node is not included). Through this attribute, the number of layers of the node with the best layer height can be obtained in O(1) time complexity.
-
length: Record the length of the jump table, that is, the number of nodes currently contained in the jump table (head nodes are not included). Through this attribute, the program can be O(1) Returns the length of the jump list in time complexity.
On the right side of the structure are four Detailed explanation of jump table of Redis data structure structures, which contain the following attributes
- ## Level (level): Use 1, 2 in the nodes , L3 and other words mark each layer of the node, L1 represents the first layer, L represents the second layer, and so on. Each layer has two attributes: forward pointer and span. The forward pointer is used to access other nodes located at the end of the table, while the span records the distance between the node pointed by the forward pointer and the current node (the larger the span, the farther the distance). In the picture above, the arrow with a number on the connecting line represents the forward pointer, and that number is the span. When the program traverses from the beginning of the table to the end of the table, access will proceed along the forward pointer of the layer. Every time a new jump table node is created, the program randomly generates a value between 1 and 32 as the level based on the power law (powerlaw, the larger the number, the smaller the probability of occurrence) The size of the array, this size is the "height" of the layer.
- Backward pointer: The backward pointer of the node marked with BW in the node points to the previous node of the current node. The back pointer is used when the program traverses from the end of the table to the beginning. The difference with the forward pointer is that each node has only one backward pointer, so it can only move backward one node at a time.
- Score: 1.0, 2.0 and 3.0 in each node are the scores saved by the node. In the jump table, nodes are arranged from small to large according to their saved scores.
- Member object (oj): o1, o2 and o3 in each node are the member objects saved by the node. In the same jump table, the member objects saved by each node must be unique, but the scores saved by multiple nodes can be the same: nodes with the same score will be sorted according to the size of the member objects in lexicographic order. , nodes with smaller member objects will be arranged in the front (direction closer to the head of the table), while nodes with larger member objects will be arranged in the back (direction closer to the end of the table).
Time complexity | |
---|---|
O(1) | |
O(N) | |
Average O (logN), worst case O(logN) (N is the length of the skip list) | |
The average is O(logN), the worst is O(logN) (N is the length of the jump table) | |
Average O(logN), worst O(logN) (N is the length of the jump table) | |
Average O(logN), worst O(logN) (N is the length of the jump list) | |
O(1) | |
Average O( logN), worst O(logN) (N is the length of the jump table) | |
Average O(logN), worst O(logN) (N is the length of the jump list) | |
O(N), N is the number of nodes to be divided | |
O(N), N is the number of nodes to be removed. |
The above is the detailed content of Detailed explanation of jump table of Redis data structure. For more information, please follow other related articles on the PHP Chinese website!

Redis plays a key role in data storage and management, and has become the core of modern applications through its multiple data structures and persistence mechanisms. 1) Redis supports data structures such as strings, lists, collections, ordered collections and hash tables, and is suitable for cache and complex business logic. 2) Through two persistence methods, RDB and AOF, Redis ensures reliable storage and rapid recovery of data.

Redis is a NoSQL database suitable for efficient storage and access of large-scale data. 1.Redis is an open source memory data structure storage system that supports multiple data structures. 2. It provides extremely fast read and write speeds, suitable for caching, session management, etc. 3.Redis supports persistence and ensures data security through RDB and AOF. 4. Usage examples include basic key-value pair operations and advanced collection deduplication functions. 5. Common errors include connection problems, data type mismatch and memory overflow, so you need to pay attention to debugging. 6. Performance optimization suggestions include selecting the appropriate data structure and setting up memory elimination strategies.

The applications of Redis in the real world include: 1. As a cache system, accelerate database query, 2. To store the session data of web applications, 3. To implement real-time rankings, 4. To simplify message delivery as a message queue. Redis's versatility and high performance make it shine in these scenarios.

Redis stands out because of its high speed, versatility and rich data structure. 1) Redis supports data structures such as strings, lists, collections, hashs and ordered collections. 2) It stores data through memory and supports RDB and AOF persistence. 3) Starting from Redis 6.0, multi-threaded I/O operations have been introduced, which has improved performance in high concurrency scenarios.

RedisisclassifiedasaNoSQLdatabasebecauseitusesakey-valuedatamodelinsteadofthetraditionalrelationaldatabasemodel.Itoffersspeedandflexibility,makingitidealforreal-timeapplicationsandcaching,butitmaynotbesuitableforscenariosrequiringstrictdataintegrityo

Redis improves application performance and scalability by caching data, implementing distributed locking and data persistence. 1) Cache data: Use Redis to cache frequently accessed data to improve data access speed. 2) Distributed lock: Use Redis to implement distributed locks to ensure the security of operation in a distributed environment. 3) Data persistence: Ensure data security through RDB and AOF mechanisms to prevent data loss.

Redis's data model and structure include five main types: 1. String: used to store text or binary data, and supports atomic operations. 2. List: Ordered elements collection, suitable for queues and stacks. 3. Set: Unordered unique elements set, supporting set operation. 4. Ordered Set (SortedSet): A unique set of elements with scores, suitable for rankings. 5. Hash table (Hash): a collection of key-value pairs, suitable for storing objects.

Redis's database methods include in-memory databases and key-value storage. 1) Redis stores data in memory, and reads and writes fast. 2) It uses key-value pairs to store data, supports complex data structures such as lists, collections, hash tables and ordered collections, suitable for caches and NoSQL databases.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools