Memcached completely parse –1. The foundation of Memcached

Source: Internet
Author: User
Tags memcached
http://kb.cnblogs.com/page/42731/
Series article Navigation:

Memcached completely parse –1. The foundation of Memcached

memcached comprehensive analysis of –2. Understanding the memory storage of memcached

memcached comprehensive analysis of –3. The deletion mechanism and development direction of memcached

memcached comprehensive analysis of –4. A distributed algorithm for memcached

memcached comprehensive analysis of –5. memcached Application and Compatibility program

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 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.

option description
servers specify memcached with array Service and Port

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.