Memcache Multithreading concurrency Model

Source: Internet
Author: User
Tags memcached

Memcached, believe that we have a Linux back end of the migrant workers know. Here is a simple analysis of how memcached handles a large number of concurrent connections.

Title, memcached is a single process program, single process multithreaded program (Linuxer may smile, this is not a multi-process). Memcached the bottom of the libevent to manage the event, let's see how this libevent Classic application works. In fact, the first memcached is an authentic single-process program, in fact, the use of asynchronous technology after the basic CPU and network card performance to the limit (in this case, it will make the program performance decline), but later with the popularity of multi-core CPU, in order to squeeze light CPU performance, The introduction of multithreading is also a homeopathic.

Memcached's source code structure is very simple, where the thread-related codes are basically in THREAD.C. To put it simply, Memcached's many threads are a master-worker model, where the main thread is responsible for receiving the connection, then splitting the connection to each worker thread, completing the command reception, processing, and returning the results in each worker thread. Last Picture:

OK, let's begin with the main function, step by step.

In the main function, the thread-related code is basically the following lines:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 Case ' t '://Handle here the-t parameter, set the number of threads. Note the following warning, the number of threads exceeds the number of CPU cores actually meaningless, there will only be negative effect settings.num_threads = Atoi (Optarg);

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.