4

I'm using php::memcache module to connect a local memcached server (@127.0.0.1), but I don't know that which one I should use, memcache::connect() or memcache::pconnect ? Does memcache::pconnect will consume many resource of the server?

Thank you very much for your answer!

cyberscorpio
  • 41
  • 1
  • 4

6 Answers6

7

Memcached uses a TCP connection (handshake is 3 extra packets, closing is usually 4 packets) and doesn't require any authentication. Therefore, the only upside to using a persistent connection is that you don't need to send those extra 7 packets and don't have to worry about having a leftover TIME-WAIT port for a few seconds.

Sadly, the downside of sacrificing those resources is far greater than the minor upsides. So I recommend not using persistent connections in memcached.

St. John Johnson
  • 6,590
  • 7
  • 35
  • 56
  • Thank you, I'll use memcache::connect() instead :) – cyberscorpio Mar 14 '09 at 05:06
  • 1
    No problem! Glad I could help. Please select an answer as the one that answered your question. – St. John Johnson Mar 14 '09 at 12:18
  • 6
    what exactly are the "downsides" that you say far outweigh the upsides? I am curious as I have been looking for a solid answer to this same question. – jW. Jul 24 '09 at 17:36
  • the only downside i know of is that if you have a farm of multiple servers using same memcached you can run into connections limit. Especially if your site has issues and you are using memcached for sessions. each request will need a connection so the more requests running concurrently the more connections you need. 1024 is the default limit. – Art79 Feb 13 '11 at 14:06
  • I think it would be a safe design consideration that each api/dynamic page load would at least need one memcache connection, no? – CasualT Apr 07 '15 at 16:06
2

pconnect stands for persistant connection. This means that the client (in your case the script) will constantly have a connection open to your server which might not be a resouces problem - more a lack of connections available.

You should probably be wanting the standard connect unless you know you need to use persistant connections.

Ross
  • 46,186
  • 39
  • 120
  • 173
  • I'm using memcache::addServer( string $host [, int $port [, bool $persistent [, int $weight [, int $timeout [, int $retry_interval [, bool $status [, callback $failure_callback]]]]]]] ) the 3rd parameter is persistent, and the default value is TRUE. but memcache::connect() doesn't use persistent.. – cyberscorpio Mar 13 '09 at 17:02
1

As far as I know, the same rules that govern persistent vs. regular connections when connecting to MySQL apply to memcached as well. The upshot is, you probably shouldn't use persistent connections in either case.

Community
  • 1
  • 1
Jeremy DeGroot
  • 4,496
  • 2
  • 20
  • 21
  • 3
    memcache connections are much lighter than mysql connections -- so keeping an idle connection open is relatively cheap. memcache connections are also stateless (right?). So at least two of the arguments against mysqlpconnect do not apply to memcached. – Frank Farmer Nov 16 '10 at 00:21
0

One downside is that PHP gets no blatant error or warning if one or all of the persistently-connected memcached daemons vanish(es). That's a pretty darn big downside.

FYA
  • 402
  • 4
  • 6
0

"Consumes" TCP port.

vartec
  • 131,205
  • 36
  • 218
  • 244
0

In application I'm developing I use pconnect as it uses connection pool and from the view of hardware - one server keeps one connection to memcache. I don't know exactly how it works but I think memcached is smart enough to track IP of memcached client machine.

I've played with memcached for a long time and found that using memcache::getStatus shows that connections count doesn't increased when using pconnect.

You can use debug page which show memcached stats and try to tweak pconnect or connect and see what's going on.