Memcached on php & Mysql

The largest problem with scalability within a typical environment is the speed with which you can access information. For fre-quently accessed information, using MySQL can be slow because each access of information requires execution of the SQL query and recovery of the information from the database. This also means that queries on tables that are locked or blocking may delay your query and reduce the speed of recovery of information.

Memcached is a simple, yet highly scalable key-based cache that stores data and objects wherever dedicated or spare RAM is available for very quick access by applications. To use, you run memcached on one or more hosts and then use the shared cache to store objects. Because each host’s RAM is storing information, the access speed is much faster than loading the information from disk. This can provide a significant performance boost in retrieving data versus loading the data natively from a database. Also, be-cause the cache is just a repository for information, you can use the cache to store any data, including complex structures that would normally require a significant amount of effort to create, but in a ready-to-use format, helping to reduce the load on your MySQL servers.


Installing Memcached using YUM


If you are using Centos or RHEL, you need to install the RPMforge rpm to install the packs. Go to and download/install the correct rpm/arch belongs to your server.


Once you have installed, try


  • # yum install memcached
  • # yum install libmemcached


This should install all the required packs for you and once you have installed you can skip this Source installation process.


Installing Memcached from source


Download the latest source from website.


Make sure you have install the libevent & libevent-devel packages before running ./configure.


Installing the libmemcached


libMemcached is an open source C/C++ client library and tools for the memcached server ( It has been designed to be light on memory usage, thread safe, and provide full access to server side methods. You can download the latest version from


  • # tar zxvf libmemcached-0.53.tar.gz
  • # cd libmemcached-0.53
  • # ./configure
  • # make && make install


Staring the memcached as daemon process


It is advisable to run/start the memcached process as non-root user, you can use the nobody user/group to start.

  • # memcached -d -u nobody -m 1048 -p 11211


This will start the memcached under nobody user on port 11211 binding with localhost( with the memory limit of 1 GB.


Install the Memcache PECL Extension for PHP


Even though memcached is happily running on the server, it’s not accessible from PHP without the PECL extension. Fortunately this is a very easy process, just use the pecl command.


  • # pecl install memcache

Then add the memcache extension to your php.ini file.



FInally restart your apache or nginx web server to load the new settings.


Checking the memcached using php and libmemcached


Download and rename the below as memcached-test.php,txt in your domain and try access it in your browser.


You should be able to get the following similar to.

string(28) “String to store in memcached” int(123) object(stdClass)#3 (1) { [“attribute”]=> string(4) “test” }


Checking using the memcp command (libmemcached)


memcat: Display the value for each ID given on the command line:


  • # memcat –servers=localhost hwkey
    Hello world
    # echo “Hello World” > hwkey
    # memcp –servers=localhost hwkey
    # memcat –servers=localhost hwkey
    Hello world


Benchmarking using memslap


Test the load on one or more memcached servers, simulating get/set and multiple client operations. For example, you can simulate the load of 100 clients performing get operations:


  • # memslap –servers=localhost –concurrency=100 –flush –test=get
    # memslap –servers=localhost –concurrency=100 –flush –test=get Threads connecting to servers 100
    Took 13.571 seconds to read data

Good Luck!