How can a single-process single-thread Redis be highly concurrent

Source: Internet
Author: User
Tags epoll

Reference Documentation:

(1) http://yaocoder.blog.51cto.com/2668309/888374
(2) http://www.cnblogs.com/syyong/p/6231326.html

1. Basic Principles
Multi-Channel I/O multiplexing technology allows a single thread to efficiently handle multiple connection requests (minimizing network IO time consumption)
(1) Why not use multi-process or multi-threaded processing?

Multithreading may involve locking
Multithreaded processing involves thread switching and consumes CPU

(2) The disadvantage of single thread processing?

Multi-core CPU performance is not possible, but can be improved by opening multiple Redis instances on a single machine

2. Is there a thread safety problem with Redis?
Redis uses a thread-blocking approach that closes a task to a thread, naturally avoiding thread-safety issues, but still requires a lock for a composite operation that relies on multiple redis operations, and is likely to be a distributed lock

3. What is multi-channel I/O multiplexing (epoll)
(1) The network IO is implemented through the socket, the server continues to listen on a port, the client through the socket (ip+ Port) to establish a connection to the server (serversocket.accept), after a successful connection, you can use the encapsulated InputStream and OutputStream in the socket for IO interaction. For each client, the server creates a new thread dedicated to handling
(2) By default, network IO is blocking mode, where the server thread is in a "blocked" state until data arrives, and the server thread is automatically woken up to proceed with processing. In blocking mode, a thread can handle only one stream of IO events
(3) In order to improve the server threading efficiency, there are the following three ways

(1) Non-blocking "busy polling": polling each stream in a dead loop, if there is an IO event to process, so that a thread can handle multiple streams, but not high efficiency, easy to cause the CPU idling

(2) Select agent (non-differential polling): You can observe the IO events of multiple streams, and if none of the streams have an IO event, the thread goes into a blocking state, and if one or more of the IO events occur, the thread is woken up for processing. But you still have to traverse all the streams to find out which flows need to be processed. If the number of flows is n, the time complexity is O (n)

(3) Epoll proxy: The select agent has a disadvantage that the thread polls all streams after being awakened, or there is an invalid operation. Epoll What stream happens to the I/O event notification processing thread, so the operation of these streams is meaningful and the complexity is reduced to O (1)

4. Models used by other open source software

Nginx: Multi-process single-threading model
Memcached: Single-process multithreaded model

How can a single-process single-thread Redis be highly concurrent

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.