Redis data architecture, you can refer to the following figure:

Redis data architecture

All values in Redis exist in the form of Object, and their general structure is as follows:

typedef struct redisObject {

    unsigned [type] 4;

    unsigned [encoding] 4;

    unsigned [lru] REDIS_LRU_BITS;

    int refcount;

    void *ptr;

} robj;

Copy the code

  • Type: String, Hash, List, Set, or ZSet.
  • Encoding: Specifies the implementation of the type. For example, is Set implemented using hashTable or intSet?
  • Lru: The last time the information was accessed, in fact, the lRU is estimated to be related to the elimination strategy;
  • Refcount: object reference count;
  • PTR: address pointing to the actual implementer;

String

A String in Redis is not just a String. It can also be an integer or a floating point.

String encoding can be int, RAW, or embstr; Embstr is a new data structure in Redis 3.0:

If the string length is less than 39 bytes, use an embstr object, otherwise use a traditional raw object (after Redis 3.2, this is changed to 44 bytes).

Embstr has the advantage of allocating less space for creation (RedisObject and SDS are continuous), freeing less space for deletion, and connecting all data of objects together to find convenience;

If the length of the string increases and memory needs to be reallocated, the entire RedisObject and SDS need to be reallocated.

When modifying an embstr object, Redis will convert it to raw format and then modify it. Therefore, the modified object must be raw.

SDS

Application scenario: all common counts can be used for caching, counting, speed limiting, etc., such as the number of remaining items and dictionary table information. The length cannot exceed 512MB.

Hash

The underlying implementation of a Hash object can be ziplist or HashTable.

Ziplist: This data structure is stored in the order key1, value1, key2, value2;

HashTable: is implemented by the dict structure. (This structure is more complex, and I will write a separate article later.)

Application scenarios: Hash is used to store structured objects. You can directly modify the value of a field in the object. Like user information.

List

List objects can be encoded as Ziplist or LinkedList, and the names indicate both structures.

Ziplist: a compressed linked list that stores data in contiguous memory areas.

Linkedlist: is a two-way linkedlist.

Application scenario: A List can be used to store the message List on a website. And the lrange command, starting with an element, how many elements to read, can be viewed as a pagination query, like you see on a lot of websites, where you can pull down multiple pages.

Set

A Set is automatically weighted relative to a List; Its encoding can be **intset or hashtable **.

Intset: a set of integers that support three lengths: int16_t, int32_t, and int64_t. The data in the Set must be of the same length, such as an INT16_t Set. When an int32_t data is inserted, all data is converted to int32_t (no degradation is supported).

HashTable: For a Set, the value of hashTable is always NULL.

Application scenario: If you want to store a list and need to perform data reloading, you can use set. In addition, Redis also provides Set to find the intersection, union, difference and other operations, such as weibo [common concern] function, you can use Set to achieve.

ZSet / Sorted Set

Compared with Set, ZSet adds a parameter score, according to which the elements in the Set are arranged in order.

The encoding of ordered collections can be ziplist or a combination of skipList and hashTable.

Ziplist: Similar to Hash, elements and scores are stored in order; It is suitable for scenarios where the element content is small.

SkipList + hashTable: is a kind of to add, remove, update operations such as more efficient data structure elements, the jump table data structure, I will send a recent article introduced separately.

Application scenario: orderly + heavy scene, such as the students who often play games, should not be unfamiliar with various rankings, you can use ZSet to achieve.

Uncle will point code | article “original”


Please pay attention to the uncle who can code