Redis (5) of the growth path of programmers -- introduction to the hash type of redis data structure

Preface
To quote the text on Baidu Encyclopedia, the hash value generally refers to the value that converts an input of any length into a fixed-length output through a hash function, and the hash function generally refers to a hash function. Based on this, the hashMap data type is generated in java. So what is the hash data type in redis? Is it similar to java's hashMap?

What is the Hash type of redis?

The hash type of Redis consists of: key { field1: value1, field2: value2, field3: value3 ....} The
Hash type implementation structure of Redis is the same as the hashMap in java, which is an array of linked lists, stored in the form of key-value pairs, and can be Make an analogy with classes in java for easy understanding. Key is the class name, field is the member variable name, and value is the value of the member variable. For example: when we want to store a user's shopping cart list, we can set the key to be the user id, the field to be the product id, and the value to be the quantity of the product, thus forming a small shopping cart storage type.

Redis hash type structure

insert image description here
Let's first look at the hashmap data type of java. The storage method is as follows:

  1. Mapping storage: Use the hash mapping function to obtain the position in the hash table according to the key. If there is a value in the position, it will be connected in the form of a linked list, as shown in the figure. At the beginning, it is stored in the form of a linked list. When the length of the linked list is 8, it is converted into a red-black tree for storage to improve the search and storage speed.
    Ps: Why is it 8? This is the value obtained after weighing the average search length (log(n)) of the red-black tree and the average search length (n/2) of the linked list, that is, log(n) = n/2
  2. Hash expansion: There is a filling factor in the hashMap, which is generally 0.75, and an initial length of 16, which means that if there are at least 12 (16*0.75) positions in the hash table. To expand. The expansion method is more complicated. Take jdk1.7 as an example: when a hashMap object is new, the array will be initialized, and its capacity will become a power of 2 not less than the specified capacity. Thresholds are then determined based on the loading factor. If it is not the first put, the new array capacity = old array capacity * 2, and the new threshold = new array capacity * load factor.
    For details, please refer to the hashmap of the growth path of programmers.
    Let's take a look at the hash data type of Redis again.
  3. Mapping storage: This storage method is similar to hashmap and stores data in the form of "array + linked list". When the data on the hash array collides, the colliding elements will be connected in the form of a linked list.
  4. Hash expansion: Java jdk1.7 version will have a step to put the data set in the original array into the new array when expanding the capacity. Here, the rehash function is involved. For details, please refer to the hashmap of the growth path of programmers. However, when the number of data sets is large, rehash will be a time-consuming operation and may be blocked. Therefore, redis needs to redesign the expansion mechanism - progressive rehash.
    Why is it called progressive rehash? Unlike ordinary rehash, which is completed all at once, progressive rehash is completed gradually in multiple times. The advantage of this is to avoid service congestion when the amount of data in the hash structure is large.
    Conditions to trigger rehash:
  5. Capacity expansion
    a) Redis is not currently executing the BGSAVE command or the BGREWRITEAOF command, and the load factor of the hash table (the number of nodes saved in the hash table / the size of the hash table) is greater than or equal to 1.
    b) Redis is currently executing the BGSAVE command or the BGREWRITEAOF command, and the load factor of the hash table is greater than or equal to 5.
    Note: Executing expansion while executing the BGSAVE command or BGREWRITEAOF command will result in excessive separation of memory pages.
  6. Shrinkage (load factor is less than 0.1)
    progressive rehash principle:
    1. Progressive rehash will allocate new space, the size of which is twice the capacity of ht[0] (the original hash structure) [note that rehash is accompanied by expansion], so that Save the old and new hash structures, which are recorded as ht[0] and ht[1] respectively, ht[0] is the old hash structure, and ht[1] is the new hash structure.
    2. Set a subscript counter rehashidx, initialize it to 0, and prepare for rehash operation. As shown in Figure
    insert image description here
    3, every time the redis addition, deletion, modification and query operation is performed, all the data on the linked list in the rehashinx index in ht[0] will be migrated to ht[0].size + rehashinx in the ht[1] table location, see hashmap expansion mechanism for details. Then add one to the value of rehashidx, pointing to the next linked list to be migrated. As shown below. In addition, when the instruction is idle, redis will also have a scheduled task to automatically relocate the dictionary.
    insert image description here
    4. When rehashidx == ht[0].size, it means that all the data in ht[0] has been migrated to ht[1]. At this time, set rehashidx to -1.

Addition, deletion, modification and query of redis in the process of progressive rehash:

  1. Add data: directly add data in ht[1] , avoiding the problem that the data in ht[0] cannot be migrated to ht[1] in time
  2. Delete, update, and query: operations such as deletion, lookup, and update of the dictionary will be performed on two hash tables. First query in ht[0], if not found, then query in ht[1].

Advantages and disadvantages of Hash data structure

advantage:

  1. It is convenient to store structured data. It is not necessary to serialize the entire object before storing it like the string type. It is suitable for object storage.
    shortcoming:
  2. Compared with the string type, the storage overhead is greater.

Common hash commands

  1. Hset key field value //store structured data
  2. Hmset key field1 value1【field2 value2 ...】 //store structured data
  3. Hget key field // Get an attribute value in a certain structured data
  4. Hgetall key // Get all attributes + attribute values ​​in a structured data
  5. Hlen key // get the length

Redis installation
https://blog.csdn.net/qq_31236027/article/details/121879741
redis introduction
https://blog.csdn.net/qq_31236027/article/details/121879604
redis data structure introduction
https://blog.csdn. net/qq_31236027/article/details/121894713
Introduction to string type of redis data structure
https://blog.csdn.net/qq_31236027/article/details/122016881?spm=1001.2014.3001.5501

Guess you like

Origin blog.csdn.net/qq_31236027/article/details/122897761