【redis其它面试问题】
redis其它面试问题
redis是单线程,为什么还那么快
1. 纯内存操作
所有数据存储在内存中,读写速度比磁盘快 100,000倍 以上(内存访问约 100ns,SSD 约 1ms)。
避免传统数据库的磁盘 I/O 瓶颈。
2. 单线程无锁竞争
单线程避免了多线程的上下文切换和锁竞争开销。
3. 使用 I/O 多路复用,非阻塞IO
解释一下I/O多路复用模型
Redis是纯内存操作,执行速度非常的快,它的性能瓶颈是网络延迟而不是执行速度,I/O多路复用模型主要就是实现了高效的网络请求
#mermaid-svg-6cpQnJIIBwK9GFJI {font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-6cpQnJIIBwK9GFJI .error-icon{fill:#552222;}#mermaid-svg-6cpQnJIIBwK9GFJI .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-6cpQnJIIBwK9GFJI .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-6cpQnJIIBwK9GFJI .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-6cpQnJIIBwK9GFJI .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-6cpQnJIIBwK9GFJI .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-6cpQnJIIBwK9GFJI .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-6cpQnJIIBwK9GFJI .marker{fill:#333333;stroke:#333333;}#mermaid-svg-6cpQnJIIBwK9GFJI .marker.cross{stroke:#333333;}#mermaid-svg-6cpQnJIIBwK9GFJI svg{font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-6cpQnJIIBwK9GFJI .label{font-family:\"trebuchet ms\",verdana,arial,sans-serif;color:#333;}#mermaid-svg-6cpQnJIIBwK9GFJI .cluster-label text{fill:#333;}#mermaid-svg-6cpQnJIIBwK9GFJI .cluster-label span{color:#333;}#mermaid-svg-6cpQnJIIBwK9GFJI .label text,#mermaid-svg-6cpQnJIIBwK9GFJI span{fill:#333;color:#333;}#mermaid-svg-6cpQnJIIBwK9GFJI .node rect,#mermaid-svg-6cpQnJIIBwK9GFJI .node circle,#mermaid-svg-6cpQnJIIBwK9GFJI .node ellipse,#mermaid-svg-6cpQnJIIBwK9GFJI .node polygon,#mermaid-svg-6cpQnJIIBwK9GFJI .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-6cpQnJIIBwK9GFJI .node .label{text-align:center;}#mermaid-svg-6cpQnJIIBwK9GFJI .node.clickable{cursor:pointer;}#mermaid-svg-6cpQnJIIBwK9GFJI .arrowheadPath{fill:#333333;}#mermaid-svg-6cpQnJIIBwK9GFJI .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-6cpQnJIIBwK9GFJI .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-6cpQnJIIBwK9GFJI .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-6cpQnJIIBwK9GFJI .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-6cpQnJIIBwK9GFJI .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-6cpQnJIIBwK9GFJI .cluster text{fill:#333;}#mermaid-svg-6cpQnJIIBwK9GFJI .cluster span{color:#333;}#mermaid-svg-6cpQnJIIBwK9GFJI div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\"trebuchet ms\",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-6cpQnJIIBwK9GFJI :root{--mermaid-font-family:\"trebuchet ms\",verdana,arial,sans-serif;} 多路复用器 是 否 监听 监听 监听 读/写 读/写 fd1 调用select/epoll阻塞等待 fd2 ... 启动 注册fd到多路复用器 有fd就绪? 处理就绪的fd
- 初始化:将需要监听的fd(如Socket)注册到多路复用器(如 epoll)。
- 阻塞监听:调用 epoll_wait 阻塞线程,直到至少一个fd就绪。
- 事件触发:当某个fd就绪(如客户端发送数据),多路复用器返回就绪的fd列表。
- 非阻塞处理:程序遍历就绪的fd,执行读/写操作(不会阻塞其他fd的处理)。