当前位置:网站首页>[database learning] redis parser & single thread & Model
[database learning] redis parser & single thread & Model
2022-07-25 22:22:00 【Hua Weiyun】
@[toc]
Redis High performance parser of the Protocol

although redis The protocol is easy to read and implement , But it can be implemented with performance similar to binary Protocol .
Resp Use prefix length to transmit large amount of data , So it doesn't need to scan the load to find JSON Special characters like , There is no need to reference the load that needs to be sent to the server .
Batch and multi batch lengths can be processed using code , These codes perform a single operation on each character , And scan at the same time CR character , For example, the following C Code :
Resp Use prefix length to transmit multiple lines of data , So it doesn't need to scan the load to find JSON Special characters like , There is no need to reference the load that needs to be sent to the server .
You can handle multiple lines and multiple line lengths with code . This code performs a single operation on each character , And scan at the same time CR character .
#include <stdio.h>int main(void) { unsigned char *p = "$123\r\n"; int len = 0; p++; while(*p != '\r') { len = (len*10)+(*p - '0'); p++; } /* Now p points at '\r', and the len is in bulk_len. */ printf("%d\n", len); return 0;} Identify the first CR after , You can skip it and the following LF, Without any treatment . then , Large volumes of data can be read using a single read operation that does not check the payload in any way . Last , remainder Cr and LF Characters will be discarded without any processing .
Redis The performance of the protocol is equivalent to that of the binary Protocol . what's more , It is easy to implement in most high-level languages , Thus reducing the number of errors in the client software .
C Language implementation , although C be conducive to redis Performance of , But language is not the core factor .
Pure memory input / Output . Compared with other disk based databases ,redis The pure memory operation of has natural performance advantages .
Input / Output multiplexing , be based on epoll/select/kqueue Equal input / Output multiplexing technology , Achieve high throughput network input / Output .
In the single threaded model , A thread cannot use more than one kernel , But on the other hand , It avoids the overhead of synchronization mechanisms such as frequent context switching and locking of multiple threads .
Single thread
about DB,CPU Usually not a bottleneck , Because most requests are not CPU intensive , It is i/o intensive . about redis, If you don't think about rdb/aof And so on ,redis Is a complete pure memory operation , Very fast execution . therefore , This part of the operation is usually not a performance bottleneck .redis The real performance bottleneck is the network i/o, That is, the network transmission delay between the client and the server . therefore ,redis Select single thread i/o Multiplexing to realize its core network model .
Avoid too much context switching overhead
In multithreading scheduling , Need to be in CPU Switch thread context between , Context switching involves a series of register replacements , Such as program counter 、 Stack pointer and program status word 、 Program stack reset , even to the extent that CPU Cache and TLB Quick table replacement . If you switch between multiple threads in a process , The effect is better , Because multiple threads in a single process share the process address space , therefore , The thread context is much smaller than the process context . If it's cross process scheduling , You need to switch the entire process address space .
If it's a single thread , You can avoid the overhead of frequent thread switching in the process , Because the program always runs in a single thread in the process , And there is no scenario of multi-threaded switching .
The expected multithreaded programming is compared with the actual multithreaded programming :
A network model
Redis Agreement passed TCP, Client and Redis Instance maintains duplex connection , As shown in the figure below :
Serialization implementation
The implementation of serialization is relatively simple . After executing the previous command , The same connection sends a second request . As shown in the figure below :
Single connection throughput = 1 / (2* Network delay + Server processing time + Client processing time )
redis Processing time for a single request (10 Microsecond ) Often than LAN Small delay 1 An order of magnitude . therefore , In serial mode , A single connection waits the most time on the network , The processing power of the server is not fully utilized .
边栏推荐
- Synchronized and volatile
- How to implement an app application to limit users' time use?
- After 2 years of functional testing, I feel like I can't do anything. Where should I go in 2022?
- 【数据库学习】Redis 解析器&&单线程&&模型
- The testing work is not valued. Have you changed your position?
- Recursive case -c
- D3.js learning
- H5幸运刮刮乐抽奖 免公众号+直运营
- internship:普通常用的工具类编写
- IPv4地址已经完全耗尽,互联网还能正常运转,NAT是最大功臣!
猜你喜欢

Use of hyperlinks

Visitor mode

成为比开发硬气的测试人,我都经历了什么?

对需求的内容进行jieba分词并按词频排序输出excel文档

Advanced database · how to add random data for data that are not in all user data - Dragonfly Q system users without avatars how to add avatar data - elegant grass technology KIR

聚名十年,说出你的故事,百万豪礼等你拿

6-18 vulnerability exploitation - backdoor connection

According to the use and configuration of data permissions in the open source framework

SMART S7-200 PLC通道自由映射功能块(DO_Map)

Compile and decompile
随机推荐
Minor GC 和 Full GC 有什么不同呢?
【C语法】void*浅说
Some summary about function
Gan, why '𠮷 𠮷'.Length== 3 ??
Xiaobai programmer's first day
还不懂mock测试?一篇文章带你熟悉mock
Wechat applet application development competition works comprehensive development record - Jinlu cultural tourism (cloud development - Overview)
数学规划分类 Math Programming Classfication
PySpark数据分析基础:pyspark.sql.SparkSession类方法详解及操作+代码展示
Victoriametrics single node of kubernetes
Playwright tutorial (II) suitable for Xiaobai
Usage of in in SQL DQL query
C language: random generated number + bubble sort
Whether the five distribution methods will produce internal fragments and external fragments
H5幸运刮刮乐抽奖 免公众号+直运营
『SignalR』. Net using signalr for real-time communication
Based on if nesting and function call
Arcgis10.2 configuring postgresql9.2 standard tutorial
kubernetes之VictoriaMetrics单节点
Jenkins+svn configuration