Redis Tutorial (14): Introduction to Memory optimization

Source: Internet
Author: User
Tags redis tutorial
First, special code:

Since Redis 2.2, many data types have been optimized for storage space in a specially coded way. The Hash, list, and integer sets can be used to optimize the storage structure to take up less space and, in some cases, save 9/10 of space.
These special encodings are completely transparent to the use of Redis, in fact, it's just a transaction between CPU and memory. If the memory usage is higher, the CPU consumes more naturally when manipulating the data, and vice versa. A set of configuration parameters are provided in Redis to set the various thresholds associated with special encodings, such as:

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

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

Second, 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 the string type of value data as if it were an array of operations. For example, an ID that uniquely identifies a user may be just one of a string of string values. This can be easily extracted through the getrange/setrange command. Then there is the bitmap can be used to represent the user's gender information, such as 1 means that male,0 represents female. In this way to represent 100,000,000 users of the gender information, but also only occupy 12MB of storage space, at the same time, the data traversal through the Setbit/getbit command is very efficient.

Third, use hash whenever possible:

Since small hash type data occupies relatively little space, we should consider the use of hash types, such as the user's registration information, such as name, gender, email, age and password, as much as possible in the actual application. We can of course store this information in the form of a key, and the information that the user fills in is stored as String value. Redis, however, is more recommended to store in hash form, and the above information is expressed in field/value form.
Now we can further prove this by learning the storage mechanism of Redis. At the beginning of the blog, a special coding mechanism was mentioned, with two configuration parameters related to hash types: Hash-max-zipmap-entries and Hash-max-zipmap-value. As far as the scope of their 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 that each element is less than hash-max-zipmap-value in length. This allows Redis to create a fixed length of space for hash value whenever a new hash type of key/value is stored, 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 the fields in the hash have been reserved, and the field/value can be accessed randomly as if the array was accessed, with a step interval of hash-max-zipmap-value. Redis will consider re-storing them as hash table only if the number of fields in the hash value or the length of a new element exceeds the two parameter values above, otherwise this efficient storage and access method will always be maintained. Not only that, since each key stores some associated system information, such as expiration Time, LRU, and so on, the hash type greatly reduces the number of keys (most of the keys are represented and stored in the form of a hash field) compared to Key/value of type string. This further optimizes the efficiency of storage space usage.

This is the Redis tutorial (14): Memory optimization Introduction, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.