当前位置:网站首页>[redis design and implementation] part I: summary of redis data structure and objects
[redis design and implementation] part I: summary of redis data structure and objects
2022-07-06 21:09:00 【Twilight_ years】
Simple dynamic string :SDS
【Redis note 】 Simple dynamic string (SDS)_ Twilight _ Blog years -CSDN Blog _sds Dynamic string
Linked list :linkedlist
Dictionaries :hashtable、map
【Redis note 】 Data structure and objects : Dictionaries _ Twilight _ Blog years -CSDN Blog
Skip list :skiplist
Set of integers :intset
Compressed list :ziplist
【Redis note 】 Compressed list (ziplist)_ Twilight _ Blog years -CSDN Blog
object :
The benefits of using objects :
(1) Judge whether an object can execute a given instruction according to the type of object
(2) You can set a variety of different data structures for objects according to different usage scenarios
The type and encoding of the object :
Redis Each object consists of a redisObject Structural representation
type: Record the type of object

Redis The key of is always a string object .
encoding: Determine the data structure of the object

benefits : adopt encoding Property to set the data structure used by the object , Promoted Redis Flexibility and efficiency , because Redis You can set different data structures for an object according to different usage scenarios , So as to optimize the efficiency of the object in a certain scene .
ptr The pointer : Data structures that point to the underlying implementation of the object
String object :
Three coding methods :int,raw,embstr
(1) If a string object holds an integer value , And this integer can be used long Type said , Then the string object will store the integer value in the string object structure ptr Properties of the , And will encoding Set to REDIS_ECODING_INT.
(2) If the string object holds a string value , And the length of this string value is greater than 32 byte , Then the string object will use SDS To save the string value , And set the object's encoding to REDIS_ENCODING_RAW
(3) If the string object holds a string value , And the length of this string value is less than or equal to 32 byte , Then the string object uses embstr Code to save the string value .

explain :long double Floating point numbers of type are Redis Is also saved as a string value . Floating point numbers <-> character string
Encoding conversion :
int->raw: Objects no longer hold integer values
embstr->raw: because embstr read-only , If the embstr Execute any modification commands , The program first encodes the object from embstr Convert to raw, Then execute the modify command .
List objects :
Two data structure implementation methods :ziplist,linkedlist
1、 Use ziplist The implementation , Each compressed list node holds a list element .
2、 Use linkedlist The implementation , Each double ended linked list node stores a string object , Each string object holds a list element .
String object Redis5 Objects of each type are the only objects that will be nested by objects of the other four types
Encoding conversion :

Hash object :
Two ways of implementation :ziplist,hashtable
1、 Use ziplist The implementation , Whenever a new key value pair is added to the hash object , The program will first save the key node and add it to the end of the table , Then add the value node to the end of the table
(1) Two nodes of the same key value pair are always close together
(2) The key value pairs added first are in the header , The key value pair added after is at the end of the table .
2、 Use hashtable The implementation , Key value pairs are saved using dictionary key value pairs . Every key and value in the dictionary is a string object .
Code conversion :

A collection of objects :
Two ways of coding :intset,hashtable
1、 Use intset when , All elements of the collection object are saved in intset in .
2、 Use hashtable when , Each key in the dictionary is a string object , Each string object contains a collection element , All values are set to null
Encoding conversion :

An orderly collection of objects :
Two ways of coding :ziplist,skiplist
1、 Use ziplist when , Each set uses two compressed list nodes next to each other to save , The first node holds the members of the element , The second element holds the score of the element .
The set elements in the compressed list are sorted from small to large , Elements with smaller scores are placed close to the heading , Elements with larger branches are placed in the direction of the footer .
2、 Use skiplist when , Use zset Structure implementation , Including dictionary and jump table .
(1) The jump table saves all element sets from small to large according to the score , Each hop table node holds a set element :object Attribute save member ,score Save score . If you only use dictionaries , Keep an orderly time O(logn), Space O(n)
(2) The dictionary creates a mapping from members to scores for an ordered set , In the dictionary Each key value pair of the holds a collection element . Can pass O(1) Find the score of a given member . If you only use the jump table ,O(logn)
(3)zset Structure uses both jump tables and dictionaries to store set elements , But they share members and scores of the same element through pointers . So using them at the same time won't waste extra memory .
Encoding conversion :

Redis Properties of objects :
Type checking :
Redis There are two kinds of commands for operating keys :
You can perform... On any type of key ; Can only be performed on a specific type of key .
Before executing a type specific command ,Redis Will check the type of key , Then decide whether to execute the command .
Command polymorphism :
Redis According to the encoding method of the value object , Choose the appropriate code to implement the command .
Type based polymorphism : No matter what type of key you enter , Commands can be executed correctly
Coding based polymorphism : One command can be used to handle many different codes at the same time
Memory recovery :
Redis A reference counting mechanism is built in the object system to realize memory recycling , The program can track the reference count information of the object , Automatically release objects and reclaim memory when appropriate .

Object sharing :
Object reference counting can realize object sharing .
stay Redis in , An object that allows multiple keys to share the same value :
(1) Let the value pointer of the key point to an existing object .
(2) Add the reference count of the shared object 1
The shared object mechanism is helpful to save memory , The more objects with the same value are saved , Object sharing mechanism can save more memory .
(1)Redis When the server is initialized , establish 0~9999 String objects of all integer values are shared .
(2)Redis Only string objects containing integer values are shared

The idle time of the object :

边栏推荐
- 请问sql group by 语句问题
- Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
- 3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
- Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
- Study notes of grain Mall - phase I: Project Introduction
- HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
- R語言可視化兩個以上的分類(類別)變量之間的關系、使用vcd包中的Mosaic函數創建馬賽克圖( Mosaic plots)、分別可視化兩個、三個、四個分類變量的關系的馬賽克圖
- Huawei device command
- 1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
- What is the problem with the SQL group by statement
猜你喜欢

The mail command is used in combination with the pipeline command statement

Is it profitable to host an Olympic Games?

Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?

MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.

15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient

Performance test process and plan

防火墙基础之外网服务器区部署和双机热备

Comprehensive evaluation and recommendation of the most comprehensive knowledge base management tools in the whole network: flowus, baklib, jiandaoyun, ones wiki, pingcode, seed, mebox, Yifang cloud,

15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace

HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
随机推荐
Reference frame generation based on deep learning
每个程序员必须掌握的常用英语词汇(建议收藏)
如何实现常见框架
R语言可视化两个以上的分类(类别)变量之间的关系、使用vcd包中的Mosaic函数创建马赛克图( Mosaic plots)、分别可视化两个、三个、四个分类变量的关系的马赛克图
Performance test process and plan
'class file has wrong version 52.0, should be 50.0' - class file has wrong version 52.0, should be 50.0
15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace
el-table表格——获取单击的是第几行和第几列 & 表格排序之el-table与sort-change、el-table-column与sort-method & 清除排序-clearSort
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
The mail command is used in combination with the pipeline command statement
js 根据汉字首字母排序(省份排序) 或 根据英文首字母排序——za排序 & az排序
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
None of the strongest kings in the monitoring industry!
Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition
Nodejs教程之让我们用 typescript 创建你的第一个 expressjs 应用程序
SAP UI5 框架的 manifest.json
el-table表格——sortable排序 & 出现小数、%时排序错乱
Reinforcement learning - learning notes 5 | alphago
【微信小程序】運行機制和更新機制