The data structure mentioned here is for redis internal storage key-value. Other data structures such as redis configuration are not covered in this article. Redis Data Structure dict, hash table, redis all key-values are stored in it. Hash table (dictionary) data structure, all key-value pairs of redis are stored in this
The data structure mentioned here is for redis internal storage key-value. Other data structures such as redis configuration are not covered in this article. Redis Data Structure dict, hash table, redis all key-values are stored in it. // Hash table (dictionary) data structure. All key-value pairs of redis are stored in this
The data structure mentioned here is for redis internal storage key-value. Other data structures such as redis configuration are not covered in this article.
Redis Data Structure
Dict, Hash table, all key-values of redis are stored in it.
// Hash table (dictionary) data structure. All key-value pairs of redis are stored here. It contains two hash tables. Typedef struct dict {// hash table type, including hash function, comparison function, and key value memory release function dictType * type; // stores some additional data void * privdata; // two hash tables dictht ht [2]; // The hash table resets the subscript, specifying the array subscript int rehashidx of the hash array; /* rehashing not in progress if rehashidx =-1 * // number of iterators bound to the hash table int iterators;/* number of iterators currently running */} dict;
RedisObject, Any value will be encapsulated into a redisObject. The redisObject can specify the value type, encoding method, and other data attributes.
Typedef struct redisObject {// exactly 32 bits // object type, string/LIST/set/hash table unsigned type: 4; // unused two unsigned notused bits: 2; /* Not used * // encoding method. To save space, redis provides multiple methods to save a data. // For example, "123456789" is stored as an integer of 123456789 unsigned encoding: 4; // use unsigned lru: 22 when the memory is insufficient and data is eliminated;/* lru time (relative to server. lruclock) * // reference count int refcount; // Data Pointer void * ptr;} robj;
Zset, Is a jump table, insert and delete very fast.
Typedef struct zset {// hash table dict * dict; // hop table zskiplist * zsl;} zset;
Adlist, A common double-stranded table.
Typedef struct list {// head pointer listNode * head; // tail pointer listNode * tail; // data copy function pointer void * (* dup) (void * ptr ); // destructor pointer void (* free) (void * ptr); // data comparison pointer int (* match) (void * ptr, void * key ); // chain table length: unsigned long len;} list;
ZiplistIs a compressed dual-chain table that optimizes the CPU cache. Ziplist is actually a string that uses a series of algorithms to compress double-chain tables.
Intset, An integer set.
Typedef struct intset {// type of each integer uint32_t encoding; // intset length uint32_t length; // integer array int8_t contents [];} intset;
Sds, String data structure. Because String operations are often involved, redis has implemented a special implementation. This is called Hacking String in this document.
typedef char *sds;
Zipmap, Has been deprecated. I will not discuss this data structure.
Redis commands and related data structures
Take the command SET, HSET, LPUSH, SADD, and ZADD for adding data as an example to see which data structures are used at the underlying layer of the command.
SETThe underlying layer of the command is sds, int, long, float, or double. Different data classes are used in different situations. The data types used at the underlying layer of the SET are the simplest.
HSETAt the underlying layer of the command, ziplist is used to compress the double-chain table, instead of the hash table dict.
LPUSHAt the underlying layer of the command, ziplist is used to compress the double-chain table.
SADDThe command is special. SADD is oriented to a set ). If all the data added to the set is an integer, intset is used. If one of the data in the set is not an integer, The dict hash table is used. Therefore, in a special case, if the first N data are all integers and the N + 1 data is not integers, such as strings, the data structure is converted from intset to dict.
ZADDAlso, SADD targets a sorted set ). The bottom-layer data structure of ZADD can be combined with the hop table skiplist and the hash table dict, or ziplist. The specific configuration variables of server. zset_max_ziplist_entries and server. zset_max_ziplist_value are required. The former is blended with dict to quickly find whether a member exists in the hop table. A more common application of Ordered Sets is ranking.
In the next series of articles, I will explain each data structure and the purpose of selecting the corresponding data structure.
Chaos
Http://daoluan.net
Original article address: redis Data Structure summary, thanks to the original author for sharing.