Complete memcached analysis-1. Foundation of memcached

Source: Internet
Author: User

Translation of a technical reviewArticleIt is about the serialization of memcached. Fcicq said this is very useful and I hope you will like it.

Posting date: 2008/7/2
Author: Masahiro Nagano)

I am Nagano from the system operation group of Mixi Development Department. Daily responsibilitiesProgram. Starting from today, we will discuss several times about memcached, a hot topic in the extensibility field of Web applications, and describe its internal structure and use together with the preparations of our development department Research and Development Team.

    • What is memcached?
    • Memcached features
      • Simple Protocol
      • Libevent-based event processing
      • Built-in memory storage
      • Distributed memcached does not communicate with each other
    • Install memcached
      • Install memcached
      • Start memcached
    • Connect with a client
    • Use cache: memcached
      • Use cache: memcached to connect to memcached
      • Save data
      • Get Data
      • Delete data
      • Add and subtract operations
    • Summary
What is memcached?

Memcached is a software developed by Brad fitzpatric, danga interactive under livejournal. It has become an important factor in improving the scalability of Web applications among many services such as Mixi, hatena, Facebook, Vox, and livejournal.

Many Web applications save data to RDBMS. The application server reads data from the data and displays it in the browser. However, as the data volume increases and access is concentrated, the burden on RDBMS increases, the database response deteriorates, and the website display latency.

In this case, memcached is ready to use. Memcached is a high-performance distributed memory cache server. The general purpose is to reduce the number of database accesses by caching database query results, so as to speed up dynamic web applications and improve scalability.

Figure 1 General Purpose of memcached

Memcached features

As a high-speed distributed cache server, memcached has the following features.

    • Simple Protocol
    • Libevent-based event processing
    • Built-in memory storage
    • Distributed memcached does not communicate with each other
Simple Protocol

For memcached Server Client Communication, simple text line-based protocols are used instead of complex XML and other formats. Therefore, you can use Telnet to save and retrieve data on memcached. The following is an example.


$ Telnet local host 11211
Connected to localhost. localdomain ( ).
Escape Character is '^]'.
Set Foo 0 0 3 (save command)
Bar (data)
Stored (result)
Get Foo (GET command)
Value Foo 0 3 (data)
Bar (data)

The protocol document is in the memcachedSource codeYou can also refer to the following URL.

    • Http://
Libevent-based event processing

Libevent is a library that encapsulates the epoll, kqueue, and other event processing functions of Linux operating systems into a unified interface. O (1) performance can be used even if the number of connections to the server increases. Memcached uses this libevent library to achieve high performance in Linux, BSD, Solaris, and other operating systems. For more information about event processing, see the c10k problem of Dan keel.

    • Libevent: Http:// /~ Provos/libevent/
    • The c10k Problem: Http://
Built-in memory storage

To improve performance, data stored in memcached is stored in the memory storage space built in memcached. Because the data only exists in the memory, restarting memcached and the operating system will cause all data to disappear. In addition, after the content capacity reaches the specified value, it is based on LRU (least recently used)AlgorithmThe unused cache is automatically deleted. Memcached itself is a server designed for caching, so it does not take the permanent data into consideration. For more information about memory storage, refer to the introduction in the future.

Distributed memcached does not communicate with each other

Although memcached is a "distributed" cache server, the server does not have distributed functions. Memcached does not communicate with each other to share information. So, how to implement distributed? This depends entirely on the implementation of the client. This section also describes the distribution of memcached.

Figure 2 distributed memcached

Next, we will briefly introduce how to use memcached.

Install memcached

Memcached is easy to install.

Memcached supports many platforms.

    • Linux
    • FreeBSD
    • Solaris (memcached 1.2.5 or later)
    • Mac OS X

It can also be installed on Windows. Here, Fedora Core 8 is used for description.

Install memcached

To run memcached, you must first introduce the libevent library. There is a ready-made RPM package in fedora 8. You can install it using the yum command.

$ Sudo Yum install libevent-devel

