"Four o'clock, get to know Redis fast in the end where? "

A. Development language

II. Pure memory access

III. Single-threaded

IV. Non-blocking multi-channel I / O multiplexing mechanism

Foreword

NoSQL Redis is a database based on a key (Key-Value) of, Redis may be made of Value String, hash, list, set, zset, Bitmaps, HyperLogLog data structures and algorithms, and other components.

Redis also provides key expired, publish-subscribe, transaction, Lua script, Sentinel, Cluster and other functions. Redis command execution speed is very fast, according to the official performance can be achieved 10w + qps.

In this paper, in the end so fast Redis where the following main points:

First, the development language

Now we have high-level programming language, such as Java, python and so on. You may find the C language is very old, but it is really useful, after all unix system is implemented in C, so C language is very close to the operating system language. Redis is using C language development, so the implementation will be faster.

Also say one more thing, college students a good learning C, will make you a better understanding of computer operating systems. Do not think learning a high-level language you can not focus on the bottom, always owe a debt to repay. Here recommend a book more difficult nut to "in-depth understanding of computing systems."

Second, pure memory access

Redis all data in memory, non-data synchronization to work in, is not required to read data from the disk, 0 times IO. Memory response time of about 100 nanoseconds, which is an important foundation of Redis fast speed. Take a look at CPU speed:

"Four o'clock, get to know Redis fast in the end where?  "

 

Take my computers, clocked at 3.1G, which means you can perform 3.1 * 10 ^ 9 instructions per second. So the CPU to see the world is very, very slow, a hundred times slower than its memory, disk million times slower than him, you say fast unhappy?

He borrowed a "depth understanding of computer system" FIG, shows a typical storage hierarchy, in the L0 layer, the CPU can access to a clock cycle, based on the SRAM cache line renewal, can be few CPU clock cycles access to, and are based on DRAM main memory, you can have access to them in tens to hundreds of clock cycles.

"Four o'clock, get to know Redis fast in the end where?  "

 

Third, the single-threaded

First, to achieve concurrent data structures threaded simplified algorithm to achieve not only difficult and troublesome tests also. Second, avoid the single-threaded locking thread switch and lock release brings consumption for server-side development, the locks and thread switching is usually the performance killer.

Of course, there will be single-threaded its shortcomings, but also Redis nightmare: obstruction. If you execute a command is too long, it will cause obstruction to other commands, for Redis is very deadly, so Redis is a database-oriented rapid execution scene.

In addition to the Redis, Node.js is single-threaded, Nginx is single-threaded, but they are all high-performance server model.

Fourth, non-blocking multi-channel I / O multiplexing mechanism

Prior to first say about the old blocking I / O is how it works: When using read or write to a file descriptor (File Descriptor FD) to read and write, and if the data is not received, then the thread It is suspended until the data is received. While blocking model is easy to understand, but when you need to handle multiple client task will not use blocking model.

"Four o'clock, get to know Redis fast in the end where?  "

 

I / O multiplexing is actually referring to the same process can be managed in a more connected. Multi-channel refers to the network connection, just reuse the same thread. In the network service, the role of I / O multiplexing is one time from the plurality of event notification service code connection processing manner determined by the service code.

In the I / O multiplexing model, the function call is the most important I / O multiplexing function, which can simultaneously monitor a plurality of file descriptor (fd) is read, when some of which fd when the read / write method returns the number of fd read / write.

"Four o'clock, get to know Redis fast in the end where?  "

 

Redis as I achieved using epoll / O multiplexing technique, plus its own event model Redis to convert the epoll so read, write, close to the events, not the network I / waste too much time on O. FD implement monitoring of multiple read and write, to improve performance.

"Four o'clock, get to know Redis fast in the end where?  "

 

For example the image of it. For example, a tcp server handles 20 clients socket.

A Program: sequential processing, if the first read data because the card socket slow processing, the eggs are playing back to a blocking.

Plan B: Each socket requests to create a sub-body process to handle, do not say each process consumes a lot of system resources, just enough operating system, the process of switching the tired.

Program C (I / O multiplexing model, epoll): fd user registration into the corresponding socket epoll (actually transferred between servers and operating systems, but not the socket fd fd_set data structure), which then just tell epoll need to read / write the socket, only need to deal with those active, there is a change of socket fd just fine. In this way, the whole process will be blocked only when the epoll calls, send and receive customer messages are not blocked.

Guess you like

Origin www.cnblogs.com/CQqfjy/p/12330512.html