当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
剑指offer基础版 --- 第21天
什么是 GameFi?
对递归的一些感悟
C语言实验一 熟悉C程序的环境
变量的解构赋值
Access数据库的查询
leetcode-每日一题1252. 奇数值单元格的数目(模拟优化)
剑指offer基础版 --- 第22天
三子棋讲解(C语言)
对于输出点是时间戳的渗透测试方法(以Oracle数据库为例)
leetcode-每日一题565. 数组嵌套(标记图和并查集)
【Elastic-Job】分布式调度任务概览篇
The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays
leetcode-1833. 雪糕的最大数量(排序+贪心)
First acquaintance with Flask
uni-app进阶之创建组件/原生渲染【day9】
Anaconda configure environment directives
leetcode-829. 连续整数求和(数论)
剑指offer专项突击版 ---第 5 天
tf.keras.utils.get_file()









