Memcached source code analysis starts with the SET command

Author: Calix

If you directly start the source code of memcached from the main function, I am afraid it will be a big nod, so here is a classic "SET" command to simply start, which is to recall the role of memcached. It is also based on the analysis of the SET command. I believe that if you understand what is behind a SET command, then most of the source code of memcached is well understood.

So, recall, what does the set command do?

It is nothing more than setting a value to a key and saving it in memory.

To refine a bit more:

1) memcached is a cache server. When we use it, we often use it through a client, such as the memcached client of php, or directly through telnet in the linux terminal. Either way, the first behavior is to "establish a connection". (Note that although memcached is a distributed cache server, its actual distribution algorithm is implemented on the client side, so the distributed logic is not within the scope of this source code analysis.) So, how does memcached handle network requests? Is it multi-process or asynchronous?

2) When the connection is established, we are sending a SET command, for example, after the terminal telnet is successful, type: "set name 0 0 3\r\n Tom\r\n" (this is the set protocol of memcached), So how does memcached obtain this command and data, how does it parse it after it is obtained, and which modules do it hand over to execute it?

3) After parsing to the set command, it knows to save the data of a kv structure to the memory, so how does it allocate the memory? After the allocation, how can we ensure that the next time we take it, we can quickly get the "value" through the "key"? And how to ensure atomicity and consistency in the case of concurrency?

With the above questions, I divided the structure of memcached source code analysis into three main parts:

1) Thread model of memcached source code analysis

2) Request processing for memcached source code analysis (state machine)

3) Memory management of memcached source code analysis

Please indicate: Calix  »  Memcached source code analysis starts with the SET command

Reprinted in: https://www.cnblogs.com/guolanzhu/p/5849996.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324049876&siteId=291194637