Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Timeout faster \w memcache

Tags:

php

memcached

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.

like image 986
Jan Hančič Avatar asked Apr 25 '11 15:04

Jan Hančič


2 Answers

Just call:

  • Memcache::getServerStatus() or
  • Memcache::getExtendedStats()

Also, this question is pretty much identical to yours.

like image 161
Alix Axel Avatar answered Oct 18 '22 14:10

Alix Axel


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.

like image 28
vls Avatar answered Oct 18 '22 14:10

vls