Posts Tagged ‘Memcached’

Managing yum repositories (Dag)

Saturday, September 5th, 2009

So I got a memcached project working fine on my dev env. I showed it to my boss and told him all about the advantages of having memcached working on a production server so he gave me green light to implement it.

Since we have a dedicated hosting package (Codero, former Aplus) I thought It’ll be easier to ask our hosting provider to install memcached on our servers… they mailed us back saying that it would be considered advanced support and we’ll be charged 99dlls if we wanted to install it… of course we said no. We had ssh access so we were able to do it and spare the money 😀

Since on my dev env I just did yum installs I had the idea it’ll be the same on prod server, WRONG!

Th thing is that they have the default yum repositories and memcached was found on Dag repo so I needed to add it to get memcached. Looked for info over the web and found this web with the proper instructions.

I’m on a CentOS server so I’ll put here a brief description of what I did.

1. go to /etc/yum.repos.d/

2. create a file called dag.repo with this content:

[dag]

name=Dag RPM Repository  for Red Hat Enterprise Linux

baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag

gpgcheck=1

enabled=1

3. download the GPG key to your server and import it with the following command:

rpm –import RPM-GPG-KEY.dag.txt

4. congratulate yourself!

[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://apt.sw.be/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
enabled=1

Now I was able to install the memcached server and the memcached php module:

yum install memcached

yum install php-pecl-memcache

Then I just restarted httpd and done!

Hope this is helpful.

Memcached, working!!!!

Thursday, September 3rd, 2009

Hard work, a couple of hours of investigation and a few modified lines of code later I got a project working with memcached and let me tell you it does work!!!

I was very happy when I saw my database being released from a lot of processes.

What I love the most is the options memcached gives you to choose how much time a cached object will remain active in the server!!! for very uncommon queries I set the cache to remain on the server for 15 min while for the other more persistent queries I decided to give them no limit.

I’m really pleased! just sent an email to my boss so he can check this out and it’s 2 o’clock in the morning so job done and now it’s time to go to sleep.

Memcached

Wednesday, September 2nd, 2009

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.