Phpmemcache and phpmemcached and problems encountered

Source: Internet
Author: User
Tags php memcached
Phpmemcache and phpmemcached, as well as problems encountered, PHP has two memcache clients: php memcache and php memcached.
Php memcache is implemented independently using php. it is an old client. In practice, we have found multiple problems, with fewer functions and fewer attributes;
Php memcached is an extension of libmemcached based on native c. It is more comprehensive. we recommend that you replace it with php memcached.

1. Php memcache problems
1.1 distributed problems
By default, php memcache automatically switches the instance, so it sometimes gets old data and the value is erratic.
Questions shared by netizens:
During the past few days, I encountered a small problem when I was working on a product. The product used two distributed memcached servers. The number retrieved from a counter is occasionally incorrect, and is finally located on the failover mechanism of the php memcache client.
We know that in a memcached distributed environment, a key is allocated to a memcached through hash computing.
If memcache. allow_failover = 1 in php. ini, if a memcached has a problem in a distributed environment, it will automatically go to another memcached to try, and the above problem will occur. Therefore, if you want to set allow_failover = 0, the system will directly return a failure if it is not obtained from other mc instances, so as to avoid frequent instance switching when the network or server is abnormal, old data is obtained.

1.2 High Concurrency stability issues
Lessons learned from Sina Weibo:
Changing php memcache to php memcached greatly improves the stability of high concurrency;
In addition, there are more functions and more precise error codes.

Twitter cache experience
Multi-level Cache to reduce the impact of some cache nodes being down, and read/write operations are all cache;
Replace the memcached api with libmemcached (which facilitates multi-language access to memcached and ensures consistency among distributed rules .)

1.3 1 second timeout interval cannot be modified
The php memcache Client has a one-second timeout interval that cannot be modified:
Bool Memcache: connect (string $ host [, int $ port [, int $ timeout])
The third parameter can be set to timeout in seconds, but cannot be modified.
The following three methods to modify timeout are not valid:
1.3.1. memcache api Memcache: setServerParams cannot be modified;
1.3.2. modify the memcache source code vi php_memcache.h macro definition;
1.3.3. php. ini: default_socket_timeout = 60 is invalid for this timeout.

2. Comparison between memcache and memcached
The old Php memcache Client has very few options for setting attributes;
The error code is coarse-grained and difficult to locate after an error occurs;
And lack of functions:

PHP is mainly used for two customers. One is older, more extensive PECL/cache and other new ones, rarely used, and more abundant PECL/cache features.

You can set the value, get the value, increment, reduce, and obtain data for multiple servers, such as basic servers.

Here are some more advanced functions and information.

Project pecl/memcache pecl/memcachedFirst Release Date (beta) actively Developed Yes your Dependency None your Key Fixup1 Yes NoAppend/Prepend No YesAutomatic serialation2 Yes YesBinary Protocol No longer No YesCompression Yes YesCommunication Timeout Connect Only varous your Hashing Yes else Get No YesMulti-Get Yes YesSession support Yes YesSet/Get to a specific server No YesStores Numerics Converted to Strings Yes

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.