Using MemCache for Performance

A cache is a temporary collection of data where the original data is expensive to compute or read as compared to reading from the cache. In other words, a cache is a temporary storage area where frequently accessed data can be stored for rapid access.

Cache give two great advantages

  • Significantly reduces the number of retrieval requests to database
  • Cuts down the I/O access (hard disk)

Cache is widely used to improve web application performance. Following methods of caching are famous

  • Flat file caching
  • MySQL 4.x query caching
  • Caching data in the database
  • RAM disk
  • Shared memory (APC)
  • memcached

In this post will discuss about Memcache

What is Memcache

  • Distributed Memory Object Caching System
  • Danga Interactive developed for LiveJournal.com
  • Uses RAM for storage
  • Acts as a dictionary of stored data with key/value pairs
  • Caching daemon
  • Stored in memory (RAM), not on disk
  • Non-blocking network I/O (TCP/IP) used
  • Libevent in used to scale many number of open connections
  • Its own slab allocator and hash table use to ensure virtual memory never gets externally fragmented and allocations are guaranteed

How we use it?

Set up a pool of memcached servers and assign values to keys that are stored in the memcache pool. To store client hashes the key to a particular machine in the pool. Requests for that particular key retrieve the value from the memcached server on which it was stored.

Values expire after the TTL completes. A single value cannot contain more than 1MB of data and the key strings not more then 250 characters. Non-blocking server, not a database, provide redundancy, doesn’t handle fail over and does not provide authentication. Data is not replicated across the pool it works great on a small and local Area network.

Data Storing in the pool

Advantage is in scalability to fully see the advantage, use a “pool” memcached itself doesn’t know about the pool the pool is created by and managed from the

client library. When one server goes down, the system fails over to another server in the pool memcached does not provide this some memcache clients provide fail over

If you can’t find the data in memcache, then it will retrieve data from your source again and storing it back to the cache.

Protocol API used by Memcached

  1. Storage commands: set, add, replace, append, prepend, cas
  2. Retrieval command: get, gets
  3. Deletion command: delete
  4. Increment/decrement: incr, decr
  5. Other few commands: stats, flush_all, version, verbosity, quit

$> telnet mem.example.com 11211
Trying ::1...
Connected to mem.example.com.
Escape character is '^]'.
set juster 0 0 15
This is for test.
STORED
get juster
VALUE juster 0 15
This is for test.
END
quit
Connection closed by foreign host.
$>

How to Install?

Download the memcache from Dango site http://danga.com/memcached/. Install it by using following commands
$> ./configure –-prefix=/usr/local/memcached
$> make; make install
$> /usr/local/memcached/bin/memcached -d -m 2048 -p 11211
Done!

This –m 2048 means 2 GB of RAM allocating to the memcache.

Memcached Clients

Perl, Python, Ruby, Java, C# C (libmemcached)
PostgreSQL (access memcached from procs and triggers)
MySQL (adds memcache_engine storage engine)
PHP (pecl/memcache)

Here is the example to code in PHP

<?php
$memcache = new MemcachePool();
$memcache->addserver(’10.0.0.100’,11211);
$memcache->addserver(’10.0.0.101’,11212);
$memcache->addserver(’10.0.0.102’,11213);
If (($justinObject = $memcache get->(‘key’)) === false )
{
$tmpObject = new stdClass;
$tmpObject->get jus = ‘tin’;
$tmpObject->get bar = ‘quz’;
$justinUbject = $tmpObject;
$memcache->set(‘key’, $tmpObject, 0, 3);
}

Database techniques

Before mysql_query() function create wrappers that checks the cache first and returns an array of database results and store results to the cache and get them when you execute a query. For large datasets, run a scheduled query once an hour and store it to the cache memcached can store arrays, objects, etc., but it cannot store database functions (e.g. mysql_query()) return

One Response to “Using MemCache for Performance”

  1. Using MemCache for improving web application performance…

    Use of MemCache to improve web application performance. See how to install memcache and use is for good web application performance…

Leave a Reply