One ,Redis High concurrency and fast reasons for

1.redis It's memory based , The read and write speed of memory is very fast ( Pure memory ).

2.redis It's single threaded , It saves a lot of time for context switching threads ( Avoid thread switching and race consumption ).

3.redis Using multiplexing technology , Can handle concurrent connections ( Non blocking IO).

Non blocking IO
Internal implementation adopts epoll, Adopted epoll+ Simple event framework implemented by ourselves .epoll Reading in , write , close , Connections are converted into events , And then use it epoll Multiplexing characteristics of , Never io It's a waste of time .

 

The following focuses on single thread design and IO The reason why multiplexing core design is fast .

<> Two , Why? Redis It's single threaded

2.1. Official answer

because Redis It's a memory based operation ,CPU no Redis The bottleneck of ,Redis The most likely bottleneck is the size of machine memory or network bandwidth . Since single thread is easy to implement , and CPU It won't be a bottleneck , So it's logical to adopt a single thread scheme .

 

2.2. performance index

about redis Performance of , There are also official websites , Ordinary laptops easily handle hundreds of thousands of requests per second .

 

2.3. Detailed reasons

1) No performance consumption of various locks is required

Redis The data structure of is not all simple Key-Value, also list,hash And so on , It is possible for these structures to perform very fine-grained operations , For example, add an element after a long list , stay hash Add or delete an object . These operations may require a lot of locks , The result is a significant increase in synchronization overhead .

in short , In the case of single thread , You don't have to think about locks , There is no lock , Release lock operation , There is no performance penalty due to possible deadlock .

 

2) Single thread multi process cluster scheme

The power of single threads is actually very powerful , Single core cpu It's also very efficient , Multithreading naturally has a higher performance cap than a single thread , But in today's computing environment , Even the upper limit of single machine multithreading can not meet the needs , What needs to be further explored is the scheme of multi server clustering , The multithreading technology in these schemes is still useless .

therefore “ Single thread , Multi process cluster ” It's a fashionable solution .

 

3)CPU consume

Using single thread , Unnecessary context switching and competition conditions are avoided , There is no switch caused by multi process or multi thread CPU.

But if CPU become Redis bottleneck , Or don't want the server to be anything else CUP Nuclear idle , What can we do? ?

We can consider several more Redis process ,Redis yes key-value database , Not a relational database , There are no constraints between the data . As long as the client distinguishes which key Where to put it Redis Just in the process .

<> Three ,Redis Advantages and disadvantages of single thread

3.1. Advantages of single process and single thread

*   Clearer code , The processing logic is simpler .
*   You don't have to think about locks , There is no lock , Release lock operation , There is no performance penalty due to possible deadlock .
*   non-existent “ Switch caused by multi process or multi thread ” And consumption CPU.
3.2. Disadvantages of single process and single thread

*   Can't play multi-core CPU performance , However, you can open more than one on a single machine Redis Examples to improve .
<> Four ,IO Multiplexing technology

redis Adopt network IO Multiplexing technology , To ensure the high throughput of the system in the case of multiple connections .

multiple - It means more than one socket network connections , multiplexing - It refers to reusing a thread . There are three main multiplexing technologies :select,poll,epoll.epoll It's the latest , It is also the best multiplexing technology at present .

Multi channel is adopted I/O Reuse technology : firstly , It enables a single thread to process multiple connection requests efficiently ( Minimize network IO Time consumption of ). second ,Redis The speed of operating data in memory is very fast ( Operations in memory will not be a performance bottleneck here ). The above two main points have created Redis It has high throughput .

<> Five ,Redis High concurrency and quick summary

1. Redis It's a pure memory database , Generally, it is a simple access operation , Threads take up a lot of time , The time spent is mainly concentrated in IO upper , So the reading speed is fast .

2.
Say it again IO,Redis Non blocking is used IO,IO Multiplexing , Single thread is used to poll descriptors , Open the database , shut , read , All writes are converted into events , It reduces context switching and contention during thread switching .

3. Redis The single thread model is adopted , The atomicity of each operation is guaranteed , It also reduces context switching and contention for threads .

4.
in addition , Data structures also help a lot ,Redis Full use hash structure , Fast reading speed , There are also some special data structures , Data storage is optimized , Such as compression table , Compressed storage of short data , Again , Skip Watch , Use ordered data structures to speed up reading .

5. And a little bit more ,Redis The event separator is implemented by ourselves , The efficiency is relatively high , The internal execution mode is non blocking , The throughput is relatively large .

Technology
©2020 ioDraw All rights reserved
Java realization PDF Online preview ( Four methods ) Android Development — Display food information according to customer budget spark.sql.shuffle.partitions and spark.default.parallelism The difference between Big data environment --- data warehouse (hive+mysql+hadoop) The construction of Children programming children's learning route What do you do in Shanghai to make money fast ? this 10 You can have a try ! Advanced programmer - Deep understanding of data structure After the outbreak Which programming has a bright future 2020 Nobel Prize in physiology or medicine announced Trump's "VIP therapy ": Is receiving a drug treatment that has not yet been approved