In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1. Special coding:
Since Redis 2.2, many data types can be optimized for storage space by special coding. Among them, Hash, List and Sets composed of Integer can all optimize the storage structure in this way to take up less space and, in some cases, save the space of 9x10.
These special codes are completely transparent to the use of Redis, in fact, it is just a transaction between CPU and memory. If memory usage is higher, you will naturally consume more CPU when manipulating data, and vice versa. A set of configuration parameters are provided in Redis to set various thresholds related to special encodings, such as:
# if the number of fields in the Hash is less than the parameter value, Redis will use special encoding for the Hash Value of the Key. Hash-max-zipmap-entries 6 if the maximum length of each field in a Hash does not exceed 512 bytes, Redis will also use a special encoding for the Hash Value of that Key. The meaning of the following two parameters in hash-max-zipmap-value 51 parameters is basically the same as the above two parameters related to Hash, except that the acting object type is List. List-max-ziplist-entries 512list-max-ziplist-value 6 Redis will adopt this special encoding if the number of integer elements in the set does not exceed 512. Set-max-intset-entries 512
If a value that has been encoded has been modified and exceeded the maximum limit in the configuration information, then Redis will automatically convert it to the normal encoding format, which is very fast, but if the reverse operation is done, converting a normal encoding of a larger value into a special encoding, Redis's recommendation is that it is best to simply test the conversion efficiency before officially doing it, as such conversion is often very inefficient.
Second, operations at BIT and Byte levels:
Starting with Redis 2.2, Redis provides four commands for GETRANGE/SETRANGE/GETBIT/SETBIT for string type Key/Value. With these commands, we can access value data of type String as if we were manipulating arrays.
For example, the ID that uniquely identifies the user may be just one of the substrings of the string value. In this way, it can be easily extracted through the GETRANGE/SETRANGE command.
In addition, BITMAP can be used to represent the user's gender information, such as 1 that male,0 represents female. When representing the gender information of 100000000 users in this way, it only takes up the storage space of 12MB, at the same time, it is very efficient to traverse the data through the SETBIT/GETBIT command.
Third, use Hash whenever possible:
Because small Hash type data takes up relatively little space, we should consider using Hash type as much as possible in practical application, such as user registration information, including name, gender, email, age and password.
Of course, we can store this information in the form of Key, while the information filled in by the user is stored in the form of String Value. However, Redis is more recommended to store in the form of Hash, and the above information is expressed in the form of Field/Value.
Now let's further prove this claim by learning the storage mechanism of Redis. Special coding mechanisms have been mentioned at the beginning of this blog, with two configuration parameters related to the Hash type: hash-max-zipmap-entries and hash-max-zipmap-value.
As for their scope of action has been given previously, there is no longer too much repetition here. Now let's assume that the number of fields stored in Hash Value is less than hash-max-zipmap-entries, and that the length of each element is also less than hash-max-zipmap-value. In this way, whenever there is a new Hash type of Key/Value storage, Redis creates a fixed-length space for Hash Value, and the maximum number of bytes that can be pre-allocated 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 the Field/Value can be accessed randomly as an access array, with a step interval of hash-max-zipmap-value between them.
Only when the number of fields in Hash Value or the length of a new element exceeds the above two parameter values respectively, Redis will consider re-storing them as Hash Table, otherwise it will always maintain this efficient way of storage and access.
Moreover, because each Key stores some related system information, such as expiration time, LRU, etc., compared with Key/Value of String type, Hash type greatly reduces the number of Key (most Key are represented and stored in the form of Hash field), thus further optimizing the efficiency of storage space.
These are the details of the redis memory optimization method, please pay attention to other related articles!
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