当前位置:网站首页>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 .
边栏推荐
- [Chapter 2 of word tutorial series] how to set the table on each page to have a header in word
- 在线文本过滤小于指定长度工具
- 超级工厂里的生意图鉴
- 云计算的迷路者
- 【Flutter 問題系列第 71 篇】Flutter 中 Uint8List 和 Image 之間的相互轉換
- Tanghongbin, Yaya live CTO: to truly localize, the product should not have the attribute of "origin"
- Prometeus 2.36.0 新特性
- 非科班!自学之路!
- Complex nested object pool (4) -- manage the object pool of multi class instances and multi-stage instances
- [chapter 71 of the flutter problem series] mutual conversion between uint8list and image in flutter
猜你喜欢
随机推荐
在线文本过滤小于指定长度工具
Implementation of go language plug-in platform
Oracle set password complexity and timeout exit function
Business atlas in super factory
Is it safe and reliable for changtou school to help open a securities account? How to drive
浅析搭建校园在线教学视频汇聚平台的必要性及解决方案
邂逅阿维塔 11:强产品力下久违的新鲜感
Mathematical knowledge: finding combinatorial number I - finding combinatorial number
Online text filter less than specified length tool
运维排查-使用hcache插件排查Buffer/cache占用过高
See fengzhixia | FENGZikai, the originator of Guoman, for exclusive sale of Digital Collections
C language - word analysis
油猴脚本学习
Differences among CPU, GPU, TPU and NPU
【深度学习】(2) Transformer 网络解析,代码复现,附Pytorch完整代码
手机办理股票开户安全性高吗?
TDD and automated testing
[Chapter 2 of word tutorial series] how to set the table on each page to have a header in word
Do you know all the wonderful functions of the vlookup function?
The Best of Many Worlds_ Dual Mirror Descent for Online Allocation Problems








