Data Structure In Redis

Time:2021-10-27

0.RedisObject

1 typedef struct redisObject {
2     // type
3     unsigned type:4; 
4     // encode
5     unsigned encoding:4; 
6     // reference
7     void *ptr; 
8 } robj;

redis alaways store data by a peer of key-value,so redis object can classify to key-obj and value obj. The key-obj is a String-obj in any condition. The value could fall into five objs as we discuiss below.

type indicts the type of value.It’s usually that we say any of  string、list、hash、set、zset. Using TYPE “key”  show what’s the type of key. The parameter of encoding show the underlying of data-structure.
Data Structure In Redis

1.HashTable

Hash-obj is ethier ziplist or hashtable

1.ziplist encoding

Data Structure In Redis

2.Hash encoding

Data Structure In Redis

3.The change in ziplist and hashtable

Tow conditions must be certified:

  • all of keys and values must be less 64 bytes.
  • The number peers of keys and values nust be less than 512.
  • The elements of hash-max-ziplist-value and hash- max-ziplist-entries in configuration files can control the conditions.

2.String

The underlying of String will be stored directly or SDS. But the encoding can be int、raw or embstr.
(1) int encoding:

  • the string store interge and it repesent long. The number will be stored in ptr of redisObject, and the type will be set int.As below:

Data Structure In Redis
(2) raw encoding:
The byte of String more than 32 bytes wil be stored as SDS and the type winll be set as raw. The memory of it will be allocated twice: the structure of redisObject and sdshdr. As below:
Data Structure In Redis
(3) embstr:
The byte of String less than 32 bytes wil be stored as embstr. Embstr optimse the memory allocating just one time. The memory is contiguous chunk. As below:
Data Structure In Redis
conclusion:

  • In Redis ,The type of long、double will be treated as String.
  • raw and embstr have same econding. The different is the memory allocating . Raw is twice rather than embstr once. Embstr is contignous.
  • The modify action of int and embstr is same. There will be chnaged to raw in conditions. Embstr is read-only if it changes, embster will be to raw.

3.Set

Set can be intset or hashtable
(1)intset encode
underlying of inset is Array,all elemnts will be stored in Array. As below:
Data Structure In Redis
(2)hashTable encode
underlying of hashtable is dictionary, the key of dict is a string-obj,but the value is null. As below
Data Structure In Redis
(3) Conclusion:

  • Tow condition must be fufiled: All of data is a number and the size of stored number must be less than 512.
  • et-max-intset-entries is max mumber of inset number.

4.SortedSet

Underlying of sortedset is ziplist or skiplist
(1)ziplist encoding:
ziplist is similar with hashtable, but the differnet is combined element: first indict the member, second is the socre of member. small score of member is arranged to head ,large score of member to tail.
Data Structure In Redis
(2)skiplist encoding:

  • The underlying of skiplist is dict or skiplist
  • Each node stores a collection member and sorted. The obj of node stores value and score stores the mark.
  • The dict is similar with node. The value and mark will be stored in key and value of dict respectly.

Data Structure In Redis
(3)why skiplist has two encoding

  • skiplist is sorted, the cost of finding is O(logn);but dict is messy and cost is O(1)
  • but the two encoding share the same member ,because they have same address.

(4)The change between ziplist and skiplist:

  • sortedset use ziplist:
  • all lenth of member is less than 64 bytes
  • total number of member is less than 128.
  • Redis config set zset-max-ziplist-entries and zset-max-ziplist-value.

5.List

(1) ziplist
Data Structure In Redis
(2) linkedlist
Data Structure In Redis
(3) conversion

  • In ziplist:All the number of byte of memebr must be less than 64 and the number of member must be less than 512.
  • Redis configiguration:list-max-ziplist-value and list-max-ziplist-entries.