Redis Tutorial (14): Introduction to Memory Optimization _redis

Source: Internet
Author: User
Tags memory usage redis redis tutorial value store

First, special code:

Since Redis 2.2, many data types can be optimized for storage in a special coding way. The Hash, list, and integer sets can optimize the storage structure to take up less space and, in some cases, save 9/10 of the space.
These special encodings are completely transparent to the use of Redis, in fact, it is only a transaction between CPU and memory. If memory usage is higher, the CPU used to manipulate the data is more natural and vice versa. A set of configuration parameters is provided in Redis to set various thresholds related to special encodings, such as:

Copy Code code as follows:

#如果Hash中字段的数量小于参数值, Redis will use a special encoding for the hash value of the key.
Hash-max-zipmap-entries 64
#如果Hash中各个字段的最大长度不超过512字节, Redis also uses a special encoding for the hash value of the key.
Hash-max-zipmap-value 512
#下面两个参数的含义基本等同于上面两个和Hash相关的参数, just the Action object type is list.
List-max-ziplist-entries 512
List-max-ziplist-value 64
#如果set中整型元素的数量不超过512时, Redis will use this special code.
Set-max-intset-entries 512

If a value that has been encoded has been modified to exceed the maximum limit in the configuration information, so Redis will automatically convert it to the normal encoding format, which is very fast, but if in turn, converts a larger value of a normal encoding into a special encoding, Redis's recommendation is that It's best to simply test the conversion efficiency before you do it, because such conversions are often very inefficient.

two, bit and byte-level operations:

    starting with Redis 2.2, Redis provides getrange/setrange/getbit/setbit four commands for string type Key/value. With these commands, we can access string-type value data like an array of operations. For example, an ID that uniquely identifies a user may be just one of the string values. This can be easily extracted by the Getrange/setrange command. Then there is the use of bitmap to represent the user's gender information, such as 1 indicates that male,0 said female. This way to represent the 100,000,000 users of the gender information, but also takes up only 12MB of storage space, at the same time, through the setbit/getbit command for data traversal is also very efficient.
   
Third, use hash as much as possible:

Because small hash type data takes up less space, we should consider using the hash type as much as possible in actual application, such as the user's registration information, including the name, gender, email, age and Password fields. We can of course store this information as a key, and the information that the user fills out is stored as String value. However, Redis is more recommended to store in the form of hash, the above information is expressed in the form of Field/value.
Now we can further prove this by learning the Redis storage mechanism. A special coding mechanism has been mentioned at the beginning of the blog, with two configuration parameters related to the hash type: Hash-max-zipmap-entries and Hash-max-zipmap-value. As far as their scope of action has been given, there is no longer much to repeat. Now let's assume that the number of fields stored in hash value is less than hash-max-zipmap-entries, and each element's length is less than hash-max-zipmap-value. So whenever there is a new hash-type Key/value store, Redis creates a fixed-length space for the hash value, and the maximum number of bytes that can be allocated is:
Total_bytes = hash-max-zipmap-entries * Hash-max-zipmap-value
As a result, the positions of all the fields in the hash have been reserved and can be accessed field/value as randomly as the array, and the step spacing 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 does Redis consider storing them as hash table, otherwise this efficient storage and access is always maintained. Not only that, because each key stores some associated system information, such as expiration Time, LRU, and so on, the hash type greatly reduces the number of keys compared to the string type Key/value (most of the keys are represented and stored in the form of a hash field), Thus further optimizes the use efficiency of the storage space.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.