Home Miscellaneous Memcache

Memcache

by SupportPRO Admin

One reason behind writing about this topic is because in todays hosting industry one of the major services used is the MySQL. Nowadays, everything solely depends on the working of databases. So all the high traffic websites have clustered their MySQL so that any kind of downtime or load related issues are not affecting the proper working of their website.

As we go through the working of a database, one thing that comes to our mind, how it will help if we have a caching system, something like a distributed memory system, which can be enabled in the server just to manage the traffic of MySQL databases. Yeah! you got it.. So do Brad Fitzpatrick who worked on this theory and made it a reality by developing such a caching system called MEMCACHED.

Memcached, is a simple, but powerful caching system that is mainly be used to speed up the loading of dynamic webpages by controlling the load of MySQL. Its simple design and user friendly nature makes it a paradise for the end users.

Working

Imagine you developed a new website, with lots of images, videos and downloads.. Yeah.. a dynamic website like Twitter, Youtube or Flickr. With your limited budget you managed to get a dedicated server and expects your website to work in a speed as it loads from your local PC. But you are a single user who will be accessing it, if it is from a local machine. So can you just think of making it live from a remote server in the same way from a local PC and make it accessible to thousands of users. It will be a hard time. I bet.

You might be expecting a considerable load in the server, but as the users hit increases the load may rise. Here comes the MEMCACHED caching system which helps in alleviating the load. In MEMCACHED, when a user acceses a page in a server, the SQL queries will be cached. This helps when a same webpage or query is run later. Since, its already in the cache, it loads faster. The Memcache works as a centralized system, so that it can be utilized for any part of your website. In other words, it can be be considered as its loading from a same virtual pool of memory. It mainly reduces the number of times an external data source reads a data.

The Memcached is equipped with Hash tables, where it stores the information of the databases. When a hash table is full, the older data will be purged out so that new data can enter into the table.

Memcache is a free software and can be run on Linux, Windows and Mac platforms

How to install Memcached

1. Enable EPEL on the server.

# rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-4.noarch.rpm

2. Install memcached

# yum install memcached

Loading “installonlyn” plugin

Setting up Install Process

Setting up repositories

Reading repository metadata in from local files

Parsing package install arguments

Resolving Dependencies

–> Populating transaction set with selected packages. Please wait.

—> Downloading header for memcached to pack into transaction set.

memcached-1.4.5-1.el5.i38 100% |=========================| 7.7 kB 00:00

—> Package memcached.i386 0:1.4.5-1.el5 set to be updated

–> Running transaction check

–> Processing Dependency: libevent-1.4.so.2 for package: memcached

–> Processing Dependency: libevent for package: memcached

–> Restarting Dependency Resolution with new changes.

–> Populating transaction set with selected packages. Please wait.

—> Downloading header for libevent to pack into transaction set.

libevent-1.4.13-1.i386.rp 100% |=========================| 4.5 kB 00:00

—> Package libevent.i386 0:1.4.13-1 set to be updated

–> Running transaction check

Dependencies Resolved

=============================================================================

Package Arch Version Repository Size

=============================================================================

Installing:

memcached i386 1.4.5-1.el5 epel 71 k

Installing for dependencies:

libevent i386 1.4.13-1 base 112 k

Transaction Summary

=============================================================================

Install 2 Package(s)

Update 0 Package(s)

Remove 0 Package(s)

Total download size: 183 k

Is this ok [y/N]: y

Downloading Packages:

(1/2): libevent-1.4.13-1. 100% |=========================| 112 kB 00:02

(2/2): memcached-1.4.5-1. 100% |=========================| 71 kB 00:00

Running Transaction Test

Finished Transaction Test

Transaction Test Succeeded

Running Transaction

Installing: libevent ######################### [1/2]

Installing: memcached ######################### [2/2]

Installed: memcached.i386 0:1.4.5-1.el5

Dependency Installed: libevent.i386 0:1.4.13-1

Complete!

3. Configuring memcached

# vi /etc/sysconfig/memcached

This will look similar to this:

PORT=”11211″

USER=”memcached”

# max connection

MAXCONN=”1024″

CACHESIZE=”64″

OPTIONS=””

You can edit it accordingly according to your need.

PORT: the default port is 11211

USER: the default user is memcached

MAXCONN: the maximum simultaneous connection is 1024.

CACHESIZE: the default object storage64 megabytes. You can increase according to your need.

OPTIONS=”-l 3.3.3.3″ Listens to 3.3.3.3 which is an internal IP address and can be accessed only through a Local area network. This is a way of secure installation.

4. Start memcached

# chkconfig memcached on

# /etc/init.d/memcached start

5. How to stop and restart

# /etc/init.d/memcached stop

# /etc/init.d/memcached restart

6. How to check the usage of Memory memcached

# memcached-tool IP_ADDRESS:Port

# memcached-tool IP_ADDRESS:Port display

# memcached-tool 127.0.0.1:11211

7. How to check the stats of Memcached

# memcached-tool IP_Address:Port stats

# memcached-tool 127.0.0.1:11211 stats

If you require help, contact SupportPRO Server Admin

Server not running properly? Get A FREE Server Checkup By Expert Server Admins - $125 Value

Leave a Comment