Memcached

Wow!

I’m in charge of a site that uses a lot of resources, thousands of images, thousands of db rows and literally thousands of visitors each day. We are currently getting approximately 60 to 70 thousand visitors but the thing is that by the time we get to 60 thousand the server starts to slow down. The require for images, queries and number of petitions per second made us think of a redesign of the site.

First step was to load the balance between 2 servers for http request and for database queries. Each server will have its own database and web code. Over those servers we put a load balancer hardware.

Second step was clean the database, eliminate duplicated rows and reindex fields. Throw away those fields that were not used and improve queries.

Third step was cleaning up the code, delete lines of unused code and repurpose a couple of functions. We also set a template engine (Smarty).

Now it seems the site is working fine server speed improved but still the database queries were too slow… what was left to do? Memcache!

First install memcache on the server. In my case I’m running CentOS so I just had to do:

yum install memcached

Then install php’s memcache support:

yum install php-pecl-memcache

Restart httpd and then type the following command on your server’s console

memcached -d -u apache -m 512 -p 11211

What this does is that it starts the memcached daemon with apache user privileges usin 512 Mb of RAM memory and using port 11211.

Since you’ll be needing port 11211 don’t forget to check your iptables config so you may be able to use it.

That’s it! everything is ready, the only thing left is modify your php’s code and you’re on your way.

I haven’t tested it but to be honest I really think it will save us lot’s of resources from the server. I’ll post the result in a few days.

Tags: , , ,

Comments are closed.