当前位置:网站首页>Why is the redis single-threaded also so fast?
Why is the redis single-threaded also so fast?
2022-07-31 05:47:00 【ham programming】
Background:
A very big feature of Redis is that it is fast. The official description is given:
1. Single thread, reducing thread switching time.
2. Pure memory operation
3. I/O multiplexing mechanism
Maybe the first two are easier to understand, but what is I/O multiplexing? Next, I will introduce the knowledge about it, I hope it will help youp>
Content
1. What is the I/O multiplexing mechanism?
Multi-channel I/O multiplexing model is to use select, poll, epoll to monitor the I/O events of multiple streams at the same time. When idle, the current thread will be blockedWhen one or more streams have I/O events, they will wake up from the blocking state, so the program will poll all the streams (epoll is only polling those streams that actually emit events), and onlyBy sequentially processing the ready streams, this approach avoids a lot of useless operations.Here "multiplexing" refers to multiple network connections, and "multiplexing" refers to multiplexing the same thread.Using multiple I/O multiplexing technology allows a single thread to efficiently process multiple connection requests (minimize network IO time consumption), and Redis operates data in memory very fast
2. Where is the bottleneck of redis operation?
The bottleneck of the operation lies in the network I/O, the steps of the I/O operation are divided into:
- The data reaches the kernel through the gateway, and the kernel prepares the data
- Data is written from kernel cache to user program data
3. What regions do kernel and user data represent?

4. The difference between blocking I/O, non-blocking I/O, and I/O multiplexing

In analogy to the NIO operation in jdk, NIO provides a selector, which is a multi-channel server for selectableChannel, which is used to monitor the io status of SelectableChannel.The channel is registered on the selector, and you can choose which event type to register, and the selector polls the registered events.
Take nio's SocketChannel as an example, write sample code;
public class TestNonBlockNIO {@Testpublic void testClient() throws IOException {//1. Get the channelSocketChannel sChannel=SocketChannel.open(new InetSocketAddress("127.0.0.1",9898));//2. Switch to non-blocking statesChannel.configureBlocking(false);//3. Allocate a buffer of the specified sizeByteBuffer buffer=ByteBuffer.allocate(1024);//4. Send data to the serverScanner scan=new Scanner(System.in);while(scan.hasNext()){String str=scan.next();buffer.put((new Date().toString()+"\n"+str).getBytes());buffer.flip();sChannel.write(buffer);buffer.clear();}// buffer.put(LocalDate.now().toString().getBytes());//5. Close the channelsChannel.close();}@Testpublic void testServer() throws IOException {//1. Get the server channelServerSocketChannel serverSocketChannel=ServerSocketChannel.open();//2. Switch to non-blocking modeserverSocketChannel.configureBlocking(false);//3. Bind the connectionserverSocketChannel.bind(new InetSocketAddress(9898));//4. Get the selectorSelector selector = Selector.open();//5. The channel is registered on the selector, and the event to listen to is selectedserverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);//6. Polling to get the "ready" event on the selectorwhile (selector.select()>0){//7. Get all registered "ready listener events" in the current selectorIterator iterable= selector.selectedKeys().iterator();while (iterable.hasNext()){//8. Get ready eventSelectionKey sk=iterable.next();//9. Determine what event is readyif(sk.isAcceptable()){ //Ready to receive//10. If the reception is ready, get the connection of the clientSocketChannel socketChannel=serverSocketChannel.accept();//11. Switch to non-blocking modesocketChannel.configureBlocking(false);//12. Register the channel to the selectorsocketChannel.register(selector,SelectionKey.OP_READ);}if(sk.isReadable()){//13. Get the channel whose read status is readySocketChannel socketChannel= (SocketChannel) sk.channel();ByteBuffer buffer=ByteBuffer.allocate(1024);int len=0;while((len=socketChannel.read(buffer))>0){buffer.flip();System.out.println(new String(buffer.array(),0,len));buffer.clear();}}}//14. Cancel the selection keyiterable.remove();}}} Summary
In fact, the reason why reids is single-threaded so fast is because the I/O multiplexing mechanism model is used internally, but this mechanism is not used in all cases, should be used with a large number of links, the processing time is not very long, the number of connections is preferably greater than 1000, the degree of concurrency is not high, or NIO does not have significant performance advantages in the LAN environment
边栏推荐
猜你喜欢
随机推荐
DeFi 项目中的治理Token
利用phpstudy搭建DVWA
闭包(二)
可以“繁殖”的程序
Proteus 8 Professional安装教程
(Crypto必备干货)详细分析目前NFT的几大交易市场
win11中利用IIS10搭建asp网站
leetcode-每日一题剑指 Offer II 041. 滑动窗口的平均值(队列模拟)
If the account number or password is entered incorrectly for many times, the account will be banned.
Swordsman Offer Special Assault Edition --- Day 3
剑指offer专项突击版 --- 第 4 天
vulhub靶场学习日记xxe-lab
Data set partitioning and cross-validation
leetcode-每日一题735. 行星碰撞(栈模拟)
剑指offer专项突击版 ---- 第1天
11 【定位】
剑指offer基础版 --- 第22天
sqlmap注入教程 常用指令
剑指offer基础版 ----第31天
Oracle数据库中的“limit”查询








