<>Redis Common data types

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

<>redis Data model for

dictEntry
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 .
SDS
Simple Dynamic String, Simple dynamic string , Store string data .
redisObject
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

<>String

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 }
int
Saved is usable long Integer value represented by type .
embstr
Save length greater than 44 Byte string (redis3.2 Before version 39 byte , Then 44 byte ).
row
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
127.0.0.1:6379> set num 300 127.0.0.1:6379> object encoding num "int" 127.0.0.1
:6379> set key1 wealwaysbyhappyhahaha OK 127.0.0.1:6379> object encoding key1
"embstr" 127.0.0.1:6379> set key2 hahahahahahahaahahahahahahahahahahahaha OK
127.0.0.1:6379> strlen key2 (integer) 39 127.0.0.1:6379> object encoding key2
"embstr" 127.0.0.1:6379> set key2 hahahahahahahaahahahahahahahahahahahahahahaha
OK127.0.0.1:6379> object encoding key2 "raw" 127.0.0.1:6379> strlen key2 (
integer) 45
<>List

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

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 .
linkedlist

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

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

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

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;
<>Zset

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
O(logn).

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

Technology
©2019-2020 Toolsou All rights reserved,
Redis Underlying data structure A person who dissuades others from learning computer , Are not good people win10 System computer C Where's the disc ,c disc users where? (win10c Disk not found users) Freshman c Language student management system ( lower ) hospital WIFI Which family is strong ? utilize Python Script unlimited spoof girlfriend computer C language program design —— Student management system Byte runout - Test development practice - One side cool meridian python Run code at specified time 5 Best style Linux Server system recommendation Anti anxiety life after naked resignation , I believe everything will have the best arrangement