Memcached Cache server Introduction and its installation process

Source: Internet
Author: User
Tags zts

What is Memcache?

Memcached is a software developed by Brad Fitzpatric, a Danga Interactive company in LiveJournal. Right now

has become an important factor in improving Web application extensibility in many services such as Mixi, Hatena, Facebook, Vox, and LiveJournal.

The memcached is an open-source, high-performance, distributed memory object caching system that can apply a variety of scenarios that require caching, primarily to speed up Web applications by reducing access to the database. 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, the burden of RDBMS, database response deterioration, site display delay and other significant impact. This is the time to memcached. Memcached general purpose is to reduce the number of database accesses by caching the results of database queries to improve the speed and scalability of dynamic Web applications. It is a memory-based "key-value pair" store that stores direct data for database calls, API calls, or page reference results, such as strings, objects, and so on.


Its working flow chart is:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5C/1D/wKioL1Ua4PSzswJMAAEWf3sN1Q8663.jpg "title=" Memc.png "alt=" Wkiol1ua4pszswjmaaewf3sn1q8663.jpg "/>


Characteristics of memcached

Memcached, as a distributed memory cache server running at high speed, has the following characteristics.

Simple protocol

Libevent-based event handling

Built-in memory storage mode

Memcached distributed without communication with each other


Simple protocol

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


Libevent-based event handling

Libevent is a library that encapsulates event-handling functions such as Linux's Epoll, BSD-class operating system kqueue, and so on as a unified interface. The Performance of O (1) can be played even if the number of connections to the server increases. Memcached uses this libevent library, so it can perform its high performance on Linux, BSD, Solaris and other operating systems.


Built-in memory storage mode

To improve performance, the data saved in memcached is stored in Memcached's built-in memory storage space. Because the data exists only in memory, restarting the memcached and restarting the operating system will cause all data to disappear. Additionally, when the content capacity reaches the specified value, the unused cache is automatically deleted based on the LRU (Least recently used) algorithm. The memcached itself is a server designed for caching, so there is not too much consideration for permanent data issues.

Memcached by default, a mechanism named slab allocator is used to allocate and manage memory. Prior to the advent of this mechanism, the allocation of memory was performed simply by malloc and free for all records. However, this approach can lead to memory fragmentation, aggravating the burden on the operating system memory manager, and in the worst case, cause the operating system to be slower than the memcached process itself. Slab Allocator was born to solve the problem.

The basic principle of Slab allocator is to divide the allocated memory into blocks of various sizes (chunk) According to the predetermined size, and divide the same size blocks into groups (the chunk collection). The chunk size of each size is determined by its growth factor (growth factor), with a default growth factor of 1.25. The chunk size of the next dimension = the previous chunk size * growth factor. As shown in the following:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/5C/22/wKiom1Ua38mxEnHBAAE_1gsrwY0596.jpg "title=" 2008092816564538.png "alt=" Wkiom1ua38mxenhbaae_1gsrwy0596.jpg "/>


Also, slab allocator has the purpose of reusing allocated memory. In other words, the allocated memory is not freed, but reused. However, since slab allocator allocates a specific length of chunk, it can still cause wasted space when the cached data does not have a large chunk length.



Memcached distributed without communication with each other

Memcached Although it is a "distributed" cache server, there is 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 client's implementation.





Memcached's philosophy of design

Memcached is a development tool that is neither a code accelerator nor a database middleware. The philosophy of design is mainly reflected in the following aspects:

1. Simple Key/value Storage: The server does not care about the meaning and structure of the data itself, as long as the data can be serialized. The stored items are composed of "key, expiration time, optional flag and data" four parts;

2. The implementation of the function is half dependent on the client, half based on the server side: the customer is responsible for sending the storage items to the server side, the data from the service side and unable to connect to the server when the corresponding action; The service side is responsible for receiving, storing data, and responsible for the data item timeout expires;

3. Each server ignores each other: does not synchronize the data between the servers;

4. O (1) Efficiency of implementation

5. Cleanup of Extended Data: By default, Memcached is an LRU cache, and it cleans up extended data as long as it is pre-booked, but in fact, memcached does not delete any cached data but is no longer visible to the customer after it expires; Memcached also does not really clean up the cache by age, but only when the get command arrives, checking its duration;



Memcached provides a handful of commands to accomplish server-side interactions, which are based on memcached protocol implementations.

Storage class Commands: Set, add, replace, append, prepend

Get Data Class command: Get, delete, INCR/DECR

Statistics class commands: Stats, stats items, stats slabs, stats sizes

Cleanup command: Flush_all


First, install the Libevent


Memcached relies on the libevent API, so to install it beforehand, the project home page: http://libevent.org/, readers can choose the version they want to download. This article is using the latest version of the source package libevent-2.0.16-stable.tar.gz. Installation process:


# Tar XF libevent-2.0.22-stable.tar.gz

# CD Libevent-2.0.22-stable

