Refer to the link: www.jianshu.com/p/160fb0f73… If you want to see the source code just look at the above link ~~~~~~~~~~~~~~~~~~~

The underlying storage of Redis is a hash structure, similar to HashMap, which is composed of a data + list, and then the zipper method resolves the conflict

2 The hash node contains key, value, and next Pointers, as shown below

/* * Hash table nodes */
typedef struct dictEntry {
/ / key
void *key;
/ / value
union {
    void *val;
    uint64_t u64;
    int64_t s64;
} v;
// Point to the next hash table node to form a linked list
struct dictEntry *next;
} dictEntry;
Copy the code

3 redisObject is a data structure that stores the most atomic data in Redis Server

The void * PTR is going to point to the actual data structure, so the key and value in our set key value are actually PTR to the actual location

typedef struct redisObject {
    / / type
    unsigned type:4;
    / / code
    unsigned encoding:4;
    // The last time the object was accessed
    unsigned lru:REDIS_LRU_BITS; /* lru time (relative to server.lruclock) */
    // Reference count
    int refcount;
    // A pointer to the actual value
    void *ptr;
} robj;
Copy the code

4 Redis internal String data structure!! Focus!!!!!

Its storage conversion priority is as follows: REDIS_ENCODING_EMBSTR_SIZE_LIMIT if the online threshold is 39 I have tested that the threshold of 44 will only be converted in Redis 4.0.0.) will be converted to embstr 3. Otherwise, it will be stored in SDS structure (i.e. Raw below).

4.1 Comparison between embstr and RAW

The creation of embstr requires only one allocation of memory, whereas raw allocates the object twice (once for SDS and once for redisObject, embstr saves the first allocation). In contrast, the number of times memory is freed is changed from two to one. The disadvantages: Redis does not provide any way to modify the embstr, i.e. the embstr is read-only. Changes to embstr are actually converted to RAW and then modified.

4.2 Data structure of SDS

/* * Hold the structure of the string object */struct sdshdr {
// The length of occupied space in buf
int len;
// The length of the remaining free space in buf
int free;
// Data space
char buf[];
};
Copy the code

It can be seen that it consists of Len, free and BUf. Other knowledge points:

1 Process of SDS creation — allocate memory and initialize len and free fields

2 SDS memory expansion (space preallocation) — If the length of the string is smaller than SDS_MAX_PREALLOC (1024*1024), then double the speed of expansion. If the length of the string is larger than SDS_MAX_PREALLOC, Then expand at the rate of +SDS_MAX_PREALLOC. If the size is <= 1MB, the capacity is doubled each time. If the size is > 1MB, the capacity is increased by +1MB each time

3 SDS Memory reduction (lazy space release) : During memory release, the LEN and free fields are modified without releasing the occupied memory. Puts the terminator first (lazily deleting the contents of the BUf) and adds the length of the buF to free

Note the point diagram below