Principles and Practices of memcached

Source: Internet
Author: User

1. Introduction to 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.

2. memcached features
As a high-speed distributed cache server, memcached has the following features:
1) protocol is simple: memcached Server client communication does not use complicated XML and other formats, but uses a simple text line-based protocol. Therefore, you can use telnet to save and retrieve data on memcached.
2) libevent-based event processing: libevent is a library that encapsulates Linux epoll, BSD operating systems, kqueue, and other event processing functions 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.
3) 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, when 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 does not take the permanent data into consideration.
4) distributed memcached does not communicate with each other: Although memcached is a "distributed" cache server, it does not have distributed functions on the server side. Memcached does not communicate with each other to share information. Then, the distributed architecture is completely dependent on the implementation of the client.
------------------------------------------
3. Slab Allocation mechanism: sort out the memory for Reuse
Currently, memcached uses the Slab Allocator mechanism to allocate and manage memory. The basic principle of Slab Allocator is to divide the allocated memory into blocks of a specific length according to the predefined size to completely solve the memory fragmentation problem.

1) As shown in, Slab Allocation splits the allocated memory into blocks of various sizes (chunk, memory space used for cache record ), and divide the chunk into groups with the same size. Each chunk has the same size ). By default, the size of an slab (chunks) is 1 MB, which is called 1 Page.
2) slab allocator has the purpose of reusing allocated memory. That is to say, the allocated memory is not released, but reused.
3) memcached selects the slab that best fits the data size based on the size of the received data. Memcached stores the list of idle chunks in slab. Select chunks based on the list and then cache the data. For example, when 112 bytes of data are generated, the most suitable bytes chunk will be selected (if the chunk size in slab classes contains 88 bytes, 144 bytes, bytes ......)
4) Slab Allocator solves the original memory fragmentation problem. However, because the allocated memory is of a specific length, the allocated memory cannot be effectively used. For example, if 100 bytes of data are cached to 128 bytes of chunk, the remaining 28 bytes will be wasted.
------------------------------------------
4. memcached deletion Mechanism
1) memcached is a cache, so the data will not be permanently stored on the server. In fact, the data will not actually disappear from memcached. In fact, memcached does not release allocated memory. After the record times out, the client will no longer be able to see the record, and its storage space can be reused.
2) Lazy Expiration: memcached does not monitor whether the record has expired, but checks the timestamp of the record during get to check whether the record has expired. This technology is called lazy (inert) expiration. Therefore, memcached does not consume CPU time on expired monitoring.
3) memcached will give priority to the space of records that have timed out, but even so, there will be insufficient space to append new records. In this case, the name Least Recently Used (LRU) will be Used) to allocate space. As the name suggests, this is a mechanism to delete records that are "least recently used. Therefore, when memcached has insufficient memory space (when the new space cannot be obtained from the slab class), it will be searched from the records that have not been used recently, and allocate the space to the new record.
------------------------------------------
5. Distributed memcached
1) memcached, but the server does not have the "distributed" function. Distributed is fully implemented by the client library. This type of distribution is the biggest feature of memcached.
2) When set (stores data to memcached), after 'key' ('key', data) is passed to the client library, the algorithm implemented by the client determines the memcached server that stores data based on the "key. After the server is selected, it is run to save ('key', data );
3) get ('key') when get (get data from memcached), the client passes 'key' to the function library, and the function library uses the same algorithm as the data storage, select the server according to the "key. If the algorithm used is the same, you can select the same server as the storage server and then send the get command. As long as the data is not deleted for some reason, you can get the saved value.
4) The memcached distributed architecture is implemented by storing different keys on different servers. When the number of memcached servers increases, keys will be dispersed. Even if a memcached server fails to connect, other caches will not be affected, and the system will continue to run.
5) Cache: The Memcached distributed algorithm is simply to say, "distributed by the remainder of the number of servers ". Calculate the integer Hash Value of the key [use the crc32 function, such as crc32 ($ key)], divide it by the number of servers, and select the server based on the remaining number. The remainder calculation method is simple and data dispersion is excellent, but it also has its disadvantages. That is, when a server is added or removed, the cost of cache reorganization is huge.
Reference: http://tech.idv2.com/2008/07/10/memcached-001/
Practice
1. Install memcached
1) memcached is libevent-based event processing. Therefore, install the libevent library before installing memcached.
Yum-y install libevent-devel
2) memcached compilation and installation is relatively simple. Generally, the compilation and installation methods are as follows:
Wget http://memcached.googlecode.com/files/memcached-1.4.5.tar.gz
Tar-zxvf memcached-1.4.5.tar.gz
Cd memcached-1.4.5
./Configure
Make & make install
------------------------------------------
2. Start memcached
1) memcached common parameters: (/usr/local/bin/memcached-h)
-P <num> TCP port number to listen on (default: 11211)
-U <num> UDP port number to listen on (default: 11211, 0 is off)
-L <ip_addr> interface to listen on (default: INADDR_ANY, all addresses)
-D run as a daemon
-U <username> assume identity of <username> (only when run as root)
-M <num> max memory to use for items in megabytes (default: 64 MB)
-C <num> max simultaneous connections (default: 1024)
-P <file> save PID in <file>, only used with-d option
-F <factor> chunk size growth factor (default: 1.25)
2) Start memcached in daemon mode
/Usr/local/bin/memcached-u nobody-p 11211-m 64-c 128-d
------------------------------------------
3. memcached combined with mysql and perl practices
Cat memcached_mysql.pl
#! /Usr/bin/perl
# Usage: test memcahed with mysql and perl
# Made by yunhaozou@gmail.com, 2010/12/20
#
Use Cache: Memcached;
Use DBI;
Use Data: Dumper;
Use Digest: MD5 qw (md5_hex );
Use strict;
#
My $ host = "127.0.0.1"; # db related information
My $ port = 3306 ″;
My $ db = "zichan_db ";
My $ user = "root ";
My $ password = "";
My $ dsn = "dbi: mysql: database = $ db: hostname = $ host: port = $ port ";
My $ dbh = DBI-> connect ($ dsn, $ user, $ password) or die "Couldn't connect to database:". DBI-> errstr; # connect to db
My $ memcached = Cache: Memcached-> new ({servers => ["127.0.0.1: 11211"], compress_threshold => 10_000}); # connect to memcached
# $ Memcached = Cache: Memcached-> new ({servers => ["127.0.0.1: 11211", "192.168.2.1: 11211"], compress_threshold => 10_000 }); # indicates memcached connected to two machines, which can achieve load balancing. One machine is suspended, and the other can continue to be used.
My $ zcsql = "select sn, model, cab_no, func, intra_ip, man_addr, contact from equipment where sn = '0917qar012 ′;";
#
My $ result = query_result ($ zcsql );
Print Dumper $ result;
$ Dbh-> disconnect ();
#
Sub query_result {
My $ query_ SQL = shift;
My $ query_key = md5_hex ($ query_ SQL); # convert the query SQL statement to the key of the MD5 Value
My $ que

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.