Interviewer: What is the internal implementation of sorted sets in Redis?

Interviewer: What are the basic data types in Redis?

Me: The basic data types of Redis are: string (string), hash (hash), list (list), set (set), ordered set (zset).

Interviewer: What is the internal implementation of sorted sets?

I was still immersed in the complacent complacency of the previous question, and my expression suddenly froze, and my palms began to sweat. "This... I don't know much about it," I said hesitantly.

Interviewer: Go back and wait for news.

This sentence is said neatly and neatly, and then there is no more. Failure is a successful mother, I am not discouraged, I decided to make up for it right away.

Internal implementation of sorted sets

There are two internal implementations of sorted sets, namely: ziplist and skiplist. Next, we will have a detailed understanding of each.

Internally implemented with a compressed list

When the number of elements of the sorted set is less than zset-max-ziplist-entries(default is 128), and the length of each element member is less than zset-max-ziplist-value(default is 64 bytes), the compressed list is used as the internal implementation of the sorted set.

Each set element consists of two packed list nodes next to each other, where the first node holds the member of the element and the second node holds the branch of the element. The elements in the compressed list are arranged next to each other according to the scores from small to large, which effectively reduces the use of memory space.

As an example, we use the zaddcommand to create an ordered collection implemented as a compressed list:

127.0.0.1:6379> zadd one-more-zset 1 one 2 two 3 three
(integer) 3
127.0.0.1:6379> zrange one-more-zset 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> object encoding one-more-zset
"ziplist"

Use skip table as internal implementation

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), the skip table is used as the internal implementation of the sorted set.

At this point, the ordered set actually contains two structures, one is a jump table and the other is a hash table.

In the skip table, all elements are arranged in ascending order. objectThe pointer in the node of the skip table points to the string object of the element member, which scoreholds the score of the element. Through the jump table, Redis can quickly perform operations such as score range and ranking on sorted sets.

In a hash table, a mapping from element members to element scores is created for an ordered set. The keys in the key-value pair point to string objects that are members of the element, and the value in the key-value pair holds the element's score. With a hash table, Redis can quickly find the score of a specified element.

Although sorted sets use both jump tables and hash tables, both data structures use pointers to share members and scores in elements without additional memory waste.

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"

Internally implemented conversion

When a sorted set is implemented with a compressed list as the internal implementation, and a longer element member is added to the sorted set, or the number of elements added to the sorted set is too many, then the sorted set will be converted to Jump tables are implemented as internal. However, a sorted set with a skip list as its internal implementation is not converted to a packed list as its internal implementation.

As an example, let's first create a sorted collection with a compressed list as its internal implementation:

127.0.0.1:6379> zadd one-more-zset 1 one 2 two 3 three
(integer) 3
127.0.0.1:6379> zrange one-more-zset 0 -1
1) "one"
2) "two"
3) "three"
127.0.0.1:6379> object encoding one-more-zset
"ziplist"

Then, by adding an element with a longer member to it, it is converted to have a skip table as its internal implementation:

127.0.0.1:6379> zadd one-more-zset 4 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) "one"
2) "two"
3) "three"
4) "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"

Then, the element of the longer member is removed from the sorted set, and the sorted set still uses the skip table as the internal implementation:

127.0.0.1:6379> zrem one-more-zset 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) "one"
2) "two"
3) "three"
127.0.0.1:6379> object encoding one-more-zset
"skiplist"

Summarize

In Redis, there are two internal implementations of ordered sets: ziplist and skiplist. When all elements in the set have a short member length and a small number of elements, the ziplist is used as the internal implementation. , otherwise use skip table and hash table as internal implementation. When the conditions are not met, a compressed list can be converted to a skip list, but a skip list cannot be converted to a compressed list.


I have already seen this, you and I must be destined people, leave your likes and attention , and he will become a great thing in the future.

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

Guess you like

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