preface

  • The five data structures of Redis are string, Hash, list, set, and zset. There are actually multiple implementations of their internal code.
The data structure The internal encoding
String Int raw, embstr
hash Ziplist, hashtable
list Ziplist, LinkedList, Quicklist
set Intset, hashtable
zset Ziplist, skiplist
  • Redis can view the internal encoding mode using the object Encoding command.

parsing

  • Three internal encodings of String

    • Int: a long integer of 8 bytes.
    • Embstr: contains a string of 39 bytes or less.
    • Raw: The string contains more than 39 bytes.
  • There are two internal encodings of hash:

    • Ziplist: When the number of hash elements is smaller than hash-max-ziplist-entries (512 by default) and the element size is smaller than hash-max-ziplist-value(64 bytes by default). Redis uses Ziplist as an internal implementation of the hash.
    • Hashtable (hashtable) : When the hash does not satisfy the Ziplist implementation, the hashtable is used as the internal hash implementation. In this case, the ziplist read/write performance deteriorates, while the time complexity of hashTable is O(1).
    • Note: Redis will always use hashtable as its internal implementation after ziplist is converted to Hashtable, even if the contents of the subsequent cache are completely replaced.
  • There are three internal encodings of list:

    • Ziplist: Redis uses ziplist as an internal implementation of lists when the number of elements in a list is less than hash-max-ziplist-entries and the size of the elements is less than hash-max-ziplist-value.
    • Linkedlist (linkedlist) : when a list does not satisfy a ziplist implementation, the linkedlist is used as an internal implementation.
    • Quicklist: This data structure is new since Redis 3.2 and combines ziplist+ LinkedList.

- Note that lists, like hash, can only have ziplist turned into linkedList, not linkedList turned into Ziplist.Copy the code
  • Two internal encodings of a set:

    • Intset: Redis uses intset as an implementation of collections where all elements are integers and the number of entries is less than set-max-intset-entries (default: 512) to reduce memory usage.
    • Hashtable: Redis uses hashtable as the collection implementation when the collection does not satisfy intSet implementation.
  • Two internal encodings of Zset:

    • Ziplist: Zset-max-ziplist-entries are ordered when the number of elements is less than or equal to zset-max-ziplist-value (default: 128) and each element is less than zset-max-ziplist-value (default: 64 bytes). Redis uses Ziplist as an internal collection implementation, which reduces memory usage.
    • Skiplist: Use skiplist as an internal implementation when ordered collections do not satisfy Ziplist, because ziplist reads and writes are less efficient.

The last

  • Redis also has several advanced data structures, including Bitmaps. It is similar to the String data structure, but instead of a string, it stores bits, so a string structure can store 512 megabytes, and a bitmap can store 2^32 bits. In addition, distributed BloomFilter is based on this.
  • Learn with an open mind and make progress together