redis memory operations

This article is basically a redis official website a text translation memory optimization, memory optimization

Type of special coding operation set

When redis 2.2 will start small data sets optimized to take up less space, such as a list, hash, the element is set an integer, the maximum number of elements and the element size is less than a given ordered set of values, will be a very efficient way to encode, is occupied by the memory can be reduced by 10-fold (average of 5 times).

For users and API, these operations are completely transparent, by cpu / memory tradeoffs, the following is a specific encoding type redis.conf instructions for adjusting the maximum number of elements and the largest element size. Details can be explained on the redis.conf profile.

hash-max-zipmap-entries 512 (hash-max-ziplist-entries for Redis> = 2.6) #hash maximum number of types of elements 
hash-max-zipmap-value 64 (hash-max-ziplist-value for Redis> = 2.6) #hash maximum element size of the type 
list-max-ziplist-entries 512 #list maximum number of elements of type 
list-max-ziplist-value 64 # list type maximum element size zset-max-ziplist-entries 128 # has the maximum number of elements of the ordered set maximum element size zset-max-ziplist-value 64 # ordered set the maximum number of elements in set-max-intset-entries 512 #set set

  How the number or size of the element exceeds the configuration, to automatically convert the encoding, if the modification of their configuration, is generally recommended for the next benchmark.

32-bit system

Using the bit and byte operation

  redis version 2.2 introduces a byte and bit GetRange , setRange , GETBIT and SetBit . These commands can be seen as an array of bytes string type. Use them to save more memory usage. Such as using one bit to save the user's gender: 0 for men and 1 for women.

Use hash types as possible

  As mentioned above, hash type small set will be under special code, you can increase memory efficiency. Thus, hash should be used to represent data. For example, in a web application, you should use a hash save an object, instead of using a number of key user attributes to express on behalf of the user attributes. The user-100001 {age: 19, sex: 1, adress: Hangzhou} instead user.age-100001: 19 This key-value pairs to store user information.

Use a hash abstract save memory key-value store

  We can use ordinary redis to the key-value store for modeling, which may be a string, which is not only more efficient than ordinary memory key-value store, and more efficient than memcached.

  Let's start from the facts, in a small amount of data, the use of key-value ratio expressed using the hash to represent takes up more memory.

  In many cases hash table is relatively small, containing only a few fields. When the hash is small, it can be encoded as O (N) data structures, such as those having a length prefix key array. Since we only do so when N is small, the average time Hget and HSET or O (1).

  Only from time complexity, this explanation is not sufficient, but in terms of the time constant is true, should be able to structure an array cpu cache and play together very well (better than the array structure of the local cache performance hash) .

  However, due to hash the key function is not complete redis such can not have an expiration time, and only a string.

 

Guess you like

Origin www.cnblogs.com/hhan/p/10950918.html