Memcached multi-threaded Model & nginx multi-process model

Source: Internet
Author: User


Memcached and nginx concurrency models, one is a model of single-process and multi-thread, and the other is a classic multi-process and single-thread model. By comparison, we will deepen our understanding of the two concurrency models.

First, analyze the application scenarios of the two.

Commonalities:

Two applications, one is the cache service and the other is the Web service. They are applications that have frequent connection requests and are sensitive to response time. At this point, the two have the same requirements: a large number of requests, quick response. This is basically the requirement of the server programs and cannot be escaped.

Differences:

In fact, it is nonsense. The difference is cache and web. Think about the difference between the two, that is, the difference between different request response times is relatively large. What does memcached mean? In terms of memcached, the processing time of most requests on the server is basically the same. Nginx is different. Some requests from a client can respond immediately, while others involve PHP parsing, reading files, and so on. Some have several bytes of response, while others are hundreds of kb, the difference is huge.


Commonalities:

Because they all need to process high-concurrency network Io, they all adopt the asynchronous mode. Memcached used libevent (the author of memcached tested the change of libevent to libev, and only improved the performance a little. Considering the cost of changing the database, I did it ), nginx encapsulates epoll on its own (in Linux ). In fact, the effect is the same, because libevent also encapsulates epoll in Linux. Asynchronous! Asynchronous is king!

The thread relationship in memcached is basically the same as that in nginx. It is in master-worker mode.

Differences:

The main difference between the two lies in the connection listening and distribution. memcached is the main thread responsible for listening and distribution, while nginx is listening to all processes without distribution. I think these two models correspond to the differences between the two applications.

For memcached, the vast majority of requests have to be processed for a considerable amount of time, so the main thread listens and distributes connections on average. In this way, the load of each thread is almost the same, and the listening connection efficiency is also high. In this way, memcached uses multiple threads and reduces the cost of IPC without multiple processes. However, nginx is different. The request may take some time to calculate. If the simple RR still distributes the connections evenly to each worker, the load may be uneven. Some processes are relatively idle and some are busy. Therefore, nginx allows various processes to compete for locks for listening, coupled with basic load processing. This increases the lock contention cost, but basically ensures that idle processes are more likely to accept new connections. Nginx connection processing is a bit preemptible.

In fact, in Linux, the thread is only a process that shares the memory by default! Therefore, there is no difference between the memcached thread and nginx process scheduling.

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.