memcached Complete analysis of the detailed

Source: Internet
Author: User
Tags documentation memcached memcached stats memory usage perl script


Original paste: Http://tech.idv2.com/2008/07/31/memcached-005/memcached completely parse –1. The foundation of Memcached



copyright Notice : Can reprint arbitrarily, but reprint must mark original author Charlee, original link http://tech.idv2.com/2008/07/10/memcached-001/and this statement.






Translation of a technical review of the article, is about memcached. FCICQ students said this thing is very useful, I hope you like.



Publication date: 2008/7/2
Author: Nagano Masahiro (Masahiro Nagano)
Original link: http://gihyo.jp/dev/feature/01/memcached/0001



I am Nagano of Mixi Development Department system Operation Group. Daily responsible for the operation of the program. Starting today, it will be divided into several recent hot topics in the area of scalability for Web applications, together with the memcached of our development team, to illustrate its internal structure and use.



What memcached is. Memcached feature protocol simple based on Libevent event handling built-in memory storage mode memcached The distributed installation that does not communicate with each other memcached memcached the boot of the installation memcached use the Client connection cache:: Memcached Use cache::memcached connection Memcached save data Get data delete data add one and subtract a summary of what Memcached is.



Memcached is a software developed by Brad Fitzpatric, LiveJournal's Danga Interactive company. Now it has become an important factor to improve Web application extensibility in many services, such as Mixi, Hatena, Facebook, Vox, LiveJournal and so on.



Many Web applications save data to an RDBMS, where the application server reads the data and displays it in the browser. However, with the increase of data volume and the concentration of access, there will be significant impacts such as the burden of RDBMS, the deterioration of database response and the delay of Web site display.



Then it's time to memcached. Memcached is a high-performance distributed memory cache server. The purpose of the general use is to reduce the number of database visits by caching database query results to improve the speed and scalability of dynamic Web applications.



Fig. 1 Characteristics of the use memcached of memcached in general



Memcached as a high-speed running distributed caching server, has the following characteristics. Protocol simple based on Libevent event handling built-in memory storage mode memcached communication-Less distributed 

protocol simple



memcached Server client communication does not use a format such as complex XML, but a simple text-based protocol. As a result, you can also save data and get data on memcached by Telnet. Here is an example.



$ telnet localhost 11211
Trying 127.0.0.1 ...
Connected to Localhost.localdomain (127.0.0.1).
Escape character is ' ^] '.
Set Foo 0 0 3     (Save command)
Bar               (data)
STORED            (Result)
Get foo           (take command)
VALUE Foo 0 3     (data)
Bar               (data)


The protocol document is located in the source code of memcached, or you can refer to the following URL. Http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt 

event handling based on Libevent



Libevent is a library that encapsulates the epoll of Linux, the kqueue of BSD-like operating systems and other event-handling functions into a unified interface. O (1) performance can be played even if the number of connections to the server increases. Memcached uses this libevent library to perform its high performance on Linux, BSD, Solaris, and other operating systems. About event handling here is no longer detailed, you can refer to Dan Kegel's the c10k Problem. libevent: http://www.monkey.org/~provos/libevent/the c10k Problem: http://www.kegel.com/ c10k.html 

built-in memory storage mode



To improve performance, the data saved in the memcached is stored in the memcached built-in memory storage space. Because the data exists only in memory, restarting memcached and restarting the operating system can cause all data to disappear. In addition, the unused cache is automatically deleted based on the LRU (least recently Used) algorithm after the content capacity reaches the specified value. The memcached itself is a server designed for caching, so there is no undue concern about data permanence. For more information on memory storage, the second of this series will be introduced before Sakamoto, please refer to it at that time.

  memcached distributed without communication with each other



Memcached, although a "distributed" caching server, has no distributed functionality on the server side. Each memcached does not communicate with each other to share information. So, how to distribute it. This depends entirely on the implementation of the client. This series will also introduce the distribution of memcached.



Fig. 2 Distributed memcached



Next, a brief introduction to the use of memcached. 

Install memcached



Memcached installation is relatively simple, here a little explanation.



Memcached supports many platforms. Linux FreeBSD Solaris (memcached version 1.2.5 above) Mac OS X



It can also be installed on Windows. This is illustrated using Fedora Core 8. 

installation of memcached



