Redis study notes 4--Redis data storage optimization mechanism (collection)

1. zipmap optimization hash:

As mentioned earlier, storing an object in a hash type takes up less memory and makes it easier to access the entire object. The reason for saving memory is that when a new hash object is created, zipmap is used to store it. This zipmap is not actually a hash table, but compared with the normal hash implementation, zipmap can save a lot of metadata storage overhead required by the hash itself. Although the addition, deletion, and search of zipmap are all O(n), the number of fields in general objects is not too much. So using zipmap is also fast, that is to say, adding and deleting is still O(1) on average. If the size of the field or value exceeds a certain limit, redis will automatically replace the zipmap with the normal hash implementation internally. This limit can be specified in the configuration file (the default configuration is in redis.conf in the redis root directory): 

hash-max-zipmap-entries 512 #Configuration  fields have a maximum of 512 hash
-max-zipmap-value 64 #Configure 
the maximum value of 64 bytes

2. ziplist optimization list:

If the type member value of redisObject is of type REDIS_LIST, when the number of elements of the list is less than the configuration value list-max-ziplist-entries and the length of the element value string is less than the configuration value list-max-ziplist-value, it can be encoded as REDIS_ENCODING_ZIPLIST type storage, otherwise use Dict to store (Dict is actually an implementation of Hash Table), list uses ziplist data structure to store data, on the one hand, in order to save memory, on the other hand, this structural sequential storage structure can be more Good use of cpu local and prefetching strategies.

The configuration looks like this:

list-max-ziplist-entries 512 #Configure the  maximum number of elements to 512 list -max-ziplist-value 64 #Configure  the maximum value of 64 bytes

3. intset optimization set:

When the elements in the set set are integers and the number of elements is less than the configured set-max-intset-entries value, the intset data structure is used for storage, otherwise it is converted into a Dict structure. Dict is actually an implementation of Hash Table, and the key is the element value. , the value is NULL, so that it can be judged whether the set contains an element in O(1) time.

There are three types of arrays in intset: int16_t type, int32_t type, int64_t type. As for how to choose that type of array, it is determined according to the value range of the stored value. It is int16_t during initialization, and the maximum value in the set is [INT16_MIN, INT16_MAX], [INT32_MIN, INT32_MAX], [INT64_MIN] , INT64_MAX] to dynamically determine the type of the entire array. For example, the set is of int16_t type at the beginning. When a value in the range of [INT32_MIN, INT32_MAX] is added to the set, the array storing the set will be upgraded to an array of int32_t.

The configuration of the intset element limit is as follows:

set-max-intset-entries 512 #The  maximum number of configuration elements is 512

4. ziplist optimizes sorted set:

Like the root hash and the list, the sorted set also has a way of saving memory. When the number of elements and the size of the elements of the sorted set are less than a certain limit, it is stored in ziplist.

This limit is configured as follows:

zset-max-ziplist-entries 128 #The  maximum number of configuration elements is 512 zset
-max-ziplist-value 64 #The 
maximum configuration value is 64 bytes

5. Summary:

Redis provides a lot of methods for optimizing memory. The above configuration values ​​are all default configurations, which should be adjusted according to our specific demand scenarios, and a lot of tests should be done to achieve the best results. At the same time, you must have a good understanding of these data structures of Redis.

Analysis of Redis memory storage structure: http://www.searchtb.com/2011/05/redis-storage.html


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325491135&siteId=291194637