#./configure--prefix=/usr/local/libevent

# Make && make install


# echo "/usr/local/libevent/lib" >/etc/ld.so.conf.d/libevent.conf

# Ldconfig


Second, installation configuration memcached


1, installation memcached

# Tar XF memcached-1.4.22.tar.gz

# CD memcached-1.4.22

#./configure--prefix=/usr/local/memcached--with-libevent=/usr/local/libevent (libevent installation directory)

# Make && make install



2, memcached sysv the startup script code as shown below, set it as a/etc/init.d/memcached file:


#!/bin/bash

#

# Init file for memcached

#

# Chkconfig:-86 14

# description:distributed Memory Caching daemon

#

# processname:memcached

# config:/etc/sysconfig/memcached


. /etc/rc.d/init.d/functions


# # Default Variables

Port= "11211"

User= "Nobody"

maxconn= "1024"

Cachesize= "64"



Retval=0

Prog= "/usr/local/memcached/bin/memcached"

desc= "Distributed Memory Caching"

Lockfile= "/var/lock/subsys/memcached"


Start () {

Echo-n $ "Starting $desc (memcached):"

Daemon $prog-D-P $PORT-U $USER-C $MAXCONN-M $CACHESIZE

Retval=$?

Echo

[$RETVAL-eq 0] && Touch $lockfile

Return $RETVAL

}


Stop () {

Echo-n $ "Shutting down $desc (memcached):"

Killproc $prog

Retval=$?

Echo

[$RETVAL-eq 0] && rm-f $lockfile

Return $RETVAL

}


Restart () {

Stop

Start

}


Reload () {

Echo-n $ "Reloading $desc ($prog):"

Killproc $prog-hup

Retval=$?

Echo

Return $RETVAL

}


Case "$" in

Start

Start

;;

Stop

Stop

;;

Restart

Restart

;;

Condrestart)

[-e $lockfile] && restart

Retval=$?

;;

Reload

Reload

;;

Status

Status $prog

Retval=$?

;;

*)

echo $ "Usage: $ {Start|stop|restart|condrestart|status}"

Retval=1

Esac


Exit $RETVAL



Configure memcached to become a system service using the following command:

# chmod +x/etc/init.d/memcached

# chkconfig--add memcached

# service memcached Start


See if the memcached process has started. Such as:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/5C/1D/wKioL1Ua4SLTpjhnAAJW81BSlgw966.jpg "title=" Php-fpm.png "alt=" Wkiol1ua4sltpjhnaajw81bslgw966.jpg "/>



3. Use the Telnet command to test the use of memcached


Memcached provides a set of basic commands for invoking its services or viewing server status based on the command line.

View internal status information for memcached

The first is to telnet to memcached, and then enter stats to get its memcached various information:

# telnet 127.0.0.1 11211

Stats

STAT PID 14381

STAT Uptime 307

STAT Time 1427814867

STAT version 1.4.22

STAT libevent 2.0.22-stable

STAT Pointer_size 64

STAT Rusage_user 0.000000

STAT Rusage_system 0.002999

STAT Curr_connections 10

STAT total_connections 11

STAT Connection_structures 11

STAT Reserved_fds 20

STAT Cmd_get 0

STAT Cmd_set 0

STAT Cmd_flush 0

STAT Cmd_touch 0

STAT Get_hits 0

STAT get_misses 0

STAT delete_misses 0

STAT Delete_hits 0

STAT incr_misses 0

STAT Incr_hits 0

STAT decr_misses 0

STAT Decr_hits 0

STAT cas_misses 0

STAT Cas_hits 0

STAT Cas_badval 0

STAT Touch_hits 0

STAT touch_misses 0

STAT Auth_cmds 0

STAT auth_errors 0

STAT Bytes_read 7

STAT Bytes_written 0

STAT limit_maxbytes 67108864

STAT Accepting_conns 1

STAT Listen_disabled_num 0

STAT Threads 4

STAT Conn_yields 0

STAT Hash_power_level 16

STAT hash_bytes 524288

STAT hash_is_expanding 0

STAT Malloc_fails 0

STAT bytes 0

STAT Curr_items 0

STAT Total_items 0

STAT expired_unfetched 0

STAT evicted_unfetched 0

STAT Evictions 0

STAT reclaimed 0

STAT crawler_reclaimed 0

STAT lrutail_reflocked 0

END


Enter stats slabs or stats items to get information about their cache, such as:

Stats Slabs

STAT Active_slabs 0

STAT total_malloced 0

END

Stats items

END


Test, add data using the Add command

The add command syntax format:

Add KeyName Flag Timeout datasize

Such as:

Add MyKey 0 10 12

Hello world!


Get the data for the specified key using the GET command

The GET command syntax format is:

Get KeyName

such as: Get MyKey

VALUE MyKey 0 12

Hello world!

END


4, memcached of common options description


-L <IP_ADDR>: Specifies the address of the process listener;

