Storing Data on Distributed RAM with MemCached

Credit: Ben Bleything with Michael Granger


You need a lightweight, persistent storage space, and you have systems on your network that have unused RAM.


memcached provides a distributed in-memory cache. When used with a Ruby client library, it can be used to store almost any Ruby object. See the Discussion section below for more information, and details of where to get memcached.

In this example, well use Michael Grangers Ruby-MemCache library, available as the Ruby-MemCache gem.

Assume you have a memcached server running on the machine at IP address You can use the memcache gem to access the cache as though it were a local hash. This Ruby code will store a string in the remote cache:

	require memcache

	MC =

	MC[:test] = This string lives in memcached!

The string has been placed in your memcached with the key :test. You can fetch it from a different Ruby session:

	require memcache

	MC =

	MC[:test] # => "This string lives in memcached!"

You can also place more complex objects in memcached. In fact, any object that can be serialized with Marshal.dump can be placed in memcached. Here we store and retrieve a hash:

	hash = {
	 :roses => are red,
	 :violets => are blue

	MC[:my_hash] = hash
	MC[:my_hash][:roses] # => "are red"


memcached was originally designed to alleviate pressure on the database servers for For more information about how memcached can be used for this kind of purpose, see Recipe 16.17.

memcached provides a lightweight, distributed cache space where the cache is held in RAM. This makes the cache extremely fast, and it never blocks on disk I/O. When effectively deployed, memcached can significantly reduce the load on your database servers by farming out storage to unused RAM on other machines.

To start using memcached, youll need to download the server (see below). You can install it from source, or get it via most *nix packaging systems.

Next, find some machines on your network that have extra RAM. Install memcached on them, then start the daemon with this command:

	$ memcached -d -m 1024

This starts up a memcached instance with a 1024-megabyte memory cache (you can, of course, vary the cache size as appropriate for your hardware). If you run this command on the machine with IP address, you can then access it from other machines on your local network, as in the examples above.

memcached also supports more advanced functions, such as conditional sets and expiration times. You can also combine multiple machines into a single virtual cache. For more information about these possibilities, refer to the memcached documentation and to the documentation for the Ruby library that you e using.

See Also

  • Recipe 13.2, "Serializing Data with Marshal"
  • Recipe 16.7, "Using a WSDL File to Make SOAP Calls Easier"
  • The memcached homepage, located at, contains further information about memcached, documentation, and links to client libraries for other languages; there is also a mailing list at
  • The Ruby-MemCache homepage is at; if you install Ruby-MemCache from source, youll also need to install IO::Reactor (
  • The Robot Co-op has released their own memcached library, memcache-client, available at or via the memcache-client gem; it is reported to be API-compatible with Ruby-MemCache



Date and Time



Files and Directories

Code Blocks and Iteration

Objects and Classes8

Modules and Namespaces

Reflection and Metaprogramming


Graphics and Other File Formats

Databases and Persistence

Internet Services

Web Development Ruby on Rails

Web Services and Distributed Programming

Testing, Debugging, Optimizing, and Documenting

Packaging and Distributing Software

Automating Tasks with Rake

Multitasking and Multithreading

User Interface

Extending Ruby with Other Languages

System Administration

Ruby Cookbook
Ruby Cookbook (Cookbooks (OReilly))
ISBN: 0596523696
EAN: 2147483647
Year: N/A
Pages: 399 © 2008-2020.
If you may any questions please contact us: