当前位置:网站首页>Implementation principle of redis string and sorted set
Implementation principle of redis string and sorted set
2022-07-04 08:51:00 【Attacking Xiao Wang 666】
character string
yes Redis The most common type of data storage in , Its underlying implementation is a simple dynamic string sds(simple dynamic string), Is a string that can be modified .
It uses pre allocation of redundant space to reduce the frequent allocation of memory .
When the string length is less than 1M when , Expansion is to double the existing space , If exceeded 1M, When expanding, it will only expand more at one time 1M Space .( The maximum length of the string is 512M)
So when more than 512M When an error
Every sds.h/sdshdr The structure represents a SDS Value
struct sdshdr{
// Record buf Number of bytes used in the array
// be equal to sds The length of the saved string
int len;
// Record buf Unused data in
int free;
// A character array , To hold strings
char buf[];
}
That is, every time you create one sds, Will perform the above operations , Will apply for additional 8 Only bytes are saved len(4 byte ) and free(4 byte ) Information .
sorted set Ordered set
The bottom layer is a jump table , Jump list will maintain multiple index linked lists and original linked lists ,
Constantly improve new key nodes to form a new ordered linked list , Change time through space .
The simplest example
Insert logic
Insert new elements into the linked list , Just compare the value of the new element with the key list , This will reduce the number of comparisons .( Similar index )
Key node extraction logic
Because in the jump table , Use space for time , There will be multiple levels of index linked lists , Each level of the index linked list is half of the previous level , When the amount of data is large , It can easily reduce the performance of linked list query .
As for the limit of extraction , There are only two nodes in the same layer *
Extract new inodes
Jump table adopts random method , After insertion, it will determine whether the newly inserted element has become a new key
Skip the process of inserting a node
1. The new node is compared with each layer index node one by one , Determine the insertion position of the original list O(logN)
2. Insert the node into the original linked list ,O(1)
3. Using the random way of flipping a coin , Decide whether the new node is promoted to the next level index .O(logN)
Overall, the time complexity of jump table insertion is O(logN), The space complexity is O(N)
Process of node deletion
1. Find the corresponding node in the index layer and delete , Delete the same node of each layer O(logN)
3. If there is only one node left in a layer after deleting nodes , Then the linked list can be deleted .O(logN)
The time complexity of jump table deletion is O(logN)
SortedSet Typical usage scenarios
Ranking List
边栏推荐
- Clion console output Chinese garbled code
- [CV] Wu Enda machine learning course notes | Chapter 9
- Basic operations of databases and tables ----- view data tables
- [error record] no matching function for call to 'cacheflush' cacheflush();)
- 根据数字显示中文汉字
- 广和通高性能4G/5G无线模组解决方案全面推动高效、低碳智能电网
- Horizon sunrise X3 PI (I) first boot details
- Leetcode topic [array] -136- numbers that appear only once
- C#实现一个万物皆可排序的队列
- 没有Kubernetes怎么玩Dapr?
猜你喜欢
Mouse over to change the transparency of web page image
Codeforces Round #803 (Div. 2)(A-D)
manjaro安装微信
How can we make a monthly income of more than 10000? We media people with low income come and have a look
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
小程序容器技术与物联网 IoT 可以碰撞出什么样的火花
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
【无标题】转发最小二乘法
The basic syntax of mermaid in typera
Openfeign service interface call
随机推荐
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
MySQL relearn 1-centos install mysql5.7
Codeforces Round #793 (Div. 2)(A-D)
Research Report on the current market situation and development prospects of calcium sulfate whiskers in China (2022 Edition)
AcWing 244. Enigmatic cow (tree array + binary search)
awk从入门到入土(5)简单条件匹配
ArcGIS应用(二十二)Arcmap加载激光雷达las格式数据
FOC控制
How does Xiaobai buy a suitable notebook
没有Kubernetes怎么玩Dapr?
Talk about single case mode
CLion-控制台输出中文乱码
What is inner connection and outer connection? What are the uses and benefits
awk从入门到入土(14)awk输出重定向
Awk from digging into the ground to getting started (10) awk built-in functions
NewH3C——ACL
C language - Introduction - Foundation - syntax - [main function, header file] (II)
Internal learning
根据数字显示中文汉字
The basic syntax of mermaid in typera