-D: Run in service mode;

-U <username>: Runs the memcached process as a specified user;

-M <num>: Maximum memory space for cache data in MB, default of 64MB;

-c <num>: Maximum number of concurrent connections supported, default is 1024;

-P <num>: Specifies the listening TCP port, which defaults to 11211;

-U <num>: Specifies the UDP port to listen on, the default is 11211,0 to close the UDP port;

-T <threads>: The maximum number of threads used to process inbound requests, only the support line friend valid at memcached compile time;

-F <num>: Set slab allocator defines the growth factor used when pre-allocating blocks of fixed memory space;

-M: Returns an error message when the memory space is not available, rather than using the space according to the LRU algorithm;

-N: Specifies the minimum slab chunk size; the unit is byte;

-S: Enable SASL for user authentication;



Note: Here, memcached server has been set up, the following also need to configure PHP, let IT support the memcached function. The above and below sections are not on the same server.



Third, install memcache PHP extension, use PHP to support memcached function

The installation of PHP is no longer detailed here.

① installing PHP's memcache extension

# wget Http://pecl.php.net/get/memcache-2.2.4.tgz

# Tar XF memcache-2.2.4.tgz

# CD memcache-2.2.4

#/usr/local/php/bin/phpize

#./configure--with-php-config=/usr/local/php/bin/php-config--enable-memcache

# Make && make install


There will be a hint similar to the following when the installation is complete:

Installing Shared extensions:/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/


② edit Vim/etc/php.d/memcache.ini, add the following line information:

Extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/memcache.so


Then test the memcached function, build the test page test.php in the root directory of the website and add the following:

# vim/usr/html/test.php

<?php

$mem = new Memcache;

$mem->connect ("192.168.108.199", 11211) or Die ("Could not Connect"); # #这里我的memcached服务器所在地址为192.168.108.199


$version = $mem->getversion ();

echo "Server ' s version:". $version. " <br/>\n ";


$mem->set (' TestKey ', ' Hello World ', 0, +) or Die ("Failed-to-save data at the memcached server");

echo "Store data in the cache (data would expire in seconds) <br/>\n";


$get _result = $mem->get (' TestKey ');

echo "$get _result is from memcached server.";

?>


Test, enter http://192.168.108.230/test.php on the browser, if the following information is displayed, it indicates that Memcache is already working properly.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/5C/22/wKiom1Ua4AfCMSCZAAEAWJUz7rk587.jpg "title=" Memcache.png "alt=" Wkiom1ua4afcmsczaaeawjuz7rk587.jpg "/>


Iv. Client Tools using libmemcached

The traditional way to access memcached is to use the Perl-based cache::memcached module, which works well in most Perl code, but also has a well-known performance problem. Libmemcached is an open-source C/C + + code-based memcached library file that has been developed for use in the language, and it also provides several memcached management tools that can be used remotely, such as Memcat, Memping,memstat, Memslap and so on.


1) Compile and install libmemcached


# Tar XF libmemcached-1.0.2.tar.gz

# CD libmemcached-1.0.2

#./configure

# Make && make install

# Ldconfig


2) Client Tools

# Memcat--servers=127.0.0.1:11211 MyKey

# memping

# Memslap

# MemStat



Five, Nginx integration memcached, for Nginx to provide static page caching function

Although Nginx itself can provide caching, because the data is cached on disk, performance does not catch memcached cache performance. Therefore, we should also support the memecached feature for Nginx, so that it supports caching static page function.

server {

Listen 80;

server_name www.xsl.com;


#charset Koi8-r;


#access_log Logs/host.access.log Main;


Location/{

Set $memcached _key $uri; $memcached _key is the key name, $uri the key passed to the memcached server

Memcached_pass 192.168.108.199:11211; Address and port of the memcached server

Default_type text/html; Cache only HTML-formatted data

Error_page 404 @fallback; If there is no corresponding data on the memcached, it will be found on the backend application server.

}


Location @fallback {

Proxy_pass http://127.0.0.1; The address here refers to the address of the backend application server.

}

}


Vi. memcached management and monitoring tools memadmin

Memadmin is a visual memcached management and Monitoring tool, using PHP development, small size, simple operation.


Main functions:

Server parameter monitoring: STATS, SETTINGS, ITEMS, slabs, sizes real-time refresh

Server performance monitoring: GET, DELETE, INCR, DECR, CAs and other common operation hit rate real-time monitoring

Supports data traversal for easy monitoring of stored content

Supports conditional queries to filter out key or value that satisfies a condition

Array, JSON, and other serialized character deserialization display

Other services that are compatible with the Memcache protocol, such as Tokyo Tyrant (except traversal functionality)

Support server connection pool, multi-server management switch convenient and concise

Installing Memadmin


This article is from the "Linux Learning path" blog, so be sure to keep this source http://xslwahaha.blog.51cto.com/4738972/1627129

Memcached Cache server Introduction and its installation process

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.