In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "what is Redis memory optimization method". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian and go deep into it slowly to study and learn "what is Redis memory optimization method" together!
I. Special code:
Since Redis 2.2, many data types can be optimized for storage space by special encoding. Hashes, Lists, and Sets made up of integers can all be optimized in this way to take up less space, in some cases 9/10 of the space.
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 the CPU consumption is naturally higher when operating on data, and vice versa. A set of configuration parameters is provided in Redis to set various thresholds associated with a particular encoding, such as:
#If the number of fields in the Hash is less than the parameter value, Redis will use a special encoding for the Hash Value of the Key.
hash-max-zipmap-entries 64
#If the maximum length of each field in the Hash does not exceed 512 bytes, Redis will also use a special encoding method for the Hash Value of the Key.
hash-max-zipmap-value 512
#The meaning of the following two parameters is basically the same as the above two parameters related to Hash, except that the object type of the function is List.
list-max-ziplist-entries 512
list-max-ziplist-value 64
#Redis uses this special encoding if the number of integer elements in the set does not exceed 512.
set-max-intset-entries 512
If an encoded value is modified to exceed the maximum limit specified in the configuration information, Redis will automatically convert it to the normal encoding format, which is very fast, but if you reverse the operation and convert a large value of normal encoding to a special encoding, Redis recommends that it is best to test the conversion efficiency before doing it officially, because such conversion is often very inefficient.
II. BIT and Byte level operations:
Starting with Redis 2.2, Redis provides GETRANGE/SETRANGE/GETBIT/SETBIT four commands for string types Key/Value. With these commands, we can access value data of type String just as we would an array. For example, the ID that uniquely identifies the user may be just a substring of the String value. This allows easy extraction via the GETRANGE/SETRANGE command. BITMAP may be used to indicate gender information of the user, for example, 1 indicates male and 0 indicates female. Representing the gender information of 100,000,000 users in this way only takes up 12MB of storage space, and at the same time, it is very efficient to traverse the data through SETBIT/GETBIT commands.
Use Hash as much as possible:
Because small Hash types take up relatively little space, we should consider using Hash types as much as possible in practical applications, such as user registration information, which includes fields such as name, gender, email, age, and password. We can of course store this information in the form of Key, and the information filled in by the user is stored in the form of String Value. Redis, however, 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 prove this claim further by studying the storage mechanism of Redis. The special encoding mechanism mentioned at the beginning of this blog has two configuration parameters related to Hash types: hash-max-zipmap-entries and hash-max-zipmap-value. As for their scope of action, it has already been given before, so I will not repeat it here. Now let's assume that the number of fields stored in 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 Key/Value of Hash type stored, Redis will create a fixed-length space for Hash Value. 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 are reserved, and Field/Value can be accessed randomly like an access array, with hash-max-zipmap-value steps between them. Only when the number of fields in 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 Hash Table, otherwise it will always maintain this efficient storage and access mode. Not only that, because each Key stores some associated system information, such as expiration time, LRU, etc., compared with String type Key/Value, 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.
Thank you for reading, the above is "Redis memory optimization method is what" content, after the study of this article, I believe that we have a deeper understanding of Redis memory optimization method is what this problem, the specific use of the situation also needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.