In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains how the memory model of Redis hash structure is. Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what the Redis hash structure memory model is like.
Hash type internal coding details
For the five commonly used data types of Redis (String, Hash, List, Set, sorted set), each data type provides at least two internal coding formats, and the choice of internal coding mode of each data type is completely transparent to users. Redis will adaptively choose a more optimized internal coding format according to the amount of data.
If you want to see the internal encoding format of a key, you can use the OBJECT ENCODING keyname directive, such as:
127.0.0.1 Redis 6379 > 127.0.0.1 Redis 6379 > 127.0.0.1
For the most frequently used Hash types, there may be two internal encodings:
OBJ_ENCODING_ZIPLIST (compressed list)
OBJ_ENCODING_HT (hash table)
Redis will adaptively choose the better of the two encoding methods according to the amount of data, and all these are completely transparent to the user.
When the number of data entries is small and the data value is small, Redis will use compressed list (OBJ_ENCODING_ZIPLIST) coding for storage. Here, there are "fewer" members, and the standard of "small" member values can be configured through the following configuration items:
Hash-max-ziplist-entries 512hash-max-ziplist-value 64
Redis provides the default value by default. Of course, users can configure it according to the actual situation.
When the number of fields of the Hash type key
< hash-max-ziplist-entries 并且 每个字段名和字段值的长度 < hash-max-ziplist-value 时,Redis 会使用 OBJ_ENCODING_ZIPLIST来存储该键,反之则会转换为 OBJ_ENCODING_HT的编码方式。 口说无凭,我们不妨先来做个实验感受一下吧:Obviously, this experiment verifies that when the length of the field value is greater than 64, the encoding format will be changed from ZIPLIST mode to Hashtable mode.
Before the source code, there is no secret. Let's take a look at Redis's source code implementation of this part of the switch, so we can understand it more clearly:
The internal storage models of OBJ_ENCODING_ZIPLIST and OBJ_ENCODING_HT are explained in detail below, knowing their respective characteristics, advantages and disadvantages, and naturally understanding the intention of using them internally in Redis.
OBJ_ENCODING_ZIPLIST coding
Ziplist compressed list is a compact coding format, the general idea is time for space, that is, at the cost of partial read and write performance, in exchange for extremely high memory space utilization, so it will only be used in scenarios with a small number of fields and small field values. I would like to recommend a framework exchange group: 698581634, you can get the information for free if you join the group.
The reason for the extremely high memory utilization of compressed lists is inseparable from the characteristics of continuous memory, and its typical memory structure can be vividly shown in the following figure:
So if you use Ziplist to store the hash type of Redis, the arrangement of elements becomes the graphic diagram shown in the following figure: that is, both key and value are logically continuous memory:
OBJ_ENCODING_HT coding
The inner part of the coding method of OBJ_ENCODING_HT is the real hash table structure, or dictionary structure, which can achieve O (1) complex read and write operations, so it is very efficient.
Within Redis, from the OBJ_ENCODING_HT type to the underlying real hash table data structure is nested layer by layer, the relationship is as follows:
This relationship can be seen from the source code of the definition part of the Redis hash table:
Let's explain the various parts in detail:
About Hash Node (dictEntry)
About hash tables (dictht) and dictionaries (dict)
About dictType
How Redis calculates hash values
The source code for the Redis calculation Hash is as follows:
This is a C language macro definition, in fact, the real responsibility behind the Hash value calculation is the function pointer hashFunction in the dictType structure described above. I would like to recommend a framework exchange group: 698581634, you can get the information for free if you join the group.
When initialized, the hashFunction function pointer corresponds to the actual functions that are assigned to actually calculate the Hash value, like the following:
How Redis calculates the index value of the access index
The calculation of the Index value depends on the hash value calculated above, as follows:
At this point, I believe you have a deeper understanding of "how the Redis hash structure memory model is". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.