Installing Memcached on Fedora 8

A tutorial containing detailed instructions on setting up the Memcached server on Fedora 8. The steps included are also applicable for other Linux systems.

Looking for a new simple way to plan your day? Get organized with Five.Today

Introduction to caching with Memcached

According to their website:

"Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering."

Memcached is heavily used in medium and large scale web applications, to improve performance by removing unnecessary calls to the database when possible. The basic approach is as follows:

  1. Check the cache (Memcached) for the data we need.
  2. If the data is in the cache, use it.
  3. If the data is not in the cache, retrieve it from the database, store it in the cache for the next time, and use it.

In most cases when data that is frequently accessed is already in the cache, step three will not be required therefore overall performance is improved, as reading from the cache (Memcached in RAM) will always be quicker than reading from the database (MySQL, Postgres, Oracle etc.).

The following sections describe the installation and testing of Memcached on Fedora 8, but are also applicable for many other Linux variants as we will be building the application from source.

Building Memcached from source

You can begin by downloading the version you require from the Memcached home page. In my case I needed 1.2.5 for compatibility reasons with existing applications, however you should use the latest stable version for new green-field projects.

If like me you need an older version, you can download them from here:

Now cd to where you downloaded the .taz.gz file to and extract it:

[jcollins@redhat ~]$ cd ~/Downloads/memcached
[jcollins@redhat memcached]$ tar -zxvf memcached-1.2.5.tar.gz

Now cd into the extracted directory and make and then install (as the root user) Memcached:

[jcollins@redhat memcached]$ cd memcached-1.2.5
[jcollins@redhat memcached-1.2.5]$ ./configure
[jcollins@redhat memcached-1.2.5]$ make
[jcollins@redhat memcached-1.2.5]$ su
[root@redhat memcached-1.2.5]# make install
[root@redhat memcached-1.2.5]# exit

Now it is time to start Memcached. Here I am running the service under my own user account (jcollins), giving it 256 MB of RAM, on port 11211 on the local IP address:

[jcollins@redhat memcached-1.2.5]$ memcached -d -m 256 -u jcollins -l -p 11211

Is it running?

The first this to do is confirm that it is actually running in memory using the ps command:

[jcollins@redhat memcached-1.2.5]$ ps -ef | grep memcached
jcollins 11950 1  0 11:29 ? 00:00:00 memcached -d -m 256 -u jcollins -l -p 11211

If the service is listed as above, then we know that it started up and is running. But what about confirming that it is actually working as a cache? In order to actually use the cache like a normal consumer, I am going to use a simple command line Ruby script that connection to our running Memcached, stores a value there, then reads the value back, which is the basic use case for Memcached.

Test Script

The script makes use of the Ruby Memcache gem, which is available:

You can install this as root (note here I am using JRuby which is my preference, this command would look different in regular Ruby):

[root@redhat memcached-1.2.5]# jruby --command gem install Ruby-MemCache

Now exit back to your usual account, and create this script:

require 'rubygems'
require 'memcache'
cache = MemCache::new('', 
    :debug => false, 
    :c_threshold => 100_000, 
    :compression => false, 
    :namespace => 'test')
# this value will only live for 1 second in the cache
cache.set('author', 'John Collins', 1)
puts 'First time from the cache: '+cache.get('author').to_s
# now if we wait 2 seconds and try again, the value will have expired from the cache
puts 'Second time from the cache: '+cache.get('author').to_s

And when you run the script it will attempt to connect to your new Memcached instance and store the string value "John Collins" there using the unique key "author", with an expiry time of one second. The script will then read the value from the cache within one second which will succeed, and will then attempt to read the same value after two seconds which will fail because the value will have expired from the cache. Here is what the output of running the script looks like:

[jcollins@redhat memcached]$ jruby TestMemcache.rb
First time from the cache: John Collins
Second time from the cache:

If you are having issues getting this script to work against your local Memcached, try setting the debug flag to true in the constructor on the MemCache object to get a lot more output when running the script:

cache = MemCache::new('', 
    :debug => true, 
    :c_threshold => 100_000, 
    :compression => false, 
    :namespace => 'test')
Looking for a modern PHP framework to build your new project? Please consider using the Alpha Framework

John Collins

I have been writing about web technology and software development since 2001. I am the developer of the Alpha Framework for PHP, and the Five.Today personal productivity app. I love open source, technology, and economics.