Brian Moon brianm at
Thu May 24 02:37:45 UTC 2007

Ian wrote:
> memcache_pconnect which connects to the same IP, which is our first PHP 
> server. This is the same seever which is connected to by every server. 
> Then we have memcache_add_server for all of the other 19 php servers we 
> have.
> So for example:
>         $this->memcache_id = memcache_pconnect('', 11211);
>         memcache_add_server($this->memcache_id, '', 11212, false);         
>         memcache_add_server($this->memcache_id, '', 11211, false);

The way you wrote this, it sounds like you think you are pointing 
memcached servers to PHP servers with this code, but its the other way 
around.  Maybe I just read what you wrote wrong.  Here is how our code 

$MEMCACHE = new Memcache();
$MEMCACHE->addServer ("");
$MEMCACHE->addServer ("");
$MEMCACHE->addServer ("");
$MEMCACHE->addServer ("");
$MEMCACHE->addServer ("");
$MEMCACHE->addServer ("");

No connect call is needed.  You just addServer all your servers.  And, 
if you are just starting out with memcache, I suggest using the OO 
interface for it rather than the procedural.  There is less typing for 
basically the same result.  It looks like you are OOP already this code 
mentions $this.

> With that set up on all of the servers, memcached works like it should 
> most of the time. But theres the ocasional problems where data we 
> deleted from the cache still appears, even hours after it was deleted.

When you say deleted, do you mean expired or that you have actually 
called the delete method.  I think in some earlier versions of memcached 
there was an issue with doing a delete and an immediate read.  The cache 
would still be there.  It was not an atomic operation.  I think this is 
fixed in 1.2.

> If we set it to persinstant connections, the whole thing stops working. 
> They don't fail, the processes stay alive, but memcached stops adding to 
> getting the keys.

We use persistent connections and have not had any trouble.  It all 
works just fine.

> And if we make any edits to the connection information, the whole thing 
> acts crazy and I usually have to kill every process on every server, 
> restart apache on every server, start every memcached process again and 
> then add the memcached connect info at the top of the scripts again.

Yes, if you change the order of the servers, the whole cache may be 
rewritten.  See the next question.

> What I don't understand is, how does the API know which server to find 
> the key on?

It uses a hash of the key to pick a server from the list.  So, if the 
list changes, the server chosen for the job will change.  That is why 
things go bad when you make changes to your list.  If you are relying on 
memcached to make your site usable, that can be an issue.

> And why does it act so strange when persistant connections are enabled? 
> Is it because theres 30 servers?

No, assuming you are using a 2.6 kernel that supports libevent and 
epoll, it should fly.  Now, if you don't have libevent and epoll, things 
could get bad.  But, they should be bad for normal connections as well.


Brian Moon
Senior Developer
It's good to be cheap =)

More information about the memcached mailing list