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); |