Introduction to the five major data types of Redis

33f69ec7e94b49a08c544c85ce7d7e88.jpg, Introduction

 

The five major data types of Redis are also called the five major data objects; the six major data structures introduced earlier, Redis does not directly use these structures to implement the key-value pair database, but uses these structures to build an object system redisObject; this object system includes Five data objects, string object (string), list object (list), hash object (hash), set (set) object and ordered set object (zset); and the underlying data encoding of these five large objects can be used with the command OBJECT ENCODING to view.

 

redisObject structure

 

copy code

1 typedef struct redisObject {

2 // type

3 unsigned type:4;

4 // encoding

5 unsigned encoding:4;

6 // pointer to the underlying implementation data structure

7 void *ptr;

8 // ...

9 } robes;

copy code

Redis stores data in key-value pairs, so objects are divided into key objects and value objects, that is, storing a key-value key-value pair will create two objects, a key object and a value object.

 

The key object is always a string object, and the value object can be any of the five big objects.

 

The type attribute stores the type of the object, that is, one of the string, list, hash, set, and zset we are talking about. You can use the command TYPE key to view it.

The encoding attribute records the encoding used by the formation, that is, which data structure implementation is used at the bottom of this object.

 

 

The table lists the underlying encoding constants and the output of the corresponding OBJECT ENCODING command. The first three items are string structures

 

We do not specify the encoding of the object when storing key-value pairs, but Redis will set different encodings for an object according to different usage scenarios, which can save memory and speed up access.

 

 

 

2. String object (string)

The underlying data structure of the string object is implemented as a simple dynamic string (SDS) and direct storage, but its encoding method can be int, raw or embstr, the difference lies in the memory structure.

 

(1) int encoding

 

The string stores an integer value, and this can be formally represented by the long type, then it will be directly stored in the ptr attribute of redisObject, and the encoding is set to int, as shown in the figure:

 

 

 

(2) raw encoding

 

 If the string value is larger than 32 bytes, use the simple dynamic string (SDS) structure, and set the encoding to raw. At this time, the memory structure is consistent with the SDS structure, and the number of memory allocations is twice, and the redisObject object is created. And sdshdr structure, as shown in the figure:

 

 

 

(3) embstr encoding

 

 The string value saved by the string is less than or equal to 32 bytes, and the simple dynamic string (SDS structure) is also used, but the memory structure is optimized to save the string that has been canceled; the memory allocation only needs to be done once. It can be completed, just allocate a continuous space, as shown in the figure:

 

 

 

 String object summary:

 

In Redis, long and double floating-point numbers are first converted to strings and then stored.

The encoding effect of raw and embstr is the same, the difference lies in memory allocation and release, raw twice, embstr once.

The embstr memory block is continuous, which can make better use of the advantages of the cache

If the int encoding and embstr encoding perform operations such as appending strings, they will be converted to raw encoding if the conditions are met; the object of embstr encoding is read-only, and once modified, it will be converted to raw encoding first.

3. List object (list)

The encoding of the list object can be one of ziplist and linkedlist.

 

(1) ziplist encoding

 

The underlying implementation of the ziplist-encoded hash caprice is a compressed list, and each compressed list node stores a list element.

 

 

 

(2) linkedlist encoding

 

The bottom layer of linkedlist encoding is implemented by a double-ended linked list. Each double-ended linked list node stores a string object, and a list element is stored in each string object.

 

 

 

List object encoding conversion:

 

List objects need to meet two conditions when using ziplist encoding: first, the length of all strings is less than 64 bytes;

The numbers for the two conditions can be modified in the Redis configuration file, the list-max-ziplist-value option and the list-max-ziplist-entries option.

The StringObject in the figure is the string object mentioned in the previous section. The string object is the only one used as a nested object among the five major objects.

 

 

4. Hash object (hash)

The encoding of the hash object can be one of ziplist and hashtable.

 

(1) ziplist encoding

 

The underlying implementation of the ziplist-encoded hash object is a compressed list. In the ziplist-encoded hash object, the key-value key-value pairs are put into the compressed linked list in a closely connected manner. First put the key at the end of the list, and then put the value; key-value pairs are always added to the end of the table.

 

 

 

(2) hashtable coding

 

The underlying implementation of the hash object encoded by hashtable is a dictionary, and each key-value pair in the hash object is saved by a dictionary key-value pair.

 

The dictionary key-value pair is that the key and value of the dictionary are both string objects, the key of the dictionary stores the key of the key-value, and the value of the dictionary stores the value of the key-value.

 

 

 

Hash object encoding conversion:

 

Two conditions need to be met for the hash object to be encoded with ziplist: one is that the string length of the key and value of all key-value pairs is less than 64 bytes; the second is that the number of key-value pairs is less than 512; if any one is not satisfied, use hashtable encoding .

The above two conditions can modify the hash-max-ziplist-value option and hash-max-ziplist-entries option in the Reids configuration file.

 

 

5. Collection object (set)

The encoding of a collection object can be one of intset and hashtable.

 

(1) intset encoding

 

The underlying implementation of the collection object encoded by intset is an integer collection, and all elements are stored in the integer collection.

 

 

 

(2) hashtable coding

 

The underlying implementation of the hashtable-encoded collection object is a dictionary. Each key of the dictionary is a string object, which stores a collection element. The difference is that the values ​​​​of the dictionary are all NULL; you can refer to the hashset structure in java.

 

 

 

Collection object encoding conversion:

 

Two conditions need to be met for collection objects to use intset encoding: one is that all elements are integer values;

The above second condition can modify the et-max-intset-entries option in the Redis configuration file.

 

 

 6. Ordered collection object (zset)

The encoding of an ordered collection can be one of ziplist and skiplist.

 

(1) ziplist encoding 

 

The underlying implementation of ziplist-encoded ordered collection objects is a compressed list. Its structure is similar to that of a hash object. The difference is that there are two closely connected compressed list nodes. The first one stores the members of the element, and the second one stores the score of the element. And the smaller score is closer to the head of the table, and the larger score is closer to the end of the table.

 

 

 

(2) skiplist encoding

 

The underlying implementation of the skiplist-encoded ordered collection object is a skip list and a dictionary;

 

Each jump table node saves a collection element, and arranges it according to the score from small to large; the object attribute of the node saves the members of the element, and the score attribute saves the score;

 

Each key-value pair of the dictionary holds a collection element, the key of the dictionary holds the members of the element, and the value of the dictionary holds the score.

 

 

 

Why is skiplist encoding implemented using both skiplists and dictionaries?

 

The advantage of the jump table is that it is ordered, but the query score complexity is O(logn); the dictionary query score complexity is O(1), but it is unordered, so it is realized by combining the points of the structure.

Although two structures are used, the element members and scores of the set are shared, and the two structures point to the same address through pointers, which will not waste memory.

Ordered set encoding conversion:

 

Ordered collection objects need to meet two conditions when using ziplist encoding: first, the length of all elements is less than 64 bytes; second, the number of elements is less than 128; if any of the conditions is not met, skiplist encoding will be used.

The above two conditions can modify the zset-max-ziplist-entries option and the zset-max-ziplist-value option in the Redis configuration file.

Guess you like

Origin blog.csdn.net/weixin_57763462/article/details/131989126