当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
11 【定位】
Quickly master concurrent programming --- the basics
C语言实验五 循环结构程序设计(二)
C语言实验三 选择结构程序设计
闭包(五)----一个常见的循环
数字取证autopsy工具用法
uni-app进阶之生命周期【day8】
【Elastic-Job】分布式调度任务概览篇
为什么redis是单线程还那么快?
Object,多态 1(第八天)
【C语言3个基本结构详解——顺序、选择、循环】
GUCCI、LV等奢侈品巨头如何布局元宇宙的,其他品牌应该跟上吗?
Object Detection Study Notes
leetcode-每日一题剑指 Offer II 041. 滑动窗口的平均值(队列模拟)
The interviewer asked me how to divide the database and the table?Fortunately, I summed up a set of eight-part essays
If the account number or password is entered incorrectly for many times, the account will be banned.
The TOKEN value of Kubernetes joining the cluster expires
torch.normal function usage
剑指offer专项突击版 ---第 5 天
1D, 2D, 3D convolution operations in pytorch