Extension CacheMemcache

This extension implements memcached into Cyclone3 as extension. Cyclone3 can use this extension to improve higher availability to use it as cache system intead of a150 cache tables. Look into source code documentation.

What is memcached?

memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.

Danga Interactive developed memcached to enhance the speed of LiveJournal.com, a site which was already doing 20 million+ dynamic page views per day for 1 million users with a bunch of webservers and a bunch of database servers. memcached dropped the database load to almost nothing, yielding faster page load times for users, better resource utilization, and faster access to the databases on a memcache miss.

Synopsis

use Ext::CacheMemcache::_init;

if ($TOM::CACHE_memcached && Ext::CacheMemcache::check())
{
  # memcached is enabled and running
  $cache=$Ext::CacheMemcache::cache->get(
    'namespace' => "MyCache",
    'key' => "variable name"
  );
}

Learn more.

Shouldn't the database do this?

Regardless of what database you use (MS-SQL, Oracle, Postgres, MysQL-InnoDB, etc..), there's a lot of overhead in implementing ACID properties in a RDBMS, especially when disks are involved, which means queries are going to block. For databases that aren't ACID-compliant (like MySQL-MyISAM), that overhead doesn't exist, but reading threads block on the writing threads.

memcached never blocks. See the "Is memcached fast?" question below.

What about shared memory?

The first thing people generally do is cache things within their web processes. But this means your cache is duplicated multiple times, once for each mod_perl/PHP/etc thread. This is a waste of memory and you'll get low cache hit rates. If you're using a multi-threaded language or a shared memory API (IPC::Shareable, etc), you can have a global cache for all threads, but it's per-machine. It doesn't scale to multiple machines. Once you have 20 webservers, those 20 independent caches start to look just as silly as when you had 20 threads with their own caches on a single box. (plus, shared memory is typically laden with limitations)

The memcached server and clients work together to implement one global cache across as many machines as you have. In fact, it's recommended you run both web nodes (which are typically memory-lite and CPU-hungry) and memcached processes (which are memory-hungry and CPU-lite) on the same machines. This way you'll save network ports.

What about MySQL 4.x query caching?

MySQL query caching is less than ideal, for a number of reasons:
  • MySQL's query cache destroys the entire cache for a given table whenever that table is changed. On a high-traffic site with updates happening many times per second, this makes the the cache practically worthless. In fact, it's often harmful to have it on, since there's a overhead to maintain the cache.

  • On 32-bit architectures, the entire server (including the query cache) is limited to a 4 GB virtual address space. memcached lets you run as many processes as you want, so you have no limit on memory cache size.

  • MySQL has a query cache, not an object cache. If your objects require extra expensive construction after the data retrieval step, MySQL's query cache can't help you there.

If the data you need to cache is small and you do infrequent updates, MySQL's query caching should work for you. If not, use memcached.

Is memcached fast?

Very fast. It uses libevent to scale to any number of open connections (using epoll on Linux, if available at runtime), uses non-blocking network I/O, refcounts internal objects (so objects can be in multiple states to multiple clients), and uses its own slab allocator and hash table so virtual memory never gets externally fragmented and allocations are guaranteed O(1).

See Also

eCacheMemcache source code documentation.

Classical implementation of cache by a150.

User Comments


AddThis Social Bookmark Button RSS

This content is generated from file 'http://svn.cyclone3.org/trunk/frame/_addons/Ext/CacheMemcache/_desc.docbook'

With Cyclone3 can everyone easily build custom rich internet applications based on XUL and AJAX technology