332

It seems that PHP has two memcached libraries named memcache and memcached. What is the difference and how do you know which one to use? Is one outdated? It seems that memcached offers more methods so I would assume that means it has had the most development - but it also seems to require external C/C++ libraries so I'm not sure if I can install it.

It seems that memcache has been around longer, does not require additional libraries, and has pre-compiled binaries for even windows! I would think that it would be the better choice for now. However, being new to memcached (server) I'm not sure if there is some super important features in memcached (php) that make it worth the extra trouble.

Shog9
  • 156,901
  • 35
  • 231
  • 235
Xeoncross
  • 55,620
  • 80
  • 262
  • 364
  • 1
    ZendCon have a nice episode comparing the both. [ZendCon Sessions Episode 040: Memcached: the better Memcache interface](http://devzone.zend.com/article/13104) – John Magnolia Jun 12 '11 at 15:50
  • duplicate of http://serverfault.com/questions/63383/memcache-vs-memcached by just a few days, but this got more views and votes :) – Stefano Dec 05 '12 at 18:04

4 Answers4

255

Memcached client library was just recently released as stable. It is being used by digg ( was developed for digg by Andrei Zmievski, now no longer with digg) and implements much more of the memcached protocol than the older memcache client. The most important features that memcached has are:

  1. Cas tokens. This made my life much easier and is an easy preventive system for stale data. Whenever you pull something from the cache, you can receive with it a cas token (a double number). You can than use that token to save your updated object. If no one else updated the value while your thread was running, the swap will succeed. Otherwise a newer cas token was created and you are forced to reload the data and save it again with the new token.
  2. Read through callbacks are the best thing since sliced bread. It has simplified much of my code.
  3. getDelayed() is a nice feature that can reduce the time your script has to wait for the results to come back from the server.
  4. While the memcached server is supposed to be very stable, it is not the fastest. You can use binary protocol instead of ASCII with the newer client.
  5. Whenever you save complex data into memcached the client used to always do serialization of the value (which is slow), but now with memcached client you have the option of using igbinary. So far I haven't had the chance to test how much of a performance gain this can be.

All of this points were enough for me to switch to the newest client, and can tell you that it works like a charm. There is that external dependency on the libmemcached library, but have managed to install it nonetheless on Ubuntu and Mac OSX, so no problems there so far.

If you decide to update to the newer library, I suggest you update to the latest server version as well as it has some nice features as well. You will need to install libevent for it to compile, but on Ubuntu it wasn't much trouble.

I haven't seen any frameworks pick up the new memcached client thus far (although I don't keep track of them), but I presume Zend will get on board shortly.

UPDATE

Zend Framework 2 has an adapter for Memcached which can be found here

Wilt
  • 41,477
  • 12
  • 152
  • 203
Miha Hribar
  • 5,776
  • 3
  • 26
  • 24
  • 1
    Excellent answer; also, the external dependency on libmemcached is actually a plus, in this case, as it's one of the more actively-developed clients. – Marc Bollinger Sep 18 '09 at 05:43
  • Agreed. The libmemcached external dependency should not be considered a problem whatsoever, same as libevent. – Miha Hribar Sep 18 '09 at 05:46
  • 1
    That Cas Tokens thing sounds like the biggest plus. Actually, from looking over these features it almost seems like memcached is heading more to a managed data holder, like a DB, and less like a temp cache. – Xeoncross Sep 18 '09 at 15:47
  • If you are looking for that you already have memcachedb (http://memcachedb.org/) which is actually just a Berkley DB that speaks the memcached protocol. – Miha Hribar Sep 18 '09 at 17:22
  • @Miha Hribar, that is unless you want to compile under Windows, on which libmemcached has no support for (no, the support that it claims to have is not anywhere close to be able to compile it for win32) – rustyx Mar 08 '13 at 21:33
  • 8
    this answer doesnt even tell the difference between memcache and memcached ?! is memcache just the older version of memcached? – Daniel W. Jun 10 '13 at 13:34
  • CakePHP will have support for it in v2.5 – Calin Aug 27 '13 at 08:42
  • @DanFromGermany Why not? It clearly says _where memcached came from and why_, what _features_ it has in addition to the _memcache_ ones, describes the _new dependencies_ and as a summary **gives a recommendation** to use it. – Lukas Sep 15 '13 at 03:27
  • Zend Framework 2 has an adapter for Memcached which can be found [here](http://framework.zend.com/manual/current/en/modules/zend.cache.storage.adapter.html#the-memcached-adapter) – Wilt Aug 11 '15 at 09:16
10

When using Windows, the comparison is cut short: memcache appears to be the only client available.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
rymo
  • 3,285
  • 2
  • 36
  • 40
8

Memcached is a newer API, it also provides memcached as a session provider which could be great if you have a farm of server.

After the version is still really low 0.2 but I have used both and I didn't encounter major problem, so I would go to memcached since it's new.

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
RageZ
  • 26,800
  • 12
  • 67
  • 76
  • 1
    Both actually work as session handlers even though I would never want to use them for that. http://us3.php.net/manual/en/memcache.examples-overview.php – Xeoncross Sep 18 '09 at 03:26
  • good catch my bad, yeah it's only really useful on big server when io become to be really important, plus memcache doesn't validate that it won't drop some object to get memory so difficult to know how to scale things. – RageZ Sep 18 '09 at 03:28
  • 2
    The 0.2 version has since been surpassed by a stable 1.0.0 version. – Miha Hribar Sep 18 '09 at 05:44
  • Currently at version 1.6.17. – Melroy van den Berg Oct 09 '22 at 22:19
6

This is 2013. Forget about the 2009 comments. Likewise, if you are running serious traffic loads, do not even contemplate how to make-do with a windows based memcache. When dealing with a very large scale (500+ front end web servers) and 20+ back end database servers and replicants (mysql & mssql mix), a farm of memcached servers (12 servers in group) supports multiple high volume OLTP applications answering 25K ~ 40K mc->get calls per-second. These calls are those that do NOT have to reach a database.

IMHO, this use of memcached provided SERIOUS $$$,$$$savings on CAPEX for new DB servers & licences as well as on support contracts for large commercial designs.

Ben
  • 54,723
  • 49
  • 178
  • 224
Mike Trest
  • 269
  • 2
  • 2