Running memcached requires the Libevent library that is described at the beginning of this article. Fedora 8 has a ready-made RPM package that can be installed via the Yum command.



$ sudo yum install libevent libevent-devel


Memcached source code can be downloaded from the memcached Web site. The latest version of this article is 1.2.5. Fedora 8 also contains the memcached rpm, but the version is older. Because the source code installation is not difficult, the RPM is not used here.

  download memcached: http://www.danga.com/memcached/download.bml



memcached installation is the same as a generic application, configure, make, and make install.



$ wget http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz
$ tar zxf memcached-1.2.5.tar.gz
$ CD memcached-1.2.5
$./configure
$ make
$ sudo make install


By default, memcached is installed under/usr/local/bin.

  start of the memcached



Enter the following command from the terminal to start memcached.



$/usr/local/bin/memcached-p 11211-m 64M-VV
Slab class   1:chunk size     Perslab 11915
Slab class   2:chunk size    112 Perslab  9362
Slab class   3:chunk size    144 Perslab  7281
Middle Ellipsis
Slab class  38:chunk size 391224 perslab     2
Slab class  39:chunk size 489032 perslab     2
<23 Server Listening
<24 send buffer is 110592, now 268435456
<24 Server Listening (UDP)
<24 Server Listening (UDP)
<24 Server Listening (UDP)
<24 Server Listening (UDP)


The debugging information is shown here. This starts the memcached in the foreground and listens for TCP port 11211 maximum memory usage of 64M. The content of the debugging information is mostly about the information stored, and the next time the serial is specified.



As a daemon background boot, just



$/usr/local/bin/memcached-p 11211-m 64m-d


The contents of the Memcached startup options used here are as follows.


Options Description
-P The TCP port used. Default is 11211
-M Maximum memory size. The default is 64M
-vv Boot with very vrebose mode, debug information and error output to console
-D Start as daemon in the background


The top four are commonly used startup options, others have many, through



$/usr/local/bin/memcached-h


Commands can be displayed. Many options can change the behavior of memcached, recommended for reading.

  Connecting with clients



Many languages are implemented to connect memcached clients, with Perl and PHP as the main. Only the languages listed on the memcached Web site are Perl PHP Python Ruby C # C + + Lua



Wait a minute.

  memcached Client API: HTTP://WWW.DANGA.COM/MEMCACHED/APIS.BML



Here is a way to link memcached by Mixi The Perl library you are using.

  using cache::memcached



Perl's Memcached client has cache::memcached cache::memcached::fast cache::memcached::libmemcached



And so several CPAN modules. The cache::memcached, Memcached's author, Brad Fitzpatric's work, is the most widely used module in the memcached client. cache::memcached-search.cpan.org: http://search.cpan.org/dist/Cache-Memcached/ use Cache:: memcached Connection memcached



The following source code is an example of a memcached that has just started by cache::memcached connection.



#!/usr/bin/perl

Use strict;
Use warnings;
Use cache::memcached;

My $key = "Foo";
My $value = "bar";
my $expires = 3600; # 1 hour
My $memcached = Cache::memcached->new ({
    Servers => ["127.0.0.1:11211"],
    Compress_threshold => 10_000
});

$memcached->add ($key, $value, $expires);
My $ret = $memcached->get ($key);
print "$ret/n";


Here, you specify the IP address of the memcached server and an option for cache::memcached to generate an instance. The cache::memcached commonly used options are shown below.


Options Description
Servers specifying memcached servers and ports with arrays
Compress_threshold Values to use when compressing data
Namespace Specify the prefix to add to the key


In addition, cache::memcached can then be saved after serializing complex Perl data through the Storable module, so hashes, arrays, objects, and so on can be saved directly to the memcached.

  Save Data



The method of saving data to memcached has the add replace set



They are used in the same way:



My $add = $memcached->add (' key ', ' value ', ' term ');
My $replace = $memcached->replace (' key ', ' value ', ' term ');
My $set = $memcached->set (' key ', ' value ', ' term ');


You can specify a period of time (in seconds) when you save data to memcached. When the deadline is not specified, memcached saves the data according to the LRU algorithm. The differences between the three methods are as follows:


Options Description
Add Save only if no key data exists in the storage space
Replace Save only if there is data in the same key in the storage space
Set Unlike add and replace, save at any time
Get Data


Getting the data can use the Get and Get_multi methods.



