Redis learning notes (VI) object

Time:2021-9-24

Earlier, we looked at the main data structures used by redis, such as simple dynamic string (SDS), two-way linked list, dictionary, compressed list, integer set, etc.

However, redis does not directly use these data structures to implement key value pairs, but creates an object system based on these data structures. This system includes string objects, list objects, hash objects, collection objects and ordered collection objects. In addition, redis’s object system also implements a memory recovery mechanism based on counting technology, In addition, redis also implements the object sharing mechanism through reference counting technology (under appropriate conditions, multiple database keys share the same object to save memory).

Finally, the redis object has access time record information, which can be used to calculate the idle time of database keys. When the maxmemory function is enabled on the server, the keys with large idle time will be deleted by the server first.

 

1. Each structure in redis is identified by the redisobject structure, including PTR (pointing to the underlying data structure), encoding (deciding which underlying data structure to use), type and other attributes.

 

2. When we create a key value pair, we will create at least two objects: key object (string object) and value object (species type).

 

3. The encoding of string objects can be integers, raw or enbstr, SDS

  1. Enbstr (short string length less than 32) calls the memory allocation function once to allocate a continuous memory, including redisobject structure and sdshdr structure, without modification commands. Any modification commands will be converted to raw objects.

  2. SDS (string length exceeds 32).

  3. Raw will call memory allocation twice to create redisobject structure and sdshdr structure respectively.

 

4. List object. The code of the list object can be zip list or LinkedList

  1. Ziplist uses a compressed list as the underlying implementation. When the length of all string elements saved by the list object is less than 64 bytes and the number of elements is less than 512, the compressed list is used as the underlying implementation.

  2. The LinkedList encoded list object uses a two-way linked list as the underlying implementation, and each two-way linked list node stores a string object.

 

5. Hash object

  1. The encoding of hash object can be ziplist or hashtable

  2. The zip list encoded hash object uses the compressed table as the underlying implementation. When a new key value pair is added to the hash object, the program will first push the compressed list node with the saved key to the end of the compressed list, and then push the compressed list node with the saved value to the end of the compressed list.

  3. Hashtable is used as the encoded hash object, dictionary as the underlying implementation, string object as the key and string object as the value.

6. Collection object

  1. The code of the collection object can be intset or hashtable

  2. The set object encoded by intset uses the integer set as the underlying implementation.

  3. The hashtable encoded collection object uses the dictionary as the underlying implementation. Each key of the dictionary is a string object, each string object contains a collection element, and all field values are set to null.

     

     

  4. Object conversion. Intset to hashtable condition: not all elements are integers or the number of elements exceeds 512

 

7. Ordered collection object

  1. The encoding of an ordered set can be ziplist or skiplist

  2. The zip list encoded compressed list object uses the compressed list as the underlying implementation. Each collection element is saved using two compressed list nodes next to each other. The first node saves the member of the element, and the second element saves the score of the element.

  3. The ordered collection object encoded by skiplist uses Zset structure as the underlying implementation. A Zset structure contains both a dictionary and a jump table.

  4. typedef struct zset{
        zskiplist *zsl;
        dict *dict;// Save the mapping from member to score. The key saves the element member value and saves the score.
    }

     

  5. Explain why the dictionary and jump table are used to realize the ordered set at the same time: Although the ordered set can be realized with either of the two structures, when we only use the dictionary to realize the ordered set, because the dictionary is an unordered saving element, when we implement the scope operation, we need to sort all the elements first, The time complexity used here is at least o (nlogn) and there is additional memory consumption; In addition, if only the jump table is used to realize the ordered set, although the advantage of range operation is retained, without the dictionary, the complexity of this operation will be increased from O (1) to o (logn).

  6. For encoding conversion, when the number of elements is less than 128 and the length of each element is less than 64 bytes, ziplost is used.

8. Memory recycling

  1. The reference technical information of each object is recorded by the refcount attribute of redisobject structure.

  2. When creating an object, the count value will be initialized to 1 by default. When it is used by the program, the counter will increase by one. When it is no longer used, the counter will decrease by one. When the counter is 0, the memory occupied by the object will be released.

 

9. Object idle time

  1. The idling duration of the object is recorded using the LRU attribute in the redisobjet structure, which records the time when the object was last accessed.

 

——— end ——–

 

Learn a little every day, there will always be gains.

 

Note: respect the author’s intellectual property rights. Refer to redis design and Implementation for the content in this article. Just learn here and share with you.

 

 

Recommended Today

A detailed explanation of the differences between Perl and strawberry Perl and ActivePerl

Perl is the abbreviation of practical extraction and report language “practical report extraction language”. Application of activestateperl and strawberry PERL on Windows platformcompiler。 Perl   The relationship between the latter two is that C language and Linux system have their own GCC. The biggest difference between activestate Perl and strawberry Perl is that strawberry Perl […]