Home > Backend Development > C#.Net Tutorial > Redis Tutorial (14): Introduction to Memory Optimization

Redis Tutorial (14): Introduction to Memory Optimization

黄舟
Release: 2016-12-28 15:12:25
Original
1460 people have browsed it

1. Special encoding:

Since Redis 2.2, many data types can be optimized for storage space through special encoding. Among them, Hash, List and Sets composed of Integer can all use this method to optimize the storage structure so as to occupy less space. In some cases, 9/10 of the space can be saved.
These special encodings are completely transparent to the use of Redis. In fact, it is just a transaction between CPU and memory. If the memory usage is higher, then more CPU will naturally be consumed when operating data, and vice versa. Redis provides a set of configuration parameters for setting various thresholds related to special encoding, such as:

  #如果Hash中字段的数量小于参数值,Redis将对该Key的Hash Value采用特殊编码。
    hash-max-zipmap-entries 64
    #如果Hash中各个字段的最大长度不超过512字节,Redis也将对该Key的Hash Value采用特殊编码方式。
    hash-max-zipmap-value 512
    #下面两个参数的含义基本等同于上面两个和Hash相关的参数,只是作用的对象类型为List。
    list-max-ziplist-entries 512
    list-max-ziplist-value 64
    #如果set中整型元素的数量不超过512时,Redis将会采用该特殊编码。
    set-max-intset-entries 512
Copy after login

If a value that has been encoded exceeds the maximum limit in the configuration information after being modified. , then Redis will automatically convert it to a normal encoding format. This operation is very fast. However, if you operate it in reverse and convert a large value of normal encoding into a special encoding, Redis's suggestion is to do it first before officially doing it. It is best to simply test the conversion efficiency first, because such conversions are often very inefficient.

2. BIT and Byte level operations:


## Starting from Redis 2.2, Redis provides four characters: GETRANGE/SETRANGE/GETBIT/SETBIT String type Key/Value command. Through these commands, we can access String type value data just like operating arrays. For example, the ID that uniquely identifies a user may be just a substring of the String value. In this way, it can be easily extracted through the GETRANGE/SETRANGE command. Furthermore, you can use BITMAP to represent the user's gender information, such as 1 for male and 0 for female. When using this method to represent the gender information of 100,000,000 users, it only takes up 12MB of storage space. At the same time, data traversal through the SETBIT/GETBIT command is also very efficient.


3. Use Hash as much as possible:

Since small Hash type data takes up relatively little space, we should consider using Hash type as much as possible in practical applications, such as user Registration information, which includes fields such as name, gender, email, age, and password. Of course, we can store this information in the form of Key, and the information filled in by the user is stored in the form of String Value. However, Redis prefers to store it in the form of Hash, and the above information is expressed in the form of Field/Value.

Now we will further prove this statement by learning the storage mechanism of Redis. The special encoding mechanism has been mentioned at the beginning of this blog, and there are two configuration parameters related to the Hash type: hash-max-zipmap-entries and hash-max-zipmap-value. As for their scope of action, they have been given before, so I won’t go into details here. Now let's assume that the number of fields stored in the Hash Value is less than hash-max-zipmap-entries, and the length of each element is also less than hash-max-zipmap-value. In this way, whenever there is a new Hash type Key/Value storage, Redis will create a fixed-length space for the Hash Value. The maximum number of pre-allocated bytes is:
total_bytes = hash-max-zipmap-entries * hash -max-zipmap-value
In this way, the positions of all fields in the Hash have been reserved, and Field/Value can be accessed randomly like accessing an array. The step interval between them is hash-max-zipmap- value. Only when the number of fields in the Hash Value or the length of a new element exceeds the above two parameter values ​​respectively, Redis will consider re-storing them in the form of a Hash Table, otherwise it will always maintain this efficient storage and access. Way. Not only that, since each Key must store some associated system information, such as expiration time, LRU, etc., compared with String type Key/Value, the Hash type greatly reduces the number of Keys (most Keys Represented and stored in the form of a Hash field), thereby further optimizing the efficiency of storage space usage.

The above is the content of Redis Tutorial (14): Introduction to Memory Optimization. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template