Multithreading of redis 6

Dream house 2021-05-04 16:42:34
multithreading redis


I talked with my colleagues yesterday Redis, He asked me Redis How are you doing , I said learning is OK , Then he suddenly asked “ You know, Redis Is multithreading supported now ?”, I was stunned for a moment ,Redis It's always single threaded , How to support multithreading all of a sudden ? The moment is felt by the second , Come back and look up the relevant information , Otherwise, I dare not say that I will Redis 了

Redis Knowledge review

So let's go back Redis Single thread mode related knowledge , We all know Redis The main reason for being fast is because epool I/O Reuse model , Let's talk about it briefly :

  • First ,Redis It's running in a single thread , All operations are performed in a linear sequence , But because read and write operations wait for user input or output is blocked , therefore I/O In general, the operation cannot return directly , This will lead to the I/O Blocking prevents the entire process from serving other customers , and I/O Multiplexing is to solve this problem .
  • multiple I/O The reuse model uses select、poll、epoll Can monitor multiple streams at the same time I/O The power of events , In my spare time , Will block the current thread , When one or more streams have I/O When an event is , Just wake up from the blocking state , So the program will poll all the streams (epoll Is to poll only those streams that actually emit the event ), And only process the ready streams sequentially , This avoids a lot of useless operations .
  • here “ multiple ” Refers to multiple network connections ,“ Reuse ” This refers to reusing the same thread . Using multiple channels I/O Multiplexing allows a single thread to efficiently process multiple connection requests ( Minimize the network IO Time consumption of ), And Redis It's very fast to manipulate data in memory , That is, in-memory operations are not affected Redis Performance bottlenecks , Mainly by the above points created Redis With high throughput

The picture below is Redis The internal core flow chart of ( It's also a classic introduction I can find Redis The schematic diagram of ), In fact, the internal is mainly realized through two-way queue and red black tree ( The pictures are very detailed , What you don't know in it , Baidu can be directly , Easy to find ):

Redis Single thread

