1, what is Memcached1.1, memcached overview
Memcached is a free, open source, high performance, distributed memory object caching system that accelerates dynamic Web applications by reducing database load
1.2. memcached and database interaction process
(1), check the data requested by the client is present in the memcached, if present, directly return the requested data, no longer any operation on the data.
(2), if the requested data is no longer memcache, go to query the database, the data obtained from the database to the client, while the data cache a copy into the Memcahe
(3), update the data in Memcache every time the database is updated (if the data of the database is updated or deleted), and ensure that the data in the data database in the memcache is consistent.
(4), when the allocated memcache memory space is exhausted, use LRU (Least recetnly used least recently used
) Policy plus expiration policy, the invalidated data is replaced first, and then the most recently used data is replaced.
1.3. Memcached and database interaction flowchart 2, the characteristics of memcahed 2.1, memached characteristics are as follows:
Memcached as a high-performance cache server with the following characteristics:
(1), Simple protocol
The Memcahed protocol implementation is simple, using a text-based protocol that can access data directly on the server via Telnet.
(2), libevent-based processing
Libevent enables a set of libraries developed using C, which encapsulates the kqueue of the BSD system's Epoll and other event processing functions as an interface to ensure good performance even if the number of links on the server is increased. Memcached uses this library for asynchronous event processing.
(3), built-in memory management method
When the memory but finally the data space is stained, using the LRU algorithm to automatically delete unused cache, that is, the reuse of outdated data memory space, memcahed for the cache system design. Without taking into account the data disaster recovery problem, and the machine's memory, restart the machine data will be lost
(4) There is a distribution characteristic between memcached and non-communication.
Each memcahed server does not communicate with each other, is independent of the access data, does not share any information, through the design of the client, let memcahed have distributed, can support massive cache of a large-scale application.
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. This article will also describe the distribution of memcached.
2.2, memached principle:
Memcahed is a C/S mode, on the server side to start the service daemon, at this time can specify the IP address of the listener, first memcached is run in the daemon mode in one or more servers, at any time to accept the client connection operation, the client can be written in various languages, The currently known client API includes perl/php/python/ruby/java/c#/c and so on. After the client has established a connection to the Memcached service, the next thing is to access the object, each object that is accessed has a unique identifier key, the access operation is through this key, the object saved to memcached is actually placed in memory, not saved in the Cache file, which is why memcached can be so efficient and fast. Note that these objects are not persistent and the data inside is lost after the service is stopped.
Like many cache tools, the Memcached principle is not complicated. It uses the C/S mode, on the server side to start the service process, at startup can specify the listening IP, its own port number, the size of the memory used and several key parameters. Once started, the service is always in a usable state. The current version of Memcached is implemented via C, using a single-process, single-threaded, asynchronous I/O, event-based (event_based) service approach. Use Libevent as an event notification implementation. Multiple servers can work together, but there is no communication connection between these servers, and each server simply manages its own data. Client side by specifying the IP address of the server side (through the domain name should also be possible). The object or data that needs to be cached is saved on the server side in the form of a key->value pair. The value of key is converted by hash, and value is passed to the corresponding specific Server according to the hash value. When object data needs to be obtained, it is also based on key. The key is hashed first, and the obtained value determines which server it is stored on, and then makes a request to the server. The client only needs to know which server the value of the hash (key) will be stored on.
In fact, Memcache's job is to maintain a huge hash table in the memory of a dedicated machine to store some of the arrays and files that are often read and written, thus greatly improving the efficiency of the site.
3, installation of memcahed 3.1, installation libevnet
Memcache used the libevent this library for socket processing, so you also need to install libevent. (If your system already has libevent installed, you can not install
#wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz
# Ls/usr/lib | grep libevent
# mkdir/opt/tmp && cd/opt/tmp
# mv/libevent-2.0.21-stable.tar.gz/opt/tmp
# TAR-XVF Libevent-2.0.21-stable.tar.gz
# CD Libevent-2.0.21-stable
#./configure--prefix=/usr && make && make install
3.2, Installation memcached
# wget http://www.danga.com/memcached/dist/memcached-1.4.0.tar.gz
# TAR-XVF memcached-1.4.0.tar.gz && CD memcached-1.4.0
#./configure--with-libevent=/usr && make && make install
# ls-al/usr/local/bin/memcached detects if the installation was successful
3.2. Start memcached
/usr/local/bin/memcached-d-M 10-u root-l 127.0.0.1-p 12000-c 512-p/tmp/memcached.pid
The-D option is to start a daemon,
-M is the amount of memory allocated to Memcache, in megabytes, I am 10MB, normally 1024 or according to the business to divide
-U is the user running memcache, I am root here,
-L is the server IP address of the listener, if there are multiple addresses, I specify the server IP address 192.168.0.200,
-P is the port that sets Memcache listening, I set here 12000, preferably more than 1024 ports,
The-c option is the maximum number of concurrent connections to run, the default is 1024, I set the 256 here, according to the load of your server to set,
-P is set to save memcache PID file, I am here to save in/tmp/memcached.pid
4, memcahed Test 4.1, common error
This indicates that no files were found: Libevent-1.2.so.1 the solution is as follows:
/usr/local/bin/memcached:error while loading shared libraries:libevent-2.0.so.5:cannot open shared object File:no such File or directory
# ln-s/usr/lib/libevent-2.0.so.5/usr/lib64/libevent-2.0.so.5
4.2. Basic test
# telete 127.0.0.1 12000
# telnet 127.0.0.1 12000
Set Test 0 0 3 to test Store Data
123 the key entered as Test Deposit Data
STORED return set result
Get Test Get Data
VALUE Test 0 3
123 getting the data from key to test
incr Test 1 Data added 1
124
DECR Test 2 2 reduction in data
122
Quit
4.3. Parameter description
>statuspid ProcessIDof this server processes (process ID of the memcache server)Uptimenumber of seconds This server has been running (seconds the server has been running) TimeCurrent UNIX Timeaccording to the server (server current UNIX time) version versionstringof this server (Memcache version) Pointer_size current system pointer pointer size (32-bit system is generally 32bit) Rusage_user Accum ulated User Time forthis process, which accumulates user time (seconds: subtlety)) Rusage_system accumulated system Time for This process, which accumulates system time (seconds: subtlety) curr_items current number of items stored by the server (the amount of content currently stored by the server) Total_items Total number of items stored by the this server ever since it started (amount of content that has been stored since the server started) bytes Current # o F bytes used by the this server to store items (the number of bytes that the server currently stores content) curr_connections number of open connections (currently open connections) Tota L_connections total number of connections opened since the server started running (all connections received since the server was running) connection_structures Nu Mber of connection structures allocated by the server (number of connection structures allocated by the server) Cmd_get cumulative numbers of retrieval re Quests (Get command total requests) Cmd_set cumulative number of storage requests (set command (SAVE) total requests) Get_hits Numbe R of keys that has been requested and found present (total number of successful requests) get_misses numbers of items that has been requeste D and not found (the total number of failed requests) threads current number of thread (currently thread count) bytes_read all numbers of bytes re Ad by this server from nEtwork (the total number of bytes that the server reads from the network) bytes_written all numbers of bytes sent by the This server to network (the total number of bytes sent by the server to the net) Limit_maxby TES number of bytes This server was allowed to use forstorage. (The total number of bytes that the server is allowed to use when it is stored) evictions numbers of valid items removed from the cache to FreeMemory fornew Items (the number of items to be deleted in order to get free memory (the space allocated to memcache is filled with the need to delete the old items to get the space assigned to the new items), where the most attention is given to the following parameters:Uptime: Is the number of seconds that the memcached is running. Cmd_get: Is the number of query caches. Cmd_get/UptimeThe result is the average number of requests per second of cache-the larger the result value, the higher the utilization of the memcached, the greater the site's access, and, if too low, the file system cache, which simply does not reflect the powerful performance of using memcached. Cmd_set: Is the setting key=the number of value. The whole memcached is a big hash, with Cmd_get not found content, will call Cmd_set write into the cache. Get_hits: Is the number of cache hits. The so-called hit rate= Get_hits/cmd_get * -%. Get_misses: Is the number of cache misses. Get_misses plus get_hits equals Cmd_get. Stats: Display server information, statistics, etc. stats reset: emptying statistics stats slabs: Displays information about each slab, including chunk's size, number, usage, and more stats items: Displays the number of item in each slab and the length of storage (The number of seconds from the last access time) quit: Quit
4.4. Restart
# Kill ' Cat/tmp/memcached.pid '
5. Memcahed Distributed Deployment Concept 5.1, program
Usually a smaller application can meet the needs of a memcached server, but large and medium-sized projects may require multiple memcached servers, which involves a distributed deployment problem.
For multiple memcached servers, how do you determine which server a data should be saved to? There are two schemes, one is the normal hash distribution, the other is the consistent hash distribution. Detailed instructions below.
Just like the amoeba in db. The hash algorithm is used to take modulus% 2
Distribution to Server1
Server2
<?PHP/** * Normal hash distribution*///hash FunctionfunctionMhash ($key){ $MD 5=substr(MD5($key), 0, 8); $seed= 31; $hash= 0; for($i= 0;$i< 8;$i++){ $hash=$hash*$seed+Ord($MD 5{$i}); $i++; } return $hash& 0x7FFFFFFF;}//Suppose there are 2 memcached servers$servers=Array( Array(' host ' = ' 192.168.1.1 ', ' port ' = 11211),Array(' host ' = ' 192.168.1.1 ', ' port ' = 11211));$key= ' MyBlog ';$value= ' http://blog.phpha.com ';$SC=$servers[Mhash ($key)% 2];$memcached=NewMemcached ($SC);$memcached->set ($key,$value);?>
Introduction and installation of memcached cache system