A long time ago, I began to study memcache and wrote a series of learning experiences, such as discuz! . The dozens of replies that follow have also made this article a popular topic in this blog.
Meanwhile, in Baidu and Google, the keyword memcache occupies the second place (the first is official) for more than a year, providing some information for many friends who need to know or use memcache, but I always felt that it was not enough, so this article was born.
I have been talking about it for a long time. If you think it is too long, please read the last section, which is the focus of this article.
In fact, the PHP-based extended memcache client has already been implemented and is very stable. First, I would like to explain some terms. memcache is an open-source project of danga.com, which can be analogous to services such as MySQL. The memcache extended by PHP is actually connected to memcache.
First, you can view the details of installing memcache:
Install memcache in Linux: http://www.ccvita.com/257.html
Install memcache in Windows: http://www.ccvita.com/258.html;
Second, the PHP extension installation, the official address is http://pecl.php.net/package/memcache
Finally, start the memcache service, such
/Usr/local/bin/memcached-D-P 11213-u root-M 10-C 1024-T 8-P/tmp/memcached. PID
/Usr/local/bin/memcached-D-P 11214-u root-M 10-C 1024-T 8-P/tmp/memcached. PID
/Usr/local/bin/memcached-D-P 11215-u root-M 10-C 1024-T 8-P/tmp/memcached. PID
Start three instances and use only 10 MB of memory for testing.
Memcache In the PECL extension of PHP has actually implemented multi-server support in version 2.0.0 and is now 2.2.5. See the following code.
$ Memcache = new memcache;
$ Memcache-> addserver ('localhost', 11213 );
$ Memcache-> addserver ('localhost', 11214 );
$ Memcache-> addserver ('localhost', 11215 );
$ Memstats = $ memcache-> getextendedstats ();
Print_r ($ memstats );
In the previous example, the distributed deployment of memcache has been implemented. Is it very simple.
Benign operation of Distributed Systems
In the actual use of memcache, the most serious problem is that when the server is increased or decreased, it will lead to a wide range of cache loss, which may lead to database performance bottlenecks, to avoid this problem, first look at the consistent hashing algorithm. For Chinese introduction, refer to here to achieve this by selecting a server algorithm change during access.
Modify the PHP memcache extension in the source code of memcache. c
"Memcache. hash_strategy" = Standard
"Memcache. hash_strategy" = consistent
Re-compile. At this time, we use the consistent hashing algorithm to find the server to access data.
Effective test data shows that the use of consistent hashing can greatly improve the situation of large cache loss when memcache is added or deleted.
Nonconsistenthash: 92% of lookups changed after adding a target to the existing 10
Non consistenthash: 90% of lookups changed after removing 1 of 10 targets
Consistenthash: 6% of lookups changed after adding a target to the existing 10
Consistenthash: 9% of lookups changed after removing 1 of 10 targets
Tag: deployment, memcache, distributed
Kimi at 21:19:00 in memcache
Copyright statement: original works. You are welcome to reprint them.HyperlinkIndicate the original address, author information, and the statement.
"Memcache distributed Deployment Solution"
I think it is best to have a stable machine on the addserver connect.
Come on, boy! It shoshould be "memcached "....KimiJune 9 th, 2009 at 09:46:33Reply
@ Guest, TKs for reminding me
- Dan Mu Qiu Feng16:51:01Reply
I have made some improvements when I look at it again.
I recently looked at the memcache source code. I don't know if I can write some source code analysis or analyze the implementation principle of memcache.
- Chen min08:39:26Reply
I searched for memcached and found your website.
The tag for seeing your daughter-in-law is big too.
Made a link
Please refer to the Technical article for more information.
Thanks for sharing. It is very powerful and learning.