当前位置:网站首页>Why does redis choose single thread?
Why does redis choose single thread?
2022-07-25 22:00:00 【Hua Weiyun】
Redis Why single thread ?
stay Redisv6.0 before ,Redis The core network model of is implemented by single thread . Let's look at the official answer first :
It’s not very frequent that CPU becomes your bottleneck with Redis, as usually Redisis either memory or network bound. For instance, using pipelining Redisrunning on an average Linux system can deliver even 1 million requests per second, so if your application mainly uses O(N) or O(log(N)) commands, it is hardly going to use too much CPU.
The core meaning is , For one DB Come on ,CPU It's not usually a bottleneck , Because most requests won't be CPU intensive , It is I/O intensive . Specific to the Redis Words , If you don't think about RDB/AOF And so on ,Redis It's a complete memory only operation , The execution speed is very fast , So 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 Single threaded I/O Multiplexing to implement its core network model .
In fact, the reasons for choosing single thread are as follows :
Avoid too much context switching overhead : If it is a single thread, it can avoid the frequent thread switching overhead in the process , Because the program always runs in a single thread in the process , There is no scenario of multithreading switching .
Avoid the overhead of synchronization mechanism : If Redis Choose the multithreading model , Again because Redis It's a database , Then it is bound to involve the problem of underlying data synchronization , Some synchronization mechanisms will be introduced , Like locks , And we know that Redis It's not just about simple key-value data structure , also list、set and hash And other rich data structures , Different data structures have different granularity of locking for synchronous access , It may lead to a lot of locking and unlocking overhead in the process of data operation , It increases the complexity of the program and reduces the performance at the same time .
Simple and maintainable : If Redis Use multithreading mode , Then all the underlying data structures must be thread safe , This undoubtedly makes Redis The implementation of becomes more complex .
To make a long story short ,Redis Choosing a single thread can be said to be a trade-off after a multi-party game : With enough performance , Use a single thread to keep the code simple and maintainable .
Redis It's really single threaded ?
Discuss Before this question , Let's take a look at Redis Two important nodes in the version of :
Redisv4.0( Introducing multithreading to handle asynchronous tasks )
Redisv6.0( Formally implemented in the network model I/O Multithreading )
therefore , The Internet says Redis A single thread , Usually means in Redisv6.0 Before , Its core network model uses a single thread . and Redis Asynchronous tasks of are still multithreaded .
Redis stay v4.0 Version has introduced multithreading to do some asynchronous operations , This is mainly aimed at those very time-consuming orders , By asynchronizing the execution of these commands , Avoid blocking single threaded event loops .
stay Redisv4.0 After that, some non blocking commands were added, such as UNLINK、FLUSHALL ASYNC、FLUSHDB ASYNC.
Redisv6.0 Why multithreading ?
It's simple , Namely Redis Network of I/O The bottleneck has become more and more obvious .
With the rapid development of Internet , The Internet business system has to deal with more and more online traffic ,Redis The single thread mode of will cause the system to consume a lot of CPU Time on the Internet I/O So as to reduce the throughput , To improve Redis There are two directions to the performance of :
Optimize the network I/O modular
Improve the reading and writing speed of machine memory
The latter depends on the development of hardware , There is no solution for the moment . So we have to start with the former , The Internet I/O The optimization can be divided into two directions :
Zero copy technology or DPDK technology
Take advantage of multi-core
Zero copy technology has its limitations , Can't fully fit Redis This kind of complex network I/O scene , More networks I/O Yes CPU The consumption of time and Linux Zero copy technology . and DPDK Technology through the bypass network card I/O The way to bypass the kernel protocol stack is too complicated and requires the support of the kernel and even hardware .
therefore , Taking advantage of multi-core has become an optimized network I/O The most cost-effective solution .
Redis Delete strategy of expired key ?
Redis The expired deletion policy is : Lazy deletion and periodic deletion are used together .
Lazy deletion :Redis The lazy deletion strategy of is created by db.c/expireIfNeeded Function implementation , All key read / write commands are called before execution expireIfNeeded Function to check it , If expired , Delete the key , Then perform the operation where the key does not exist ; Do not operate if it has not expired , Continue with the original order .
Delete periodically : from Redis.c/activeExpireCycle Function implementation , Functions run at a certain frequency , Every time it runs , Take a certain number of random keys from a certain number of databases for inspection , And delete the expiration key .
边栏推荐
- Bitcoin.com:usdd represents a truly decentralized stable currency
- JSP初识
- [51Nod1676 无向图同构]无向图哈希[通俗易懂]
- Guiding principles of information security construction
- 少儿编程 电子学会图形化编程等级考试Scratch一级真题解析(判断题)2022年6月
- 面了个腾讯三年经验的测试员,让我见识到了真正的测试天花板
- Origen foundation officially launched $ogy stacking, leading a new round of ecological benefits
- [test development methodology] experience of test development platform PK - choice
- 突破性思维在测试工作中的应用
- 分享|智慧消防应急管理平台解决方案(附PDF)
猜你喜欢

The technical aspects of ByteDance are all over, but the result is still brushed. Ask HR why...

动画曲线天天用,你能自己整一个吗?看完这篇你就会了!

新版Maixhub部署(V831与K210)

【Redis底层解析】链表类型

Having met a tester with three years' experience in Tencent, I saw the real test ceiling

2年功能测试,却感觉自己什么都不会,2022我该何去何从?

Share | intelligent fire emergency management platform solution (PDF attached)

磁盘空间的三种分配方式

All you want to know about interface testing is here

信息安全建设原则指导
随机推荐
【测开方法论】测开平台pk心得-抉择
Redis 使用详解
在进行自动化测试,遇到验证码的问题,怎么办?
[hand torn STL] unordered_ set、unordered_ Map (encapsulated with hash table)
I/o case practice
Wet- a good choice for people with English difficulties - console translation
How to implement distributed locks with redis?
Redis是什么?简述它的优缺点
关于接口测试你想知道的都在这儿了
文件无法保存(文件夹已损坏无法读取怎么办)
EL表达式改进JSP
New maixhub deployment (v831 and k210)
jenkins+SVN配置
Excuse me, how to deal with repeated consumption of MySQL data
Tesseract OCR初探
别投了,软件测试岗位饱和了...
How to solve the problem of using the download Plug-in for export?
在腾讯干软件测试3年,7月无情被辞,想给划水的兄弟提个醒...
选择的能力
ZigBee development board (nxpzigbee Development)