当前位置:网站首页>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
边栏推荐
- The upper layer route cannot Ping the lower layer route
- How to solve the problem of computer jam and slow down
- Industry depression has the advantages of industry depression
- C语言-入门-基础-语法-[主函数,头文件](二)
- 老掉牙的 synchronized 锁优化,一次给你讲清楚!
- [untitled] 2022 polymerization process analysis and polymerization process simulation examination
- @Role of requestparam annotation
- C # implements a queue in which everything can be sorted
- 学习Nuxt.js
- awk从入门到入土(6)正则匹配
猜你喜欢
FOC control
Question 49: how to quickly determine the impact of IO latency on MySQL performance
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)
Educational Codeforces Round 115 (Rated for Div. 2)
The basic syntax of mermaid in typera
HMS core helps baby bus show high-quality children's digital content to global developers
微服务入门:Gateway网关
Fault analysis | MySQL: unique key constraint failure
DM8 command line installation and database creation
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
随机推荐
C # implements a queue in which everything can be sorted
C, Numerical Recipes in C, solution of linear algebraic equations, Gauss Jordan elimination method, source code
没有Kubernetes怎么玩Dapr?
地平线 旭日X3 PI (一)首次开机细节
2022 examination questions for safety managers of metal and nonmetal mines (underground mines) and examination papers for safety managers of metal and nonmetal mines (underground mines)
没有Kubernetes怎么玩Dapr?
C#,数值计算(Numerical Recipes in C#),线性代数方程的求解,Gauss-Jordan消去法,源代码
go-zero微服务实战系列(九、极致优化秒杀性能)
Private collection project practice sharing [Yugong series] February 2022 U3D full stack class 007 - production and setting skybox resources
C language - Introduction - Foundation - syntax - [identifier, keyword, semicolon, space, comment, input and output] (III)
FOC控制
awk从入门到入土(11)awk getline函数详解
Ehrlich sieve + Euler sieve + interval sieve
微服務入門:Gateway網關
awk从入门到入土(9)循环语句
Awk from getting started to digging in (9) circular statement
What if I forget the router password
Basic discipline formula and unit conversion
There are 100 people eating 100 apples, one adult eating 4 apples, and four children eating 1 apple. How can they eat exactly 100 apples? Use any high-level language you are familiar with
Question 49: how to quickly determine the impact of IO latency on MySQL performance