当前位置:网站首页>Redis core technology and practice 1 - start with building a simple key value database simplekv
Redis core technology and practice 1 - start with building a simple key value database simplekv
2022-04-23 19:16:00 【Li Siwei】
Data model and operation interface
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 .
The data model and the operation interface can decide under what circumstances it is suitable to use the key value database constructed by us , Under what circumstances is not suitable for , Instead, you need to use other types of databases .
Data model
The basic data model of key value database is : key-value Model .
Usually key yes String type .
Some key value databases only support vlaue by String type , for example memcached database .
It can also be designed as value All basic data types are supported +String type .
and redis Multiple complex data types are supported : Such as String、 Hashtable 、 list 、 Collection etc. .
Redis It can be widely used in actual business scenarios , It benefits from supporting diversified types of value.
Operation interface
newly added \ to update :PUT( perhaps SET)
obtain :GET
Delete :DELETE
Range :SCAN
Does it already exist :EXISTS
Whether key value pairs are stored in memory or external memory ?
Save in memory , Fast access (ns Level ), There is a risk of power failure and loss
Save in external storage , Power off will not be lost , But the access speed is too slow (ms Level ).
Select according to the application scenario
for example , The data in the cache scenario is required to be accessed quickly, but it is allowed to be lost , At this time, the key value data is saved in memory .
Memcached and Redis Are all memory key value databases .
What access mode is used ?- Access module
One way is to use it for external applications through function library calls , such as , Link to our own program in the form of dynamic link library .
You can also use the network framework to Socket Provide key value pair operation in the form of communication .
Network framework access mode
The mode of providing access services through the network framework , The application scenario of key value database is expanded , But it also gives the performance of key value database 、 The design of the operation model brings some potential problems : How to design a suitable network I\O Model ?
The so-called network I\O, In the network framework access mode , How to handle user access : How to handle network connection 、 What protocol is used to resolve network requests 、 How to handle data access ? Whether to use single thread or multi thread or multi process ? Different networks I\O The model has different effects on the performance and scalability of key value database .
If you use a thread, you have to deal with both network connections 、 Request parsing , And complete data access , So once a certain operation is blocked 、 The whole user access will be blocked , This reduces the system response speed .
If we use different threads to handle different operations , So when a thread is blocked , Other threads can still run normally . But when multiple threads access the same resource , There will be competition again , It also affects efficiency .
Redis Using a single threaded network I\O Model , And did it “ Single thread 、 High performance ”.
How to locate key value pairs - Index module
When the user request is parsed , The key value database needs to find out whether the key value pair requested by the user exists , This depends on the index module of the database .
The function of the index is , Let the key value database according to key find 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 types consume space 、 performance 、 Concurrency control has different characteristics .
Some key value databases use jump tables to store key-value Yes , and redis and memcached Are all hash tables .
Send the user requested key-value Write or delete - Storage module
When the user calls PUT When the command operates on data , Need to be for key-value Allocate memory space for data
When the user calls DELETE When the command operates on data , Need to put key-value Free up the occupied memory space
Memory allocator
SimpleKV Simple glibc As a memory allocator .
But for Redis Come on , Because of its value Support various complex data types , Therefore, the size of key value pairs varies ,glibc When dealing with random size memory block allocation , Not so good . Once the saved key value is too large for the data , There will be a serious memory fragmentation problem .
Redis A variety of optional memory allocators are provided , Users can choose according to the application scenario .
How to provide services quickly after restart - Persistence
There are two persistence schemes :
Every time PUT perhaps DELETE when , Drop disk while writing memory ( Call the file interface to write the file ). Will result in very low efficiency
Periodically drop the data to the disk according to a certain frequency , Every once in a while, all the PUT perhaps DELETE Write the data to the file , In this case, it is possible to lose
版权声明
本文为[Li Siwei]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210600380964.html
边栏推荐
- Codeforces Round #784 (Div. 4)
- redis优化系列(三)解决主从配置后的常见问题
- UML类图几种关系的总结
- js 计算时间差
- Quick start to static class variables
- OpenHarmony开源开发者成长计划,寻找改变世界的开源新生力!
- SQL server requires to query the information of all employees with surname 'Wang'
- Accessing private members using templates
- JS controls the file type and size when uploading files
- Reflection on the performance of some OpenGL operations in the past
猜你喜欢
開關電源設計分享及電源設計技巧圖解
Using Visual Studio code to develop Arduino
Reflection on the performance of some OpenGL operations in the past
Openharmony open source developer growth plan, looking for new open source forces that change the world!
该买什么设备,Keysight 给你挑好了
White screen processing method of fulter startup page
arcMap 发布切片服务
2022.04.23 (the best time for lc_714_to buy and sell stocks, including handling charges)
为何PostgreSQL即将超越SQL Server?
On the forced conversion of C language pointer
随机推荐
Is it safe to open an account in Bohai futures.
Translation of audio signal processing and coding: Preface
Esp01s with Arduino development environment
C1000k TCP connection upper limit test 1
Raspberry pie 18b20 temperature
SQL server requires to query the information of all employees with surname 'Wang'
An 8266 crash
[today in history] April 23: the first video uploaded on YouTube; Netease cloud music officially launched; The inventor of digital audio player was born
[报告] Microsoft :Application of deep learning methods in speech enhancement
Partage de la conception de l'alimentation électrique de commutation et illustration des compétences en conception de l'alimentation électrique
SQL常用的命令
Class loading process of JVM
8266 obtain 18b20 temperature
Disable Ctrl + Alt + Del
FTP, SSH Remote Access and control
Simplified path (force buckle 71)
@Analysis of conditional on Web Application
Switching power supply design sharing and power supply design skills diagram
The difference between ordinary inner class and static inner class
An algorithm problem was encountered during the interview_ Find the mirrored word pairs in the dictionary