当前位置:网站首页>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
边栏推荐
- ArcGIS application (XXII) ArcMap loading lidar Las format data
- Getting started with microservices: gateway gateway
- 【无标题】转发最小二乘法
- How to re enable local connection when the network of laptop is disabled
- Use Alibaba cloud NPM image acceleration
- [BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
- OpenFeign 服务接口调用
- Clion console output Chinese garbled code
- C language - Introduction - Foundation - syntax - [operators, type conversion] (6)
- Awk from getting started to digging in (9) circular statement
猜你喜欢
微服務入門:Gateway網關
微服务入门:Gateway网关
FOC控制
[error record] no matching function for call to 'cacheflush' cacheflush();)
awk从入门到入土(12)awk也可以写脚本,替代shell
Developers really review CSDN question and answer function, and there are many improvements~
【无标题】转发最小二乘法
Educational Codeforces Round 119 (Rated for Div. 2)
ES6 summary
How to choose solid state hard disk and mechanical hard disk in computer
随机推荐
The basic syntax of mermaid in typera
How to send pictures to the server in the form of file stream through the upload control of antd
埃氏筛+欧拉筛+区间筛
awk从入门到入土(18)gawk线上手册
[attack and defense world | WP] cat
ES6 summary
awk从入门到入土(15)awk执行外部命令
SQL statement view SQL Server 2005 version number
Basic discipline formula and unit conversion
Call Baidu map to display the current position
Awk from entry to earth (8) array
Horizon sunrise X3 PI (I) first boot details
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
DM database password policy and login restriction settings
awk从入门到入土(7)条件语句
[BSP video tutorial] stm32h7 video tutorial phase 5: MDK topic, system introduction to MDK debugging, AC5, AC6 compilers, RTE development environment and the role of various configuration items (2022-
Xcode 6 swift code completion does not work properly - Xcode 6 swift code completion not working
Awk from digging into the ground to getting started (10) awk built-in functions
C language - Introduction - Foundation - syntax - data type (4)
Relationship and operation of random events