当前位置:网站首页>Redis advanced - correspondence between object and code base
Redis advanced - correspondence between object and code base
2022-06-12 00:37:00 【Noblegasesgoo】
1. String object
String type yes redis Basic data types , Not only all key All are String type , Other data types , The elements of other data types are also strings , Note, however, that the length of the string cannot exceed 512M.
1.1 code
- int code : What's kept is that you can use long Type means An integer value .
- embstr code : preservation
length < 44 byteString (redis3.2 Before the version was 39 byte , And then 44 byte ). - raw code : preservation
length > 44 byteString (redis3.2 Before the version was 39 byte , And then 44 byte ).
As can be seen from the above ,int Encoding is used to hold integer values , then embstr Encoding is used to save short strings ,raw Encoding is used to hold long strings .
1.2 Memory layout

1.3 raw and embstr The difference between
raw and embstr Is to use redisObject and sds Save data , Their differences are as follows :
- embstr Use of allocates memory space only once ( This is the result of redisObject And sds Memory is contiguous and contiguous Why ),raw It needs to be allocated twice ( To assign separately to redisObject and sds).
So with raw comparison ,embstr The advantage of the is that less space is allocated when creating , Free space less once when deleting , And all the data of the object is continuous space , Convenient query . and embstr There are also disadvantages , If it exceeds the size , To reallocate memory space .
So to avoid reallocating memory ,redis Medium embstr Implemented as a read-only .
notes :Redis Floating point type is also saved as string in , Switch back when necessary .
1.4 Code conversion
When int code Saved values No longer an integer , or The size is over long The scope of the when , Automatically convert to raw.
about embstr code , Because in redis It is read-only , In the face of embstr object When making modifications , Will be converted into raw Modify again , therefore , As long as it's a modification embstr object , The modified object must be raw Of , Whether it's reached or not 44 Bytes .
2. List objects
list list , It's a simple list of strings , Sort by insertion order , Because the bottom layer is actually a linked list , So you can plug it in .
2.1 code
The encoding of the list object is quicklist( Quick list ).
In previous versions linked and ziplist These two codes . further , at present Redis Defined 10 Object encoding method macro naming , Two of them are completely idle
OBJ_ENCODING_ZIPMAPAndOBJ_ENCODING_LINKEDLIST. from Redis In the evolution history of , The former is the encoding value that may be supported later ( The code is still ), The latter should be completely eliminated .
2.2 Memory layout

3. Hash object
Hash object Key is a string type , A value is a collection of key value pairs .
3.1 code
The encoding of hash objects can be implemented ziplist perhaps hashtable.
There are two corresponding underlying implementations , One is ziplist One is dict.
3.2 Memory layout

Pay attention to the imprecision in the above figure :
- ziplist Each of them entry, In addition to the binary data of the key and the value itself , Other fields are also included , But it is not drawn in the picture
- dict The bottom layer may hold two dictht example
- There is no drawing of dict Hash conflict for
It should be noted that : When HT code , namely Use dict As the underlying data structure of the hash object , Both keys and values are in sds Is stored in the form of .
3.3 Illustrate with examples
The copyright belongs to https://pdai.tech all . link :https://www.pdai.tech/md/db/nosql-redis/db-redis-data-type-enc.html
When using ziplist, That is, when the compressed list is used as the underlying implementation , The new key value pairs are saved to the end of the compressed list , For example, execute the following command :
hset profile name "Tom"
hset profile age 25
hset profile career "Programmer"
If you use ziplist,profile Store as follows :

When using hashtable When coding , The above commands are stored as follows :

hashtable Encoded hash table objects use dictionary data structure at the bottom , Each key value pair in the hash object uses a dictionary key value pair .
When we talked about compressed lists earlier , The compressed list we introduced is Redis Developed to save memory , It is a sequential data structure composed of a series of special coded continuous memory blocks , Relative to dictionary data structure , Compressed list for fewer elements 、 Scenes with small element length . The advantage is centralized storage , Save a space .
3.4 Encoding conversion
And the above list object ziplist The coding is the same , When both of the following conditions are satisfied , Use ziplist code :
The total number of elements saved in the list < 512 individual, This condition can be set through the... In the configuration fileset-max-intset-entriesMake changesLength of each element < 64 byte
If these two conditions are not met, use hashtable code .
4. Set object
A collection of objects set yes string type ( The whole number will also be changed into string Type to store ) The unordered set of .
Notice the difference between sets and lists :
- Elements in a collection are unordered , Therefore, an element cannot be manipulated through an index
- Elements in a collection cannot be repeated
4.1 code
The code of the collection object can be intset perhaps hashtable;
There are two underlying implementations :
- intset: Centralized storage can only be Numerical data , also It has to be an integer .
- dict: take == All data is stored in dict In the key of , The value field is idle ==.

4.2 Illustrate with examples
SADD numbers 1 3 5

SADD Dfruits "apple" "banana" "cherry"