My $val = $memcached->get (' key ');
My $val = $memcached->get_multi (' Key 1 ', ' Key 2 ', ' Key 3 ', ' Key 4 ', ' key 5 ');


Use Get_multi when you get more than one piece of data at a time. Get_multi can obtain multiple key values synchronously, at a speed of dozens of times times faster than a loop call get.

  Delete Data



Deleting data uses the Delete method, but it has a unique feature.



$memcached->delete (' key ', ' blocking time (sec) ');


Deletes the data for the key specified by the first parameter. The second parameter specifies a time value that prevents the use of the same key to save the new data. This feature can be used to prevent the incomplete caching of data. However, it is important to note that theset function ignores the blocking and saves the data as usual and minus one operation



You can use a specific key value on the memcached as a counter.



My $ret = $memcached->incr (' key ');
$memcached->add (' key ', 0) unless defined $ret;


Add one and minus one is Atomic operation, but does not set the initial value, will not be automatically assigned 0. Therefore, error checking should be done, and initialization should be added if necessary. Also, the server side does not check for behavior that exceeds the 2<sup>32</sup>. 

Summary



This is a brief introduction to Memcached, and its installation method, and the use of Perl client cache::memcached. As long as you know, the use of memcached is very simple enough.



The next time the memcached's internal structure is explained by the former Sakamoto. Understanding the internal structure of memcached makes it possible to know how to use memcached to make Web applications faster. Please continue reading the next chapter.







How do you feel after reading this article? If you need more content, you can look at the following, may be helpful to you: memcached comprehensive Analysis--pdf summary memcached comprehensive analysis--5. memcached Application and Compatibility program memcached comprehensive analysis of--4. The memcached distributed algorithm memcached comprehensively analyzes the--3.memcached deletion mechanism and the development direction memcached comprehensively analyzes--2. Understanding memcached Memory Storage Apache Report no spaces left on Device solution How to use UTF-8 coded diff command in download file name OpenNMS Configure note Web Communication analysis tool



There are 16 comments in this article, come and discuss it together. Complete analysis of #1 memcached
2008-07-17 13:01



[...] Read the full text: memcached fully analyze [...] #2 memcached distributed algorithm-consistent hashing | Pioneer-Internet Technology development practice
2008-07-28 22:30



[...] Next, the distributed approach to the Perl client function library cache::memcached implementation mentioned in the 1th time is described. [...] #3»links for 2008-07-17 Moonlight Blog Net Pick
2008-08-01 11:13



[...] memcached Complete analysis of memcached is a livejournal under the Danga Interactive company developed a software. Now it has become an important factor to improve Web application extensibility in many services, such as Mixi, Hatena, Facebook, Vox, LiveJournal and so on. (Tags:programming web) [...] #4 CY Tech Er ' s blog»memcached completely parse –1. The foundation of Memcached
2008-08-13 16:32



[...] 13 August, 2008 (16:32) | memcache| By:xiao.jia memcached completely parse –1. The foundation of the memcached [...] #5 CY Tech Er's blog»memcached completely parse –1. The foundation of Memcached
2008-08-13 16:36



[...] What memcached is. [...] #6 CY Tech Er ' blog»memcached comprehensive analysis of –2. Understanding memcached Memory Storage
2008-08-13 16:38



[...] The last article described the memcached as a distributed cache server. [...] #7 CY Tech Er ' blog»memcached comprehensive analysis of –5. memcached Application and Compatibility program
2008-08-13 16:41



[...] 1th time: http://tech.idv2.com/2008/07/10/memcached-001/[...] #8 CY tech Er ' blog»memcached comprehensive analysis –4. A distributed algorithm for memcached
2008-08-13 16:42



[...] 1th time: http://tech.idv2.com/2008/07/10/memcached-001/[...] #9 haohtml ' s blog»blog archive»memcached complete anatomy
2008-09-24 19:19



[...] What memcached is. [...] A distributed algorithm for the»blog archive»memcached of the #10 dust-free residence-consistent hashing[turn]
2008-10-07 13:43



[...] Next, the distributed approach to the Perl client function library cache::memcached implementation mentioned in the 1th time is described. [...] #11 User links about "libevent" on Ilinkshare
2008-10-12 11:03



