5

I'm trying to force PHP's Memcache extension to timeout almost immediately if a memcached server I'm connecting to isn't available (for whatever reason). I'd like to throw an exception in this case (which will be handled somewhere else).

I've been searching and trying different things without any luck. I'm adding servers (only one for now) to the pool with the standard:

$this->memcache->addServer ( $server['host'], $server['port'] );

I then killed the memcached deamon (also tried with a wrong port&host) and opened my page. It just loads for a very long time and then nginx comes back with a 504 Gateway Time-out error.

How can I tell the memcache client to try for, I don't know, 1 second and then give up, at which point I should be able to detect the timeout somehow.

The bottom line is that if our memcached server would be down I'd like to display a user-friendly error page (already working for uncaught exceptions) as soon as possible and not make the user wait for 30 sec before he sees a generic server error.

0

2 Answers 2

5
+100

Just call:

Also, this question is pretty much identical to yours.

3

Reduce the the value of max_failover_attempts memcache module configuration parameter, default number is too high.

You can also specify timeout as 3rd parameter to connect() method:

$memcache->connect('memcache_host', 11211, $timeout);

however the default timeout should be already set to 1 second.

Another place to look are TCP timeout parameters in OS.

3
  • 1
    I've set it to 1 and the result is the same. Commented Apr 25, 2011 at 16:35
  • Jan: memcache connect and pconnect functions accept timeout as 3rd parameter. You can either try setting that or changing tcp timeout parameters in the OS.
    – vls
    Commented Apr 25, 2011 at 16:53
  • I tried with the timeout, but like you said it's already set to 1. And MySQL timeouts work just fine. Does it make any difference that memcache is running locally, and MySQL on another server? Commented Apr 26, 2011 at 7:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.