当前位置:网站首页>Interviewer: what is the internal implementation of strings in redis?
Interviewer: what is the internal implementation of strings in redis?
2022-06-28 23:08:00 【51CTO】
While waiting in the interview room , It feels so warm , My cold rental house has to be covered with two layers of quilt to sleep . I was about to take off my coat , I suddenly heard footsteps outside the door , The door is then opened , The young man with clean clothes and beautiful face came in , A faint fragrance of men's perfume comes to us. .
interviewer :Redis What are the basic data types in ?
I :Redis The basic data types of are : character string (string)、 Hash (hash)、 list (list)、 aggregate (set)、 Ordered set (zset).
interviewer : What is the internal implementation of string type ?
I was also immersed in the complacency of the last question , Suddenly, his expression solidified , The palms began to sweat .“ This .. Not much in-depth understanding ”, I hesitated .
interviewer : Go back and wait for the news .
This sentence is clear , And then there's no then . Failure is the mother of success , I'm not discouraged , Decided to mend it right away .
Type and code
First , Understand what is type ? What is? code ? stay Redis Objects are used in to represent keys and values in memory . Each object consists of a redisObject The structure represents , There are three properties : type (type)、 code (encoding)、 Pointer to specific data (ptr).
We usually say string 、 Hash 、 list 、 aggregate 、 Ordered sets are redisObject Medium type , In fact, for each data structure Redis There are various internal coding implementations at the bottom of the system , This is to select the appropriate internal code in the appropriate scene , To achieve a balance between memory space and processing efficiency , This may be the golden mean .
In the interview , Often asked about the internal implementation 、 Internal construction 、 internals , Generally speaking, it means redisObject Medium code .
String encoding
There are three types of encoding for string types :
- int:8 A long integer of bytes .
- embstr: Less than or equal to 44 A byte string .
- raw: Greater than 44 A byte string .
stay 3.2 After the version ,embstr and raw Change into 44 Byte is the boundary , Before it was with 39 Byte is the boundary . The newer version shall prevail here .
To verify and understand , We use object encoding Command to check the internal code .
The effect of integer type is as follows :
The effect of short string type is as follows :
The effect of long string type is as follows :
Of course , I haven't fully understood the above details yet “ conquer ” interviewer , We need to go deeper :)
Simple dynamic string
stay C In language , A string is an array of characters ending with a null character . stay Redis There is no direct use of C Language string , Instead, a simple dynamic string is defined (Simple Dynamic String,SDS) Structure , And take it as Redis The default string represents .
Simple dynamic strings have three properties :
len: Record buf Number of bytes used in the character arrayfree: Record buf The number of bytes used in the character arraybuf[]: A character array , To hold strings
In order to understand , Let's take an example :
that , The corresponding simple dynamic string is like this :

among ,len by 7, Indicates that a simple dynamic string is saved 7 A byte string ;free by 0, Indicates that this simple dynamic string does not allocate unused space ;buf It's an array of characters , An array of former 7 Each byte holds O、n、e、M、o、r、e character , The last byte is a null character \0.
be relative to C Language string , What are the benefits of simple dynamic strings ?
- The time complexity of getting the string length is O(1).
- Can save byte array , Support secure binary data storage .
- The pre allocation mechanism of memory space is implemented internally , Reduce the number of memory space allocations .
- The internal implementation of the inert deletion mechanism , Memory is not released after string reduction , As pre allocated space .
- API Is safe , No buffer overflow .
Interviewer, you'll see , You don't care about me today , Tomorrow I'll keep you up , Ha ha ha ...
reference :
《Redis Design and implementation 》
《Redis Development and operations 》
《Redis Deep Adventure : Core principles and application practice 》
I've seen it here , You and I must be predestined friends , Leave your give the thumbs-up and Focus on , It will become a great thing in the future .
边栏推荐
- Interpretation of papers (DCN) towards k-means-friendly spaces: simultaneous deep learning and clustering
- 在线文本过滤小于指定长度工具
- Zadig + sonarqube, ensuring the safety of the development process
- Online linear programming: Dual convergence, new algorithms, and regret bounds
- A password error occurred when docker downloaded the MySQL image to create a database link
- Google Earth engine (GEE) -- crop extraction and analysis using sentinel-2 data
- CIN at QT (the clearest tutorial in the whole network)
- [sword finger offer] 50 First character that appears only once
- Tanghongbin, Yaya live CTO: to truly localize, the product should not have the attribute of "origin"
- Prometeus 2.36.0 new features
猜你喜欢

Chapter V virtual memory exercise
![LeetCode 324 摆动排序 II[排序 双指针] HERODING的LeetCode之路](/img/41/b8ba8d771b7224eac1cc8c54fe9d29.png)
LeetCode 324 摆动排序 II[排序 双指针] HERODING的LeetCode之路

2022 PMP project management examination agile knowledge points (4)

【深度学习】(2) Transformer 网络解析,代码复现,附Pytorch完整代码

WEB API学习笔记1

Jointly explore digital technology and information security, and the fourth China Russia Digital Forum was successfully held

The love digital smart 2022 summit opens, sharing data strategy and building data-driven organization methodology

第五章 虚拟存储器 练习

一文读懂,WMS仓储管理系统与ERP有什么区别

第三章 处理机调度练习
随机推荐
国盛证券开户是真的安全可靠吗
Differences among CPU, GPU, TPU and NPU
WEB API学习笔记1
【深度学习】(2) Transformer 网络解析,代码复现,附Pytorch完整代码
On the necessity and solution of building a campus online teaching video convergence platform
FANUC机器人_KAREL编程入门(2)_通用IO信号的使用方法
Web API learning notes 1
Online linear programming: Dual convergence, new algorithms, and regret bounds
【深度学习】(3) Transformer 中的 Encoder 机制,附Pytorch完整代码
That's how he did it!
[gateway development] handle the IP address segment represented by CIDR when NGX nested Lua
CIN at QT (the clearest tutorial in the whole network)
Leetcode detailed explanation of stack type
Complex nested object pool (4) -- manage the object pool of multi class instances and multi-stage instances
See fengzhixia | FENGZikai, the originator of Guoman, for exclusive sale of Digital Collections
两栏布局左边图片显示部分由右边内容高度决定
Google Earth Engine(GEE)——利用sentinel-2数据进行农作物提取分析
Panxiaoming, senior vice president of IC nansha|amd and President of Greater China: process, architecture and platform optimization break through the computing boundary
小样本利器2.文本对抗+半监督 FGSM & VAT & FGM代码实现
一文搞懂shell脚本