当前位置:网站首页>Redis core technology and basic architecture of actual combat: what does a key value database contain?

Redis core technology and basic architecture of actual combat: what does a key value database contain?

2022-07-06 06:19:00 Miapenso

Hello , I'm Jiang Dejun .

We know ,Redis Is a typical key value database , So today , I'm going to take you hand in hand to build a simple key value database . Why do you want to do this ?

Remember what I said at the beginning ?Redis It's complicated , If we directly study specific technical points as soon as we come up , such as “ Single thread ”“ cache ” etc. , Although you can learn the specific content directly , Even small problems can be solved immediately , But this way , It's easy to get lost in the details .

From my own experience , A better way to learn is to establish “ System view ”. That means , If we want to understand and optimize Redis, We must have a global understanding of its overall architecture and key modules , Then go deep into the specific technical points . This is also a way of teaching that we insist on in this course .

I Believe , After such a process , When we position and solve problems in practice , It's a lot easier , And you can also transfer this learning method to other learning activities . I hope you can thoroughly grasp this learning idea , Let your study 、 Work more efficiently .

Far away , Let's go back to the topic of today's course . today , When constructing this simple key value database , We only need to focus on the overall architecture and core modules . This is equivalent to medicine before the formal anatomy of the human body , Will dissect a white mouse first . By dissecting the simplest key value database , To quickly grasp learning and tuning Redis The key to .

I call this simple key value database SimpleKV. It should be noted that ,GitHub There is also a named SimpleKV Project , That's what I'm talking about SimpleKV It's not the same thing , I'm just talking about a key value database architecture with key components .

Okay , Are you ready to , Then let's build it together SimpleKV Well .

Start building SimpleKV when , First of all, we should consider what kind of data can be stored in it , What operations can be done on the data , That is, data model and operation interface . They seem simple , In fact, we understand Redis Often used to cache 、 seckill 、 Distributed locks are an important foundation for scenarios such as .

Understand the data model , You will understand , Why in some scenes , Data originally saved using a relational database , You can also use the key value database to save . for example , User information ( user ID、 full name 、 Age 、 Gender, etc ) It is usually stored in a relational database , In this scenario , A user ID Corresponding to a user information set , This is a data model of key value database , It can also fulfill this storage requirement .

however , If you only know the data model , Without understanding the operation interface , May not understand , Why in some scenes , It's not appropriate to use the key value database . for example , Also in the above scenario , If you want to calculate the average age of multiple users , The key database cannot be completed . Because it only provides a simple operation interface , Complex aggregate calculations cannot be supported .

that , about Redis Come on , What can it do , What can't be done ? Only understand its data model and operation interface first , We can really put “ This good steel is used on the blade ”.

Next , Let's first look at what data can be stored .

What data can be stored ?

For key value databases , The basic data model is key-value Model . for example ,“hello”: “world” It's a basic KV Yes , among ,“hello” yes key,“world” yes value.SimpleKV No exception . stay SimpleKV in ,key yes String type , and value It's the basic data type , for example String、 Integers, etc .

however ,SimpleKV After all, it is a simple key value database , For the key value database in the actual production environment ,value Types can also be complex types .

Different key value databases support key The types generally vary little , and value Types are quite different . When we select the key value database , An important consideration is that it supports value type . for example ,Memcached Supported by value Type is only String type , and Redis Supported by value Types include String、 Hashtable 、 list 、 Collection etc. .Redis It can be widely used in actual business scenarios , It benefits from supporting diversified types of value.

From the perspective of use , Different value Type implementation , It can not only support the data needs of different businesses , It also implies that different data structures have different performance 、 Differences in space efficiency , This leads to different value There are differences between operations .

Only a deep understanding of the principle behind this , We can choose Redis value Type and optimization Redis Performance time , Be at ease .

What can be done with the data ?

Know the data model , Next , We'll look at its basic operations on data .SimpleKV Is a simple key value database , therefore , The basic operation is nothing more than adding, deleting, modifying and checking .

Let's get to know SimpleKV Need support 3 Basic operations , namely PUT、GET and DELETE.

  • PUT: Write or update a new key-value Yes ;
  • GET: According to a key Read the corresponding value value ;
  • DELETE: According to a key Delete the whole key-value Yes .

It should be noted that , Some key value databases are newly written / The update operation is called SET. Although new writes and updates use an operation interface , But in practice , Will be based on key Execute the corresponding new write or update process based on whether it exists .

In the actual business scenario , We often encounter this kind of situation : Query the access records of a user in a period of time . This operation belongs to... In the key value database SCAN operation , According to a paragraph key Returns the corresponding value value . therefore ,PUT/GET/DELETE/SCAN Is a basic operation set of key value database .

