Why Redis uses single-process single-threaded mode is also so fast

Source: Internet
Author: User
Tags epoll memcached

Redis uses a memory-based KV database with single-process single-threaded models, written in C. The official data provided is a QPS that can reach 100000 +. This data is no more Memcached than the same memory-based KV database with single-process multithreading.

The main reasons for Redis's rapid speed are:

    1. Completely based on memory;
    2. The data structure is simple, and the data operation is simple;
    3. Using a multiplexed I/O multiplexing model;

The first to 2nd is not detailed, mainly around the 3rd using multi-channel I/O multiplexing technology to expand.

The multi-channel I/O multiplexing model is the ability to use Select, poll, Epoll to monitor I/O events for multiple streams at the same time, blocking the current thread when idle, and waking up from the blocking state when one or more flows have I/O events, so the program polls through all the streams (epoll is to poll only those streams that actually emit the event, and the process-ready stream is only sequential, which avoids a lot of useless operations. Here "multi-path" refers to multiple network connections, "multiplexing" refers to the reuse of the same thread. Multiple I/O multiplexing techniques allow a single thread to efficiently handle multiple connection requests (minimizing network IO time consumption), and Redis operates data in memory very quickly (in-memory operations do not become a performance bottleneck here), with more than two points leading to a high throughput of Redis.

Unlike Memcached, Redis does not use Libevent directly, but accomplishes a very lightweight implementation of the common interfaces of Select, Epoll, Evport, and Kqueue. In different system calls to choose the appropriate interface, Linux under the default is Epoll. Because Libevent is heavier, more general, the code is very large, with a lot of Redis does not function, Redis in pursuit of "light" and remove the dependency, they choose to encapsulate a set.

Single-process single-thread benefits
    1. Clearer code, simpler processing logic
    2. No need to consider the various locks, there is no lock-release lock operation, there is no potential for deadlock caused by the performance of the consumption
    3. No multi-process or multithreading-induced switchover consumes CPU
Single-process one-thread disadvantage
    1. Multi-core CPU performance is not possible, but can be perfected by opening multiple Redis instances on a single machine;
Some of the other best open source software models used
    • Multi-process single-threaded model: Nginx
    • Single-Process multithreaded model: Memcached
Reference links
    • Https://en.wikipedia.org/wiki/Epoll
    • 1190000003063859#articleheader6
    • [http://www.ibm.com/developerworks/cn/linux/l-async/]http://www.ibm.com/developerworks/cn/linux/l-async/

Source: Syyong

Why Redis uses single-process single-threaded mode is also so fast

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.