php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #58758 Persistent connections are opened and not reused efficiently
Submitted: 2009-07-10 20:09 UTC Modified: 2009-07-16 13:45 UTC
From: php at muckrake dot net Assigned:
Status: Not a bug Package: memcached (PECL)
PHP Version: 5.2.6 OS: Ubuntu 9.04
Private report: No CVE-ID: None
 [2009-07-10 20:09 UTC] php at muckrake dot net
Description:
------------
A simple script that sets a value and the retrieves it is included below called memcached.php.

There are no open connections to the memcached server running locally on port 11211:
$ netstat -an | grep ESTABLISHED | awk '{print $5}' | grep 11211 |wc -l
0

Add some load to the script with apachebench:
$ ab -n2000 -c10 "http://localhost/memcached.php"

There are a huge number of open connections to the memcached server:
$ netstat -an | grep ESTABLISHED | awk '{print $5}' | grep 11211 |wc -l
1003

A sample netstat line is included here:
tcp        0      0 127.0.0.1:41306         127.0.0.1:11211         ESTABLISHED

These connections do not dissipate and quickly overwhelm whatever the max connections limit is on memcached, causing the script to hang.

For example, this is the output when apachebench is run a second time immediately after the first run:

$ ab -n2000 -c10 "http://localhost/memcached.php"
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
apr_poll: The timeout specified has expired (70007)
Total of 44 requests completed

After a restart of memcached, with no other users of the memcached server and running the ab command above twice, the memcached server shows these stats:
STAT total_connections 1035
STAT connection_structures 1018

Apache is running with MPM prefork and MaxClients 150.


Reproduce code:
---------------
<?php
$key = "key_" . uniqid();
$memcached = new Memcached('persistent');
$memcached->addServer('localhost', 11211);
$memcached->set($key, 'value');
$value = $memcached->get($key);
echo $value;
// prints "value" when loaded in a browser

Expected result:
----------------
A small number of connections are established and reused.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-16 13:45 UTC] andrei@php.net
Thank you for taking the time to write to us, but this is not
a bug.

This is expected behavior, based on your script. You are accessing a persistent instance of Memcached and adding a server to the server list. However, libmemcached does not check whether the server already exists in the list, so you have multiple duplicate servers when you do that. Hence, multiple connections to memcached server from each Apache child.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 15:01:29 2024 UTC