[...] | User-saved Public Links | Ilinkshare 1 votesmemcached completely analyzes –1. Memcached base >> Saved by F1lmjunkie 2 days Ago4 votes[original] Future distributed BBS possible implementation architecture ...>> saved by [...] #12 recent Faves Tagge D with "c10k": mynetfaves
2008-10-22 17:51



[...] The public links >> c10k memcached completely parse –1. Memcached the base of the Saved by Moongreebs | 1 days ago PURCHASE Hi Capacity SLAs UPS Battery for APC BackUPS [...] #13 the time of silence»memcached distributed algorithm-consistent hashing[turn]
2008-10-23 14:41



[...] Next, the distributed approach to the Perl client function library cache::memcached implementation mentioned in the 1th time is described. [...] #14 Maohuibo
2008-11-05 16:30



Nice #15. Large-scale distributed cache correlation at We-life (Vihan blog)
2008-11-21 13:38



[...] http://tech.idv2.com/2008/07/10/memcached-001/[...] #16 anything is possible.»blog Archive»memcache Tutorial
2008-11-28 16:20



[...] Http://tech.idv2.com/2008/07/10/memcached-001/admin on November 29th, 2008 | File Under memcached | - [...]






memcached comprehensive analysis of –2. Understanding memcached Memory Storage



copyright Notice : Can reprint arbitrarily, but reprint must mark original author Charlee, original link http://tech.idv2.com/2008/07/11/memcached-002/and this statement.






Here is the second part of the comprehensive anatomy of memcached.



Publication date: 2008/7/9
Author: Former Sakamoto (Toru Maesaka)
Source text link: http://gihyo.jp/dev/feature/01/memcached/0002 slab allocation Mechanism: the principle of caching records in allocation by organizing memory to reuse the main terms of slab slab Disadvantages of slab allocator use growth factor to tune the internal status of the memcached view Slabs usage Summary of memory storage



I am former mixi of the research and Development Group of the corporation. The last article described the memcached as a distributed cache server. This will introduce how memcached's internal constructs are implemented, and how memory is managed. In addition, the weaknesses resulting from the internal structure of the memcached will also be explained. slab allocation mechanism: Defragmenting memory for reuse



The most recent memcached by default uses a mechanism named slab allocator to allocate and manage memory. Prior to this mechanism, the allocation of memory was done simply by malloc and free for all records. However, this approach can lead to memory fragmentation, burdening the operating system memory manager, and, in the worst case, slower operating systems than the memcached process itself. Slab allocator was born to solve the problem.



Here's a look at the slab allocator principle. The following are the objectives of the slab allocator in the memcached documentation:



The primary goal of the slabs subsystem in memcached is to eliminate memory fragmentation a issues totally by using fixed-s Ize memory chunks coming from few predetermined size classes.



In other words, the basic principle of slab allocator is to split the allocated memory into a specific length block to completely resolve the memory fragmentation problem, according to the predetermined size.



The principle of slab allocation is quite simple. Divides the allocated memory into blocks of various dimensions (chunk) and divides the same blocks into groups (the chunk set) (Figure 1).



Fig. 1 structure diagram of slab allocation



Also, slab allocator has the purpose of reusing allocated memory. In other words, the allocated memory is not released, but reused. 

The main terms of slab allocation



Page



The memory space assigned to slab is 1MB by default. After assigning to slab, it is divided into chunk according to the size of the slab.



Chunk



The memory space used to cache records.



Slab Class



A group of chunk of a specific size. 

The principle of caching records in Slab



The following explains how memcached selects slab for the data sent by the client and caches it in chunk.



Memcached selects the slab that best fits the data size based on the size of the data received (Figure 2). Memcached contains a list of slab free chunk, select chunk from the list, and then cache the data in it.



Figure 2 Method for selecting a group to store records



In fact, slab allocator also has advantages and disadvantages. Here are some of its drawbacks. 

The disadvantage of slab allocator



Slab allocator solves the original memory fragmentation problem, but the new mechanism also brings new problems to memcached.



The problem is that the allocated memory is not effectively utilized because of the memory allocated for a specific length. For example, by caching 100 bytes of data into a 128-byte chunk, the remaining 28 bytes are wasted (Figure 3).



Fig. 3 The use of chunk space



There is no perfect solution for this problem, but a more effective solution is documented in the document.