For a request operation ,Redis Mainly do 3 thing : Read data from client 、 perform Redis command 、 Write back the data to the client ( If it's more accurate , In fact, it also includes the analysis of the protocol ). So the main thread is actually to put all the operations of this 3 thing , Serial execution together , Because it's memory based , So the execution speed is very fast :

advantage VS shortcoming :

advantage :

  • There is no lock problem
  • Avoid inter thread CPU Switch shortcoming :
  • A single thread cannot take advantage of multiple CPU
  • Serial operation , Some operation “ have a problem ” Meeting “ Blocking ” Follow up operation

Redis Multithreading

Redis Multithreading optimization ideas : Because of the Internet I/O stay Redis It takes up most of the time CPU Time , So the Internet I/O Part of it comes out alone , Make it multithreaded .

Multithreading here , It's really just the Redis These two things are done in a single thread “ Read data from client 、 Write back the data to the client ”( It can also be called the Internet I/O), It's multithreaded , however “ perform Redis command ” Or serial execution in the main thread , The logic remains the same .

Maybe some students will ask , Main thread and multiple I/O Threads , All of them deal with the data in the graph at the same time “ queue ”, Will there be lock competition ? Here's a clever design , Is that when epoll obtain socket When linking , The event will be thrown into the queue first , Like throwing it away N Events , At this time, the main thread will be busy and so on (spinlock Spin lock effect ) state . And then many I/O Threads start to network in parallel I/O, And analyze the data protocol , When all the queues have been processed , The main thread serializes the requests in the queue ““ perform Redis command ”, Then clear the queue . So the whole execution process is summed up : The main thread queues the request -> I/O Thread parallel network read -> The main thread executes serially Redis command -> I/O Thread parallel network write -> The main thread clears the queue , And receive the next batch of requests .

advantage VS shortcoming

advantage :

  • Improve response time , Make full use of CPU shortcoming :
  • Increased code complexity

summary

  • Redis Multiplexing technology , Support epoll、kqueue、selector;
  • 5.0 Version and before , There is only one thread to handle client requests , Serial processing ;
  • 6.0 Version introduced worker Thread, Only the network IO Read and write , The core IO Responsible for serial processing of client instructions .

Welcome to like it more , More articles , Please follow the WeChat public account “ Louzi's way to advancement ”, Focus , Neverlost ~~

版权声明
本文为[Dream house]所创,转载请带上原文链接,感谢
https://javamana.com/2021/05/20210504164134485b.html

  1. Realization of reactor Kafka through spring boot Webflux
  2. RPC框架设计----Socket与I/0模型
  3. Problems in upgrading from Java 8 to Java 11
  4. RPC framework design -- socket and I / 0 model
  5. RPC框架设计----I/0模型
  6. RPC framework design: I / 0 model
  7. RPC框架设计----NIO编程缓冲区Buffer
  8. RPC框架设计----NIO编程缓冲区Buffer
  9. RPC framework design -- NiO programming buffer
  10. RPC framework design -- NiO programming buffer
  11. Java多线程基础
  12. Java multithreading Foundation
  13. 码农飞升记-00-Java发展历程
  14. Development history of coder-00-java
  15. 码农飞升记-00-Java发展历程
  16. Development history of coder-00-java
  17. Spring and Autumn Moon
  18. Node.js与Spring Boot比较? - Ryan Gleason
  19. Spring WebFlux的明显陷阱 - ŁukaszKyć
  20. Spring创始人Rod大叔对YAML的真实想法
  21. Compare node.js with spring boot- Ryan Gleason
  22. Obvious pitfalls of spring Webflux- Ł ukaszKy ć
  23. Spring founder uncle rod's real thoughts on yaml
  24. 码农飞升记-02-OracleJDK是什么?OracleJDK的版本怎么选择?
  25. What is manong feisheng-02-oracle JDK? How to choose the version of Oracle JDK?
  26. Spring tide surging Xinanjiang
  27. Linux内核软中断
  28. Linux kernel soft interrupt
  29. Linux内核软中断
  30. Linux kernel soft interrupt
  31. Java multithreading Foundation
  32. The construction of Maven private library nexus
  33. I / O stream in Java
  34. JDK 16:Java 16的新功能 - InfoWorld
  35. 在Java中本地进行线程间数据传输的三种方式和源码展示
  36. jdon导致cpu 99%最后tomcat死掉---banq给予回复
  37. 用领域事件模拟AOP注入
  38. JDK 16: new function of Java 16 - InfoWorld
  39. Cartoon: from JVM lock to redis distributed lock
  40. Spring 3.1 终于加入了Cache支持
  41. Prototype与JQuery对比
  42. Three ways of data transmission between threads in Java and source code display
  43. Jdon causes 99% of CPU and Tomcat dies -- banq replies
  44. docker 原理之 user namespace(下)
  45. Simulating AOP injection with domain events
  46. Spring 3.1 finally adds cache support
  47. Comparison between prototype and jquery
  48. User namespace of docker principle (2)
  49. The way to learn java IO stream and XML
  50. Why does a seemingly correct code cause the Dubbo thread pool to be full
  51. 0 基础 Java 自学之路(2021年最新版)
  52. 0 basic Java self study road (latest version in 2021)
  53. c#—基础拾遗(1) 面向对象
  54. C - basic information (1) object oriented
  55. 技术分享|SQL和 NoSQL数据库之间的差异:MySQL(VS)MongoDB
  56. Technology sharing differences between SQL and NoSQL databases: MySQL (VS) mongodb
  57. PHP教程/面向对象-3~构造函数和析构函数
  58. Spring Cloud的Feign客户端入门
  59. 优化Spring Boot应用的Docker打包速度
  60. PHP tutorial / object oriented - 3 ~ constructor and destructor