In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article is to share with you about why Redis is fast. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Redis is a kind of NoSQL database based on key-value pair (Key-Value). The Value of Redis can be composed of String,hash,list,set,zset,Bitmaps,HyperLogLog and other data structures and algorithms. Redis also provides functions such as key expiration, publish subscription, transaction, Lua script, Sentinel, Cluster and so on.
Redis executes commands very fast and can reach 10w+qps according to official performance. So this article mainly introduces where the Redis is fast, there are the following points:
I. Development language
Now we all program in high-level languages, such as Java, python and so on. You may think the C language is very old, but it is really useful. After all, unix systems are implemented in C, so C is very close to the operating system. Redis is developed in the C language, so execution is faster.
In addition, college students to learn C, will give you a better understanding of the computer operating system. Don't think that if you learn a high-level language, you don't have to pay attention to the bottom, and the debts you owe have to be repaid. A more difficult book, in-depth understanding of Computing Systems, is recommended here.
Second, pure memory access
Redis put all the data in memory, non-data synchronization is working normally, there is no need to read data from disk, 0 times IO. The memory response time is about 100 nanoseconds, which is an important foundation for the high speed of Redis. Let's take a look at the speed of CPU:
In my computer, for example, the main frequency is 3.1g, which means it can execute 3.1 * 10 ^ 9 instructions per second. So CPU is very slow to see the world, memory is a hundred times slower than it, disk is a million times slower than it, do you think it is fast?
A diagram of "in-depth understanding of computer systems" shows a typical memory hierarchy. At layer L0, CPU can be accessed in one clock cycle, SRAM-based cache spring renewal, accessible in several CPU clock cycles, and then DRAM-based main memory, which can be accessed in tens to hundreds of clock cycles.
Third, single thread
First, the implementation of single-thread simplification algorithm, the implementation of concurrent data structure is not only difficult but also troublesome to test. Second, single threading avoids the consumption of thread switching and locking release locks, which are often performance killers for server-side development. Of course, single threading has its drawbacks, and it's Redis's nightmare: blocking. If you execute one command too long, it will block other commands, which can be fatal for Redis, so Redis is a database for fast execution scenarios.
In addition to Redis, Node.js is also single-threaded, and Nginx is also single-threaded, but they are all examples of server performance.
Fourth, the mechanism of non-blocking multi-channel Istroke O multiplexing.
Before that, let's talk about how the traditional blocking I File Descriptor FD O works: when reading and writing to a file descriptor (File Descriptor FD) using read or write, if the data is not received, the thread is suspended until the data is received.
Although the blocking model is easy to understand, it is not used when you need to handle multiple client tasks.
Ihop O multiplexing actually means that multiple connections can be managed in the same process. Multiplex refers to a network connection, and multiplexing is only the same thread. In network services, the role of IWeiO multiplexing is to notify the business code of multiple connected events at one time, and the way of handling is determined by the business code.
In the Icano multiplexing model, the most important function call is the Icano multiplexing function, which can monitor the reading and writing of multiple file descriptors (fd) at the same time. When some of the fd are readable / writable, the method returns the number of fd that can be read / written.
Redis uses epoll as the implementation of Iamp O multiplexing technology, coupled with Redis's own event handling model to convert epoll's read, write, close and so on into events, without wasting too much time on the network. To achieve the monitoring of multiple FD reads and writes to improve performance.
Let me give you a visual example. For example, a tcp server handles 20 client socket.
Plan A: sequential processing, if the first socket is slow to read data because of the network card, it will play with eggs after blocking.
Plan B: create a separate sub-process for each socket request, not to mention that each process consumes a lot of system resources, process switching alone is enough for the operating system to tire.
Scheme C (epoll): register the fd corresponding to the user socket into epoll (in fact, what is passed between the server and the operating system is not the fd of socket but the data structure of fd_set), and then epoll only tells which socket needs to be read / written, and only needs to deal with those active and changing socket fd.
In this way, the whole process will block only when epoll is called, and sending and receiving customer messages will not be blocked.
Thank you for reading! This is the end of the article on why Redis is fast. I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.