PHP extension module memcached persistent connection Usage Analysis

Source: Internet
Author: User
This article mainly introduces how to use memcached persistent connection in the PHP extension module. For more information, see

This article mainly introduces how to use memcached persistent connection in the PHP extension module. For more information, see

There is a widely spread article on the Internet about the differences between memcache and memcached, the two extension modules of php ,, the difference between memcached and memcached is that the memcached module does not support persistent connections. So many years later, I thought that memcached does not support persistent connections. In fact, the memcached extension module has supported persistent connections since a very early version. We can see from the source code of the extension module:

/* {Memcached ::__ construct ([string persistent_id [, callback on_new [, string connection_str])

Creates a Memcached object, optionally using persistent memcache connection */

Static PHP_METHOD (Memcached, _ construct)

{

From the php manual, we can see that the constructor provided by the memcached extension module provides a parameter persistent_id option, which is described as follows:

By default, the Memcached instance is destroyed after the request ends. However, you can use persistent_id to specify a unique ID for each instance during creation and share the instance among requests. All instances created with the same persistent_id value share the same connection.

The meaning of this parameter is that if you pass a name id to the constructor, it will establish a persistent connection, we usually use the PHP-FPM mode, in this way, the PHP-FPM process will have a persistent connection channel with the memcached service resume. We can also understand that persistent_id is the name of a connection pool, and all php-fpm processes are members of this connection pool.

However, we need to note that php is an explanatory language. When php establishes a persistent connection through the memached module for the first time, do not use the memcached constructor to construct a persistent connection with the same persistent_id name in subsequent php Execution. You can create a persistent connection with different persistent_id names. If the same name is repeatedly executed by php, php-fpm process exceptions will inevitably lead to slower and slower communication with memcached. At the same time, php will generate coredump Based on libmemcached versions.

So how can we avoid a single php-fpm from repeatedly establishing a persistent connection after establishing a persistent connection named persistent_id? In fact, it is explained in the PHP manual with comments. The content is as follows:

When using persistent connections, it is important to not re-add servers.

This is what you do not want to do:

$ Mc = new Memcached ('mc '); $ mc-> setOption (Memcached: OPT_LIBKETAMA_COMPATIBLE, true); $ mc-> addServers (array ('mc1 .example.com ', 11211), array ('mc2 .example.com ', 11211 ),));


Every time the page is loaded those servers will be appended to the list resulting in your simultaneous open connections to the same server. the addServer/addServers functions to not check for existing references to the specified servers.

A better approach is something like:

$ Mc = new Memcached ('mc '); $ mc-> setOption (Memcached: OPT_LIBKETAMA_COMPATIBLE, true); if (! Count ($ mc-> getServerList () {$ mc-> addServers (array ('mc1 .example.com ', 11211), array ('mc2 .example.com ', 11211 ),));}

Use the getServerList () method to check whether persistent connection resources with the same name already exist in the php-fpm process container currently in use. If so, do not reuse addServers () method to add new connection configurations.

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.