Besides , The actual business scenario usually has richer requirements , for example , In the black and white list application , You need to determine whether a user exists . If the user's ID As key, that , Can increase EXISTS Operation interface , Used to judge a key Whether there is . For a specific key value database , You can check the operation document , Understand its detailed operation interface .

Of course , When a key value database value When the types are diversified , You need to include the corresponding operation interface . for example ,Redis Of value There are list types , Therefore, its interface should include a pair of lists value The operation of . I will also introduce in detail later , Different operations on Redis The impact of access efficiency .

Speaking of which , The data model and operation interface are constructed , This is our basic work . The next? , We need to go further , Consider a very important design problem : Whether key value pairs are stored in memory or external memory

The advantage of saving in memory is that it can read and write quickly , After all, the access speed of memory is generally 100 ns Level . however , The potential risk is that once the power goes off , All data will be lost .

Save in external storage , Although data loss can be avoided , But limited by the slow reading and writing of the disk ( What time is it usually ms Level ), The overall performance of the key value database will be reduced .

therefore , How to make design choices , We usually need to consider the main application scenarios of key value database . such as , The data in the cache scenario needs to be quickly accessible but allowed to be lost , that , The key value database used in this scenario usually stores key value data in memory .Memcached and Redis All belong to the memory key database . about Redis for , Caching is a very important application scenario . Later I will focus on Redis The key mechanism used as a cache 、 advantage , And common optimization methods .

In order to and Redis bring into correspondence with , our SimpleKV Use memory to store key value data . Next , Let's get to know SimpleKV The basic components of .

In general , A key value database includes an access framework 、 Index module 、 Operation module and storage module ( See the picture below ). Next , Let's start with these four parts , Continue to build our SimpleKV.

  What access mode is used ?

There are usually two access modes : One is It can be used by external applications through function library calls , such as , In the picture above libsimplekv.so, It is linked to our own program in the form of dynamic link library , Provide key value storage function ; The other is Through the network framework to Socket Provide key value pair operation in the form of communication , This form can provide a wide range of key value storage services . In the diagram above , We can see , The network framework includes Socket Server And protocol analysis .

Different key value database servers and clients have different interaction protocols , We are redeveloping the key value database 、 When adding new functions , We must understand and master the communication protocol of key value database , In this way, a compatible client can be developed .

The actual key value database also basically adopts the above two methods , for example ,RocksDB Use in the form of dynamic link library , and Memcached and Redis Is accessed through the network framework . I'll introduce you later Redis Existing client and communication protocols .

Provide key value storage service through network framework , On the one hand, it expands the use of key value database , But on the other hand , It also gives the performance of the key value database 、 The running model provides different design options , It brings some potential problems .

for instance , When the client sends the following command , The command will be encapsulated in a network packet and sent to the key database :

PUT hello world

The key database network framework received the network packet , And analyze it according to the corresponding protocol , We can know , The client wants to write a key value pair , And start the actual writing process . here , We will encounter a system design problem , Simply speaking , Is the processing of network connection 、 Resolution of network request , And data access processing , Is to use a thread 、 Multiple threads , Or multiple processes to interact ? How to design and choose ? We generally call this problem I/O Model design . Different I/O The model has different effects on the performance and scalability of key value database .

for instance , If a thread has to handle both network connections 、 Resolve request , And complete data access , Once a certain operation is blocked , The whole thread will block , This reduces the system response speed . If we use different threads to handle different operations , that , When a thread is blocked , Other threads can run normally . however , If different threads need to access shared resources , Then there will be thread competition , It will also affect the efficiency of the system , What should we do ? therefore , It's really a “ be in a dilemma ” choice , We need careful design .

You may often hear Redis A single thread , that ,Redis And how to do it “ Single thread , High performance ” What about ? I'll talk to you later .

How to locate key value pairs ?

When SimpleKV Parsed the request from the client , Know the key value pair operation to be performed , here ,SimpleKV You need to find out whether the key value pair to be operated exists , This depends on the index module of the key value database . The function of the index is to make the key value database based on key Find corresponding value Storage location , And then perform the operation .

There are many types of indexes , The common ones are hash tables 、B+ Trees 、 Dictionary tree, etc . Different index structures in performance 、 Space consumption 、 Concurrency control has different characteristics . If you've seen other key value databases , You will find , Different key value databases use different indexes , for example ,Memcached and Redis Use hash table as key-value Indexes , and RocksDB The skip table is used as the memory key-value The index of .

generally speaking , Memory key value database ( for example Redis) Use hash table as index , A large part of the reason is , Its key value data are basically stored in memory , The high-performance random access feature of memory can be well compared with hash table O(1) Match the operation complexity of .

