Batista R. Harahap

Accident Happens

Just a few days ago, an accident happened. It was 4 AM and I am moving this blog over to CloudKilat. Already got the MySQL dump transferred, would need to have the whole site rsync-ed to the new server. So I did it. Without me realizing, I misplaced the source and destination ending up the new server's home directory rsync-ed to the original server. A fundamental mistake.

Luckily, as I said above, the MySQL dump is already safe at hand so to rebuild another Wordpress blog would be a matter of configurations.

From what I read across the web, Wordpress is relatively easier to scale and having only a single core at work, I will need to scale this blog properly. I haven't though. As of this writing, I have only set up nginx + php-fpm + APC.

Disclaimer: This is a for fun project, nothing too serious, most will be proof of concepts. Some tricks were absorbed when scaling Urbanesia's frontend, DailySocial and Trenologi's WordPress installation.

What I'm planning to do is to prepare the blog by scaling minimally only on what's needed. This CloudKilat VM is packed with 2 GB of memory so it's spacious enough to run a minimal dynamic web server with MySQL in it. However, I am not getting that illusion of speed even though CloudKilat is here in Indonesia. The web server is curl-ing post pages at 11 seconds. Way too much overhead there.

The specs are as follow:

First Look

Initial assessment shows that the server is not capable of handling high loads without some special tunings. I am also testing CloudFlare with this server and when it's cached, the response time is great. On the dynamic side, the server is lagging to spit out HTML as fas as it can. Execution time to render an HTML can be as long as 6 seconds. Unacceptable at all.

Since this is a VM and root access is a given, I'm starting off by removing all unnecessary services below:

  • Apache httpd
  • Sendmail
  • Samba
  • Bind

I never want to have sendmail to be installed by default on any of my servers. Spammers know what their doing and will surely take advantage of unsecure sendmail installations.

Dumb Cache

So the first easiest thing to do is hack WordPress' index.php file and add my own code to do caching with Memcached. What I want to do is cache every dynamically generated pages into memory, purge cache if I make any changes and hook with WordPress' output to do this.

As it turns out, I don't need to hook with WordPress' output, I just need to hook into PHP's output buffering mechanism. The codes below is a rather crude way of doing it.

The problem with the code above is that I can't refresh by will let's say if I had to make an Edit on one of my blog posts. So a line with code needs to be adjusted, specifically line 9 to be:

$cache = !empty($GET['refresh']) && $GET['refresh'] == 'waku' ? FALSE : $mem->get($key);

The final code becomes below.

Execution time goes as fast as 0.00081706047058105 second and it's exactly what I'm after. Haven't really diagnosed any problems with the codes yet.

Moral of The Story

I accidentally deleted my blog and ended up hacking Wordpress to my taste. Everything does happen for a reason ;)

02 July 2013 by Batista Harahap on hack | memcache | optimize | speed | wordpress
comments powered by Disqus