PHPMemcached client memcache, memcached comparison

Source: Internet
Author: User
Author: selfimprblog: blog. csdn. netlgg201mail: lgg860911@yahoo.com.cn1. system-Level Lock support: the memcache client does not support lock-related functions, while the server supports concurrency. This will actually cause data confusion, our previous practice is to implement an application layer lock :? Php *** author:

Author: selfimip blog: http://blog.csdn.net/lgg201 mail: lgg860911@yahoo.com.cn 1. system-Level Lock support: the memcache client does not support lock-related functions, while the server supports concurrency. This will actually cause data confusion, our previous practice is to implement an application layer lock :? Php/*** author:

Author: selfimip

Blog: http://blog.csdn.net/lgg201

Mail: lgg860911@yahoo.com.cn

1. System-Level Lock support:

The memcache client does not support lock-related functions, but the server also supports concurrency. This will actually cause data confusion. Our previous practice was to implement an application layer lock:

<? Php/*** author: selfimip * blog: http://blog.csdn.net/lgg201 * mail: lgg860911@yahoo.com.cn */$ m = new Memcache (); $ m-> addServer (......); // lock function lock ($ m, $ key, $ timeout = 5000, $ wait = 15) {while ($ I ++ <= $ wait & false! = $ M-> get ('lock _'. $ key); return false ===$ m-> get ('lock _'. $ key) & ($ m-> set ('lock _'. $ key, $ timeout);} // unlock function unlock ($ m, $ key) {return $ m-> delete ('lock _'. $ key) ;}?>

The above method is used to construct the locking and unlocking methods, and then lock before get, and unlock after set.

The above solution will reduce the data confusion caused by concurrency, but cannot solve the problem, because we may be concurrent During lock and get the lock at the same time ..

Later, I considered the Optimistic Locking Mechanism Implemented by the application layer, but it was useless if it was not applied ..

Recently I changed the Memcached client and found that the method supporting cas is actually an optimistic lock at the system layer.

<? Php $ m = new Memcached (); $ m-> addServer (....); $ value = $ m-> get ($ key, $ cas); // Business Operation $ m-> cas ($ cas, $ key, $ value);?>

When get is run, a $ cas parameter is passed. Memcached: get is defined. $ cas is a reference parameter. After execution, $ cas has been changed to a unique identifier returned by the memcached server.

When we set this key, we take this cas value and set it using the cas interface. Then, the server can determine whether the value of this key has been modified based on the cas value, if it has been modified, it indicates it is dirty data and an error message is sent to the client.

Because the authentication mechanism of this lock is provided by the memcached server, we can trust its correctness.

2. Get all keys in Memcached

This is supported in the memcache extension, but not in the Memcached extension.

The method used is Memcache: getExtendedStats (...), three parameters, which will not be written here. I will add these parameters when translating the php-memcache document.

By the way, if you have time, please participate in the support: http://code.google.com/p/phpdoc-zh/, PHP official documentation translation, public welfare project

3. persistent connection

The Memcache client supports persistent connections, while the Memcached client does not support persistent connections, and the Memcached client may have bugs when releasing the connection, in the case of high concurrency, a large number of connections on the Memcached server are in the time_wait status and cannot be released .. This will cause some requests to fail ..

We use memcache as the session handler. Because of this problem, our session cannot be obtained, and the client loses the connection, the problem is serious.

Finally, the solution found on the Internet is to modify the socket release configuration of the Memcached server. We made the following changes:

Modify the file:/etc/sysctl. cnf. Add or modify the following two configurations.

Net. ipv4.tcp _ tw_recycle = 1 // reclaim the socket in time_wait status

Net. ipv4.tcp _ fin_timeout = 3 // The timeout value.

Then/sbin/sysctl-p will reload the file

A friend on the Internet used apache AB to perform concurrent tests on this configuration modification.

1000000 requests are 30000 concurrent and no Memcached connection failure occurs.

Well, I have learned so much over the past few days. I hope I can correct the shortcomings or errors.

Thank you.

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.