SimpleKV The index of is based on key find value The storage location of . however , and SimpleKV Different , about Redis for , The interesting thing is , its value Support for multiple types , When we find one through the index key The corresponding value after , Still need from value The complex structure of ( For example, sets and lists ) Further find the data we actually need , The efficiency of this operation itself depends on their implementation structure .

Redis Some common efficient index structures are used as some value The underlying data structure of type , This technical route is Redis It provides good support for high-performance access .

What is the specific logic of different operations ?

SimpleKV The index module of is responsible for according to key Find the appropriate value Storage location . For different operations , After finding the storage location , The specific logic of operations that need to be performed further will be different .SimpleKV The operation module realizes the specific logic of different operations :

  • about GET/SCAN In terms of operation , At this point, according to value The storage location of the returned value value ;
  • about PUT A new key value for data ,SimpleKV Memory space needs to be allocated for this key value pair ;
  • about DELETE operation ,SimpleKV Key value pairs need to be deleted , And release the corresponding memory space , This process is done by the distributor .

I don't know if you notice , about PUT and DELETE For both operations , In addition to new write and delete key value pairs , You also need to allocate and free memory . I have to mention SimpleKV The storage module of .

How to provide services quickly after restart ?

SimpleKV The common memory allocator is adopted glibc Of malloc and free, therefore ,SimpleKV There is no need to pay special attention to the management of memory space . however , Key value pairs in key value databases are usually of different sizes ,glibc When dealing with random size memory block allocation , Not so good . Once the saved key value is too large for the data , It may cause more serious memory fragmentation problems .

therefore , Allocators are a key factor in key value databases . For memory based Redis for , This is especially important .Redis The memory allocator provides a variety of options , Distribution efficiency is also different , Later, I will talk about this problem in detail .

SimpleKV Although it depends on memory to save data , Provide quick access to , however , I hope too SimpleKV It can quickly restart the service after restart , therefore , I am here SimpleKV The persistence function is added to the storage module of .

however , Since disk management is more complex than memory management ,SimpleKV Directly in the form of documents , Save the key value data on the disk by calling the operation interface of the local file system . here ,SimpleKV Just consider when to save the key value data in memory to a file , That's all right. .

One way is , For each key value pair ,SimpleKV Drop and save them , Although this makes SimpleKV More reliable data , however , Because I have to write a disk every time ,SimpleKV The performance of will be greatly affected .

Another way is ,SimpleKV Just periodically save the key value data in memory to a file , This can avoid the performance impact of frequent disk write operations . however , One potential cost is SimpleKV Your data is still at risk of loss .

and SimpleKV equally ,Redis It also provides persistence . however , In order to adapt to different business scenarios ,Redis It provides many implementation mechanisms and optimization improvements for persistence , I'll introduce you one by one later Redis Key design considerations in persistence mechanisms .

Summary

thus , We constructed a simple key value database SimpleKV. You can see , The first two steps are designed from the perspective of application , That is, the application perspective ; The next four steps are actually SimpleKV Complete internal structure , It can be said that a sparrow is small , Five zang organs .

SimpleKV Contains a basic component of the key value database , With an understanding of these components , I'm learning later Redis This rich version of SimpleKV when , It's a lot easier .

To support richer business scenarios ,Redis These components or functions are extended , Or fine optimization , So as to meet the requirements of function and performance .

  From this comparison , We can see , from SimpleKV Evolve to Redis, There are several important changes :

  • Redis It is mainly accessed through the network framework , Instead of a dynamic library , It also makes Redis It can be accessed as a basic network service , Expanded Redis Application scope of .
  • Redis In the data model value There are many types , Therefore, it also brings more operation interfaces , For example, list oriented LPUSH/LPOP, Set oriented SADD/SREM etc. . In the next class , I'll talk to you about these value The data structure and operation efficiency behind the model , And they're about Redis The impact of performance .
  • Redis The persistence module of can support two ways : journal (AOF) And snapshot (RDB), These two persistence methods have different advantages and disadvantages , Affect the Redis Access performance and reliability .
  • SimpleKV Is a simple stand-alone key value database , however ,Redis Support highly reliable clusters and highly scalable clusters , therefore ,Redis It contains the corresponding cluster function support module .

Through this class SimpleKV The construction of , I believe you have an overall understanding and deep understanding of the basic structure and important modules of the key value database , This is also true Redis The core version of stand-alone . For the points just mentioned Redis The great evolution of , In the next lesson , I'll focus on it in turn . meanwhile , I will also combine the actual combat scenes , So that you can not only understand the principle , Can really apply what you have learned , Improve the actual combat ability .

原网站

版权声明
本文为[Miapenso]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060609047406.html