4.3 Encoding conversion
When the following two conditions are satisfied in the case of collective contract , Use intset code :
- All elements in a collection object are integers
- All elements of the collection object cannot exceed 512 individual ( You can use the of the configuration file
set-max-intset-entriesTo configure )
If you can't meet these two conditions, use hashtable code .
5. Zset object
And the above set Object comparison ,zset Objects are ordered , Different from using index subscript as sorting basis in list ,zset Set a for each element score Sort by .
5.1 code
zset There are still two underlying implementations of objects :
- The first one is :ziplist As an underlying implementation , The corresponding code value macro is ZIPLIST
- Use ziplist To achieve an ordered set , Only need ziplist On the basis of this data structure, just sort and de duplicate .
- The second kind :dict And skiplist Together as the underlying implementation , The corresponding code value macro is SKIPLIST
- Use skiplist To achieve an ordered set , Let's talk about it in detail .
First encode as ZIPLIST When ,zset The memory layout of is as follows :

Encoded as skiplist When ,zset The memory layout of is as follows :

In fact, ordered set can be realized by using dictionary or jump table alone , But the two data structures are combined , The effect can be superimposed .
Because if we use it alone dict , Although it can O(1) Time complexity to find the score of members , But because dictionaries store elements in an unordered way , So every time you do a range lookup , All have to be sorted ;
If we use it alone skiplist To achieve , You can perform a range lookup operation , But the lookup operation starts from O(1) -> O(logN).
therefore Redis Use them together to implement zset.
5.2 Illustrate with examples
The copyright belongs to https://pdai.tech all . link :https://www.pdai.tech/md/db/nosql-redis/db-redis-data-type-enc.html
ZADD price 8.5 apple 5.0 banana 6.0 cherry

![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-mOZ1Tnnf-1646041617394)(2021-12-15-redis The corresponding relationship between the object and the code base is explained in detail .assets/db-redis-x-object-14.png)]](/img/11/6d52af1179c6db6298687ec197dd50.jpg)
5.3 Encoding conversion
When the ordered set contract satisfies the following two conditions , Object use ziplist code :
Save the number of elements < 128The length of all saved elements < 64 byte
If the above two conditions cannot be met, use skiplist and dict Combined data structures .
Again , The above two conditions can also be modified in the configuration file , The corresponding fields are zset-max-ziplist-entries and zset-max-ziplist-value.
Code cloud warehouse synchronization notes , You can take it yourself. Welcome star correct :https://gitee.com/noblegasesgoo/notes
If something goes wrong, I hope the leaders in the comment area can discuss and correct each other , Maintain the health of the community. Let's work together , There is no tolerance for wrong knowledge .
—————————————————————— Love you noblegasesgoo
边栏推荐
- How much does it cost to develop s2b2c mall system
- [case] building a universal data lake for Fuguo fund based on star ring technology data cloud platform TDC
- How to optimize plantuml flow diagram (sequence diagram)
- DevOps落地实践点滴和踩坑记录-(1)
- 手机wps如何压缩文件打包发送
- win7 fps优化的方法
- How to change the font size of Apple phone WPS
- 七个需要关注的测试自动化趋势
- DPT-FSNET: DUAL-PATH TRANSFORMER BASED FULL-BAND AND SUB-BAND FUSION NETWORK FOR SPEECH ENHANCEMENT
- Pleasant and burden free cross process communication
猜你喜欢

Month selector disable data after the current month to include the current month

UVM: transaction level modeling (TLM) 1.0 communication standard

Optimization method of win7 FPS

The road of global evolution of vivo global mall -- multilingual solution

gin解决跨域问题

Do you want to take the postgraduate entrance examination? Will you be able to find a good job after graduate school?

Experiment 5 constructor and destructor

oracle使用Navicat工具导入导出数据

ironSource&nbsp; New functions are released, and developers can adjust advertising strategies in real time in the same session

Cube technology interpretation | detailed explanation of cube applet Technology
随机推荐
[day 6 of JUC learning] concurrent calculator and thread random factor
Collation of common array functions
Experiment 5 constructor and destructor
設計消息隊列存儲消息數據的 MySQL 錶格
使用 select 切换协程
Argodb 3.2 of star ring technology was officially released to comprehensively upgrade ease of use, performance and security
DPT-FSNET: DUAL-PATH TRANSFORMER BASED FULL-BAND AND SUB-BAND FUSION NETWORK FOR SPEECH ENHANCEMENT
Optimization method of win7 FPS
组态王如何利用无线Host-Link通信模块远程采集PLC数据?
App startup process: from clicking the application icon to the activity startup process
Oracle uses Navicat tool to import and export data
How to optimize plantuml flow diagram (sequence diagram)
SAP SD create / modify price list
Hertzbeat v1.0 beta. 4 release, easy-to-use and friendly monitoring alarm system
Design principle [Demeter's Law]
Redis的主从复制、哨兵模式和集群
Characteristics of JS logical operators
Experiment 6 constructor + copy construction
Streaming Data Warehouse 存储:需求与架构
Web keyboard input method application development guide (2) -- keyboard events