The most efficient way to reduce the waste are to use a list of size classes this closely matches (if that ' s in all Possibl e) Common sizes of objects that clients of this particular installation of memcached are to store.



That is, if you know in advance the common size of the data sent by the client, or if you only cache data of the same size, you can reduce waste by using a list of groups that fit the data size.



But unfortunately, it is not possible to do any tuning, only to look forward to the future version. However, we can adjust the size difference of the slab class. Next, the growth factor option is described.

  using growth factor for tuning



memcached specifies the growth factor factor (through the-f option) at startup to control the differences between slab to some extent. The default value is 1.25. However, before this option appears, this factor was once fixed to 2, known as the "Powers of 2" policy.



Let's use the previous setup to verbose mode to start memcached try:



$ memcached-f 2-VV


The following is the verbose output after startup:



Slab class   1:chunk size    128 Perslab  8192
Slab class   2:chunk size    256 Perslab  4096
Slab class   3:chunk size    perslab  2048
Slab class   4:chunk size   1024 Perslab  1024
Slab class   5:chunk size   2048 perslab   512
Slab class   6:chunk size   4096 Perslab   256
Slab class   7:chunk size   8192 perslab   128
Slab class   8:chunk size  16384 perslab    64
Slab class   9:chunk size  32768 Perslab    32
Slab class  10:chunk size  65536 perslab    16
Slab class  11:chunk size 131072 Perslab     8
Slab class  12:chunk size 262144 Perslab     4
Slab class  13:chunk size 524288 perslab     2


Visible, starting with a 128-byte group, the group size increases to twice times the original. The problem with this setting is that there is a large difference between the slab, and in some cases it is quite a waste of memory. Therefore, to minimize memory waste, two years ago the growth factor was appended to this option.



To see the output of the current default (f=1.25) (space is limited to the 10th group):



Slab class   1:chunk size     Perslab 11915
Slab class   2:chunk size    112 Perslab  9362
Slab class   3:chunk size    144 Perslab  7281
Slab class   4:chunk size    184 perslab  5698
Slab class   5:chunk size    232 Perslab  4519
Slab class   6:chunk size    296 perslab  3542
Slab class   7:chunk size    376 Perslab  2788
Slab class   8:chunk size    472 perslab  2221
Slab class   9:chunk size    592 perslab  1771
Slab class  10:chunk size    744 perslab  1409


Visible, the gap between groups is much smaller than 2 o'clock, more suitable for caching hundreds of-byte records. From the output above, there may be some computational errors that are deliberately set to keep the number of bytes aligned.



When you introduce memcached to a product, or deploy it directly using default values, it is best to recalculate the expected average length of the data and adjust the growth factor to get the most appropriate settings. Memory is a precious resource, so it's a shame to waste it.



Here's how to use the memcached Stats command to view the slabs utilization, and all sorts of information.

  View internal status of Memcached



Memcached has a command called stats that uses it to get a wide variety of information. There are many ways to execute commands, with Telnet the simplest:



$ telnet Host name Port number




The details of these commands can refer to the Protocol.txt documentation in the Memcached software package.



$ telnet localhost 11211
Trying:: 1 ...
Connected to localhost.
Escape character is ' ^] '.
Stats
STAT PID 481
STAT Uptime 16574
STAT Time 1213687612
STAT version 1.2.5
STAT Pointer_size 32
STAT Rusage_user 0.102297
STAT Rusage_system 0.214317
STAT Curr_items 0
STAT Total_items 0
STAT bytes 0
STAT Curr_connections 6
STAT Total_connections 8
STAT Connection_structures 7
STAT Cmd_get 0
STAT Cmd_set 0
STAT Get_hits 0
STAT get_misses 0
STAT Evictions 0
STAT Bytes_read 20
STAT Bytes_written 465
STAT limit_maxbytes 67108864
STAT Threads 4
End
Quit


In addition, the MemStat command is installed if libmemcached is installed for this client library that is oriented to C + + language. Using the method is simple, you can get the same information as Telnet with fewer steps, and you can get information from multiple servers at once.



$ memstat--servers=server1,server2,server3,...


Libmemcached can be obtained from the following address: http://tangent.org/552/libmemcached.html 

View Slabs usage



Using Memcached's Perl script, named Memcached-tool, created by Brad, you can easily get slab usage (it collates memcached return values into Easy-to-read format). You can get the script from the following address:


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.