Complete analysis of memcached--1. The basis of memcached

My name is Nagano from the System Operations Team of the Development Department of Mixi Co. , Ltd. Responsible for the day-to-day operation of the program. From today onwards, I will explain its internal structure and usage together with Maesaka from the research and development team of our company's development department.

What is memcached?

Memcached is a software developed by Brad Fitzpatric of Danga Interactive , a subsidiary of LiveJournal . It has now become an important factor in improving the scalability of web applications in many services such as mixi , hatena , facebook , Vox , LiveJournal and many others.

Many web applications store data in an RDBMS, which is read by the application server and displayed in the browser. However, with the increase of data volume and the concentration of access, there will be major impacts such as increased burden on RDBMS, deterioration of database response, and website display delay.

This is where memcached comes into play. memcached is a high-performance distributed memory cache server. The general purpose of use is to reduce the number of database accesses by caching database query results, so as to improve the speed and scalability of dynamic Web applications.

memcached-0001-01.png

Figure 1 The use of memcached in general

Features of memcached

As a distributed cache server running at high speed, memcached has the following characteristics.

  • Simple protocol
  • libevent-based event handling
  • Built-in memory storage method
  • Distributed memcached not communicating with each other

Simple protocol

Memcached's server-client communication does not use complex formats such as XML,
but a simple line-based protocol. Therefore, data can also be saved and retrieved on memcached through telnet. Below 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     (保存命令)
bar               (数据)
STORED            (结果)
get foo           (取得命令)
VALUE foo 0 3     (数据)
bar               (数据)

The protocol documentation is located in the source code of memcached, and you can also refer to the following URL.

libevent-based event handling

libevent is a program library that
encapsulates into a unified interface. Even if the number of connections to the server increases, O(1) performance can be achieved. memcached uses this libevent library, so it can play its high performance on Linux, BSD, Solaris and other operating systems.
The event handling will not be introduced in detail here. You can refer to Dan Kegel's The C10K Problem.

Built-in memory storage method

In order to improve performance, the data saved in memcached are stored in the built-in memory storage space of memcached. Since the data only exists in memory, restarting memcached and restarting the operating system will cause all data to disappear. In addition, after the content capacity reaches the specified value, the unused cache is automatically deleted based on the LRU (Least Recently Used) algorithm. memcached itself is a server designed for caching, so it doesn't think too much about the permanence of data.
Regarding the details of memory storage, Maezaka will introduce it after the second lecture of this series, please refer to it at that time.

Distributed memcached not communicating with each other

Although memcached is a "distributed" cache server, there is no distributed function on the server side.
Individual memcacheds do not communicate with each other to share information. So, how to distribute it? It all depends on the client's implementation. This series will also introduce the distribution of memcached.

memcached-0001-02.png

Figure 2 Distributed memcached

Next, a brief introduction to the use of memcached.

install memcached

The installation of memcached is relatively simple, here is a little explanation.

memcached supports many platforms.
* Linux* FreeBSD* Solaris (memcached 1.2.5+)* Mac OS X

It can also be installed on Windows. Fedora Core 8 is used here for instructions.

Installation of memcached

Running memcached requires the libevent library described at the beginning of this article. There are ready-made rpm packages in Fedora 8, which can be installed through the yum command.

$ sudo yum install libevent libevent-devel

The source code of memcached can be downloaded from the memcached website. The latest version at the time of this writing is 1.2.5.
Although Fedora 8 also includes the rpm of memcached, the version is relatively old. Because the source code installation is not difficult,
rpm is not used here.

The memcached installation is the same as the general application, just configure, make, 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 to /usr/local/bin.

Startup of 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     88 perslab 11915
slab class   2: chunk size    112 perslab  9362
slab class   3: chunk size    144 perslab  7281
中间省略
slab class  38: chunk size 391224 perslab     2
slab class  39: chunk size 489032 perslab     2
<23 server listening
<24 send buffer was 110592, now 268435456
<24 server listening (udp)
<24 server listening (udp)
<24 server listening (udp)
<24 server listening (udp)

Debug information is shown here. In this way, memcached is started in the foreground, and the maximum memory usage of listening on TCP port 11211 is 64M. Most of the content of the debugging information is about the stored information, which will be explained in the next serialization.

When starting as a daemon background, simply

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

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

Options illustrate
-p The TCP port used. Default is 11211
-m Maximum memory size. Default is 64M
-vv Start in very vrebose mode, output debugging information and errors to the console
-d Starts in the background as a daemon

The above four are commonly used startup options, there are many others, through

$ /usr/local/bin/memcached -h

Commands can be displayed. A number of options can change various behaviors of memcached, and a read is recommended.

connect with client

Many languages ​​have implemented the client connecting to memcached, among which Perl and PHP are the main ones. Only the languages ​​listed on the memcached website have

  • Perl
  • PHP
  • Python
  • Ruby
  • C#
  • C/C++
  • take

etc.

Here's how to link memcached with the Perl library that mixi is using.

Using Cache::Memcached

Perl's memcached client has

  • Cache::Memcached
  • Cache::Memcached::Fast
  • Cache::Memcached::libmemcached

Wait for a few CPAN modules. The Cache::Memcached introduced here is the work of Brad Fitzpatric, the author of memcached, and it should be regarded as the most widely used module in the client side of memcached.

Use Cache::Memcached to connect to memcached

The following source code is an example of connecting to the just started memcached via Cache::Memcached.

#!/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, the IP address of the memcached server and an option are specified for Cache::Memcached to spawn the instance. Common options for Cache::Memcached are shown below.

Options illustrate
servers Specify memcached server and port with an array
compress_threshold The value to use when compressing the data
namespace Specifies the prefix to add to the key

In addition, Cache::Memcached can serialize the complex data of Perl through the Storable module and then save it, so hashes, arrays, objects, etc. can be directly stored in memcached.

save data

The methods of saving data to memcached are

  • add
  • replace
  • set

They are all used the same way:

my $add = $memcached->add( '键', '值', '期限' );
my $replace = $memcached->replace( '键', '值', '期限' );
my $set = $memcached->set( '键', '值', '期限' );

A period of time (in seconds) can be specified when saving data to memcached. When no deadline is specified, memcached saves data according to the LRU algorithm. The differences between the three methods are as follows:

Options illustrate
add Save only if data with the same key does not exist in the storage space
replace Save only if data with the same key exists in the storage space
set Unlike add and replace, save whenever

retrieve data

To get data you can use the get and get_multi methods.

my $val = $memcached->get('键');
my $val = $memcached->get_multi('键1', '键2', '键3', '键4', '键5');

Use get multi when getting multiple pieces of data at one time. get multi can obtain multiple key values ​​asynchronously at the same time, which is dozens of times faster than calling get in a loop.

delete data

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

$memcached->delete('键', '阻塞时间(秒)');

Deletes data for the key specified by the first argument. The second parameter specifies a time value that disables saving new data with the same key. This feature can be used to prevent incomplete cached data. But note that the set function ignores the block and saves the data as usual

increment and decrement operations

You can use specific keys on memcached as counters.

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

Increment and decrement are atomic operations, but if the initial value is not set, it will not be automatically assigned to 0. Therefore, error checking should be done, and initialization should be added if necessary. Also, the server side does not check the behavior when it exceeds 2 SUP(32).

Summarize

This time I briefly introduced memcached, its installation method, and the usage of Perl client Cache::Memcached. It is enough to know that the usage of memcached is very simple.

Next time, Maesaka will explain the internal structure of memcached. Knowing the internal structure of memcached, you can know how to use memcached to make web applications faster. Welcome to the next chapter.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325725423&siteId=291194637