Home > Article > Backend Development > php memcache and php memcached and problems encountered
PHP has two memcache clients: php memcache and php memcached.
php memcache is implemented independently in PHP. It is an old client. From our practice, we have found that there are many problems, and there are few functions and few attributes that can be set;
php memcached is an extension of libmemcached based on native C, which is more complete. , it is recommended to replace it with php memcached.
1. Problems with Php memcache
1.1 Distribution problem
php memcache will automatically switch instances by default, so sometimes old data is obtained and the value is erratic.
Questions shared by netizens:
I encountered a small problem while working on a certain product these days. The phenomenon was quite strange. The product used two distributed memcached servers. Occasionally, the number retrieved by a certain counter is incorrect, and finally it is located on the failover mechanism of the php memcache client.
We know that in the memcached distributed environment, a certain key is assigned to a certain memcached through hash calculation.
If memcache.allow_failover = 1 in php.ini, in a distributed environment, if there is a problem with one memcached, it will automatically try other memcached, and the above problem will occur. Therefore, you need to set allow_failover = 0. Then if it cannot be retrieved, it will directly return failure and will not retrieve it from other mcs. This will avoid frequent switching instances when network abnormalities or server-side abnormalities occur, and old data will be retrieved.
1.2 Stability issues under high concurrency
Lessons mentioned on Sina Weibo:
Replace php memcache with php memcached, which greatly improves stability under high concurrency;
In addition, it has more functions and more accurate error codes.
Twitter’s caching experience
Multi-level Cache, which reduces the impact of some cache nodes being down, and caches both reading and writing;
Replace the memcached api with libmemcached (convenient for multi-language access to memcached, so that various rules such as distribution can be used) Consistent. )
1.3 The 1 second timeout interval cannot be modified
php memcache client has a 1 second timeout interval that cannot be modified:
bool Memcache::connect ( string $host [, int $port [, int $timeout ]] )
The third parameter can originally set the timeout in seconds, but it cannot be modified.
The following three methods of modifying timeout have been tested and all are ineffective:
1.3.1. Memcache api Memcache::setServerParams cannot be modified;
1.3.2. Change memcache source code vi php_memcache.h macro definition cannot be modified;
1.3.3 . This configuration in php.ini: default_socket_timeout = 60 is invalid for this timeout.
2. Comparison between memcache and memcached
Php memcache, an old client, has very few property settings;
The error code granularity is very coarse, making it difficult to locate after an error;
And it lacks some functions:
It is mainly used for PHP for two customers. One is the older, more extensive PECL/cache and the other is the new, rarely used, more feature-rich PECL/cache.
Support multiple servers such as basic, set value, get value, increment, decrement and get data.
Here are some more advanced features and information.
项目 pecl/memcache pecl/memcached First Release Date 2004-06-08 2009-01-29 (beta) Actively Developed Yes Yes External Dependency None libmemcached Automatic Key Fixup1 Yes No Append/Prepend No Yes Automatic Serialzation2 Yes Yes Binary Protocol No Optional CAS No Yes Compression Yes Yes Communication Timeout Connect Only Various Options Consistent Hashing Yes Yes Delayed Get No Yes Multi-Get Yes Yes Session Support Yes Yes Set/Get to a specific server No Yes Stores Numerics Converted to Strings Yes