<>Redis Common data types

Redis Common data types include :String,Hash,List,Set,ZSet

<>redis Data model for

In some programming languages , The data structure of key value pairs is called a dictionary , And in Redis in , Will give everyone key-value
Assign a dictionary entity to a key value pair , namely “dicEntry”.dicEntry It consists of three parts : key Pointer to ,val Pointer to ,next Pointer ,next Pointer to next
dicteEntry Form linked list , this next Pointers can link multiple key value pairs with the same hash value , Chain address method is used to solve the problem of hash conflict .
Simple Dynamic String, Simple dynamic string , Store string data .
Redis of 5 Two common types are RedisObject To store ,redisObject Medium type Field indicates the data type of the value ( that is 5
Basic types ).ptr Field points to the address of the object .Redis Type of object , Internal coding , Memory recycling , Shared objects and other functions , All based on RedisObject Object .

<> Memory allocator

**jemalloc **
Redis take jemalloc As default memory allocator , Reduce memory fragmentation .jemalloc stay 64
Bit system , Divide memory space into small , large , Huge three ranges ; Each range is divided into many small memory block units ; When Redis When storing data , The memory block with the most appropriate size will be selected for storage .

<> Data structure of common data types


Redis Strings in are called simple dynamic strings 「SDS」, This structure is very similar Java Medium ArrayList, Its length is dynamically variable . The encoding types of strings are
int,embstr and raw Three kinds .
struct SDS { T capacity; // Array capacity T len; // Array length byte[] content; // Array contents }
Saved is usable long Integer value represented by type .
Save length greater than 44 Byte string (redis3.2 Before version 39 byte , Then 44 byte ).
Save length less than 44 Byte string (redis3.2 Before version 39 byte , Then 44 byte ).
39 Why do bytes become 44 byte
The reason for the length change is SDS
Memory optimization in .redis Memory allocator will be less than or equal to 64 Byte objects are classified as small objects ,SDS Middle Division content External , Other fields take up less memory , Results in available for assignment to content Array space is too large .

test> set num 300> object encoding num "int"
:6379> set key1 wealwaysbyhappyhahaha OK> object encoding key1
"embstr"> set key2 hahahahahahahaahahahahahahahahahahahaha OK> strlen key2 (integer) 39> object encoding key2
"embstr"> set key2 hahahahahahahaahahahahahahahahahahahahahahaha
OK127.0.0.1:6379> object encoding key2 "raw"> strlen key2 (
integer) 45

list use quickList realization , In the old version ,list use ziplist Compress list or linkedlist Double ended list implementation .

When the amount of list data is small , use ziplist realization . Each element exists as an array , It's just next to each other , There is no need to record memory overhead such as front and back elements , So save memory . Due to the small amount of data , Even if you insert it from somewhere in the middle , Very fast, too .

Double ended list image Java of linkedList, As the amount of data increases , If still used ziplist, The cost of inserting elements from the middle is also increasing , therefore , How to use linked list , Better performance . Include front and back pointers , Head and tail pointer, etc . But it takes more space .

quickList It has reached the extreme in time and space .quicklist By ziplist Two way linked list , Each node in the linked list is compressed into a list ziplist The structure holds the data , and ziplist There are multiple entry node , Save data . Equivalent to one quicklist A node stores a piece of data , It's no longer a data .


Hash The underlying implementation of data types is ziplist Or dictionary ( also known as hashtable). Here is the selection of compressed list or dictionary , It is also determined by the number and size of elements .

When the amount of data is small , use ziplist, When the amount of data is large , Use dictionary .

use ziplist Conditions :

* The number of elements in the hash is less than 512 individual
* The key and value string length of all key value pairs in the hash is less than 64 byte
hashtables be similar to jdk1.7 former hashmap.hashmap The chain address method is used to solve the problem of hash conflict .


Set The underlying data type can be intset( Integer set ) Or hashtable.

When the data are integers and the number is small , use intset As the underlying data structure ; When there is data other than integers or the amount of data increases , use hashtable As the underlying data structure .

intset The underlying implementation is ordered , Array without duplicates .
typedef struct intset { // Coding mode uint32_t encoding; // Number of elements contained in the collection uint32_t length
; // Save an array of elements int8_t contents[]; } intset;

Redis Medium Zset, Also called ordered sets . Its bottom layer is ziplist or skiplist( Jump table ).

When the number of elements is small ziplist, Use a jump table when there are many elements .

The jump table uses the idea of dichotomy , The bottom layer stores all the elements , Each upper layer , Reduce the probability by half . Except the bottom layer , Each layer element has a pointer to the front and back nodes of the same layer and the same node of the next layer . This is based on linked list “ Binary search ” Support fast insertion , delete , The time complexity is

zadd—zslinsert— average O(logN), worst O(N)
zrem—zsldelete— average O(logN), worst O(N)
zrank–zslGetRank— average O(logN), worst O(N)

©2019-2020 Toolsou All rights reserved,
( Super detail )Eclipse Using tutorials —— use Eclipse Create first HelloWorld! Database operation 5 code implementation mysql Addition, deletion, modification and query of database What can MCU do , Do you have any interesting works made by MCU or open source hardware Go to the interview after reading this , Steady over ~~ Single linked list of primary data structure (C Language implementation )SQL Comprehensive questions Employee unit comprehensive questions Python Implementation of Hanoi Tower code VHDL——JK trigger It's over , Starting salary 30k