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. (Recommended: redis video tutorial)
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
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 string type Keys: GETRANGE/SETRANGE/GETBIT/SETBIT /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 it as much as possible in practical applications. Hash type, 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, all the Hash values The position of the field has been reserved, and Field/Value can be accessed randomly like 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, Redis will consider re-storing them in the form of a Hash Table, otherwise it will always remain this way. An efficient storage and access method.
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 are represented and stored in the form of Hash fields), thus further optimizing the use efficiency of storage space.
For more redis knowledge, please pay attention to the redis database tutorial column.
The above is the detailed content of Introduction to redis memory optimization methods. For more information, please follow other related articles on the PHP Chinese website!