Memcached SourceCodeIt can be downloaded from the memcached website. The latest version of this article is 1.2.5. Although RPM ora 8 also contains the memcached rpm, the version is old. Because it is not difficult to install the source code, rpm is not used here.

    • Download memcached: Http://

The memcached installation is the same as that of common applications. You can use configure, make, and make install.


$ Wget
$ Tar zxf memcached-1.2.5.tar.gz
$ Memcached-1.2.5 CD
$ Make
$ Sudo make install

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

Start memcached

Enter the following command from the terminal to start memcached.

$/Usr/local/bin/memcached-P 11211-M 64 m-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)

Debugging information is displayed here. In this way, memcached is started on the front-end and the maximum memory usage of TCP port 11211 is 64 MB. Most of the debugging information is about the storage information.

When started as a daemon background, you only need


$/Usr/local/bin/memcached-P 11211-M 64 m-d

The content of the memcached startup option used here is as follows.

Option Description
-P The TCP port used. The default value is 11211.
-M Maximum memory size. The default value is 64 MB.
-VV Start in very vrebose mode, and output debugging information and errors to the console.
-D Start daemon in the background

The above four are commonly used startup options, and there are many other options.



Command to display. Many options can change the behaviors of memcached. Read-Only is recommended.

Connect with a client

In many languages, clients connected to memcached are implemented, including Perl and PHP. Only the languages listed on the memcached website have

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

And so on.

    • Memcached client API: Http://

Here we will introduce how to link memcached through the Perl library being used by Mixi.

Use cache: memcached

The memcached client of Perl has

    • Cache: memcached
    • Cache: memcached: fast
    • Cache: memcached: libmemcached

And other CPAN modules. Cache: memcached is the work of Brad fitzpatric, creator of memcached. It should be regarded as the most widely used module in the memcached client.

    • Cache: Http://
Use cache: memcached to connect to memcached

The source code below is an example of connecting memcached just started through 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 => [" 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 generate an instance. Cache: Common memcached options are as follows.

Option Description
Servers Specify the memcached server and port with Arrays
Compress_threshold Value used for Data Compression
Namespace Specify the prefix to add to the key

In addition, cache: memcached uses the Storable module to serialize and save complex Perl data. Therefore, hashes, arrays, and objects can be directly stored in memcached.

Save data

The methods for saving data to memcached are as follows:

    • Add
    • Replace
    • Set

They are used in the same way:


My $ add = $ memcached-> Add ('key', 'value', 'deadline ');
My $ replace = $ memcached-> Replace ('key', 'value', 'duration ');
My $ set = $ memcached-> set ('key', 'value', 'deadline ');

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

Option Description
Add It is saved only when no data with the same key exists in the bucket.
Replace It is saved only when data with the same key exists in the bucket.
Set Unlike add and replace
Get Data


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

Use get_multi to retrieve multiple data records at a time. Get_multi can obtain multiple key values synchronously, Which is dozens of times faster than loop get.

Delete data

The delete method is used to delete data, but it has a unique function.


$ Memcached-> Delete ('key', 'blocking time (seconds )');

Deletes the data of the key specified by the first parameter. The second parameter specifies a time value. You cannot use the same key to save new data. This function can be used to prevent incomplete cached data. Note that,The Set function ignores this blocking and saves data as usual.

Add and subtract operations

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


My $ ret = $ memcached-> incr ('key ');
$ Memcached-> Add ('key', 0) Unless defined $ ret;

Increment and subtract 1 are atomic operations, but when the initial value is not set, it is not automatically assigned to 0. Therefore, an error check should be performed and initialization should be performed if necessary. In addition, the server does not check the behavior that exceeds 2 32.


This article briefly introduces memcached and its installation method and the usage of Perl Client Cache: memcached. As long as you know, memcached is easy to use.

Next, we will explain the internal structure of memcached from the beginning. By understanding the internal structure of memcached, you can know how to use memcached to accelerate web applications. Welcome to the next chapter.

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