Interviewer: What is the internal implementation of the hash data type 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 the hash data type?

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.

hash encoding

There are two encodings of hash, namely ziplist and hashtable. When the length of the key and value of all key-value pairs is less than hash-max-ziplist-value(default is 64 bytes), and the number of key-value pairs is less than hash-max-ziplist-entries(default is 512), the hash will use the compressed list as the encoding, otherwise use the hash The wish table is used as the encoding.

Let's take an example:

127.0.0.1:6379> hset one-more-hash name "万猫学社"
(integer) 1
127.0.0.1:6379> hset one-more-hash gender "男"
(integer) 1
127.0.0.1:6379> object encoding one-more-hash
"ziplist"

At this time, the length of the key and value of all key-value pairs and the number of key-value pairs are relatively small, and the hash uses a compressed list as the encoding. Let's add a key-value pair with a larger value:

127.0.0.1:6379> hset one-more-hash introduce "Java领域优质创作者、CSDN博客专家认证"
(integer) 1
127.0.0.1:6379> object encoding one-more-hash
"hashtable"

At this point, the encoding of the hash is converted from a compressed list to an encoding.

Of course, knowing the above details has not completely "conquered" the interviewer, we need to go deeper :)

The underlying implementation of hashing

When the compressed list is used as the encoding of the hash, a new key-value pair is added to the hash data type, first add the compressed list node of the key to the end of the compressed list, and then add the compressed list node of the value to the compressed list end of .

Therefore, in the compressed list of hash data type, the key-value pair added first is in the direction of the head of the compressed list, and the key-value pair added later is in the direction of the end of the compressed list; two nodes of the same key-value pair are close to each other. Next to each other, the node for the key comes first and the node for the value comes after.

Compressed lists use a more compact memory structure to achieve contiguous storage of multiple key-value pairs, which is better than hash tables in terms of memory savings.

When the hash table is used as the encoding of the hash, each key-value pair is stored using a dictionary key-value pair, each key of the dictionary is a string object, and the key of the key-value pair is stored in the object; The value is also a string object, and the value of the key-value pair is stored in the object.

Although the hash table does not save memory without compressing the list, its read and write time complexity is O(1), which is better than the compressed list in terms of time efficiency.

Summarize

There are two internal implementations of the hash data type: ziplist and hashtable. When the length of keys and values ​​of the hash data type is small and the number of key-value pairs is small, a compressed list is used as the internal implementation, otherwise a hash table is used as the internal implementation.


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=324148388&siteId=291194637