Tuesday, June 25, 2013

Using Memcached with Symfony 2 (on CentOS 6)

The biggest problem, that I've been running into for years, is always performance. But sometimes optimizing your code isn't good enough, sometimes because you're depending on external libraries, which you don't want to change.

For one of our customers, I ran into this problem as well, so I had to find a solution. I've already heared about Memcached often, but I never had the time to look into it and I was kinda scared to have to implement it in an already existing application. Afterwards, I found using the basics of memcached was pretty simple, and it boosted my page speed by 50-75%(!), only by caching doctrine queries and results.

A quick Google search found me a bundle that claims to do exactly what I want: the LswMemcacheBundle. LswMemcacheBundle is developed by LeaseWeb, which leverages the power of the PHP 'memcached' extension (not the older 'memcache' PHP extension, the d make a huge difference as explained on http://code.google.com/p/memcached/wiki/PHPClientComparison).

The installation instructions looked quite simple: install memcached, install the memcached extension and add one line to your composer.json. For Debian based systems this is the case, but CentOS 6 has some obstacles in place.

With CentOS 6 there are a couple problems:
  1. The php extension 'memcached' requires libmemcached. Unfortunatly the version of libmemcached in yum is to old to meet the requirements
  2. The php memcached extension can't be installed trough yum, so this has to be installed trough pecl
  3. Installing the php memcached extension trough pecl threw some errors while compiling.

Wednesday, February 6, 2013

Using master and slave databases with Symfony 2 + Doctrine

One of our customers works from different locations in the Netherlands, but with their backoffice on a server in south-east Germany and an extra penalty caused by the crappy internet connection, this can sometimes be very slow.
So the first thing I thought of to solve this is to run a mirror of their backoffice on location. Setting up the MySQL replication[1] and automatic syncing of files[2] was easy. But then my code had to be adjusted to work with a Master server (the server in Germany) and a Slave server (within the local network).
For those of you unfamiliar with the concept of Master and Slave database servers: The idea is is to have multiple database servers, one Master server (who 'owns' all the data) and one, or multiple, Slave servers, who receive a copy of all the changes from the Master server. The Master server needs to be accessible to all the Slave servers (i.e. via the internet), but the Slave servers can be servers within a local network.
When I started my search on the internet for information on how to achieve this, I found many complicated solutions, including solutions that told you to change vendor code, to write custom connection classes etc. But I figured there had to be an easier way and (thankfully) I was right.