Auto-restart MariaDB when it dies

I am running a small, low-memory VPS where MariaDB is frequently killed by the system due to lack of memory. Since I wanted to keep the budget fixed and didn’t have enough time to look under the hood to find the root cause, I opted in for running a cron to periodically check and start MariaDB if its not running.

* * * * * /bin/systemctl status mariadb.service > /dev/null || /bin/systemctl start mariadb.service

The double pipe || means OR and will execute the 2nd command if the first command fails somehow. (Returns an exit code greater than zero.)

jQuery noConflict

In a recent project, we had to load jQuery as a part of a chrome extension into an external page where the $ alias has already been used. We tried the classic solution: jQuery.noConflict(), which returns control of $ back to the other library. However, it did not work and we had to dig more.

Got it working with this:

Installing Symfony 4 (3.4.0-BETA1)

I wanted to install the Symfony 4 beta to play with the new features. While I was able to get most of it working, but was facing issues due to package dependency when trying to install some common bundles. I then settled with the 3.4 branch, which has the exact same functionalities except for some backward compatibility.

Here are the steps to get the framework up & running with the most used bundles:

Once the installation is done, you can start the application using the web server and it should be running.

You may want to follow the Symfony 4 blog series at to get started as the official documentation is not updated yet.


Choosing Boring Technology

A wonderful presentation by Dan McKinley of Stripe on how technology companies make mistake in choosing the next piece of shiny technology instead of relying on their existing and tested stack. He outlines how this increases the overall cost and how this cripples the development team in many ways. I have to admit that for many years, I used to do this myself and would go for the next framework, the next data store, etc. I learned the lesson the hard way and thankfully since then I choose the fewer, the most known technology that serves the need.

Organizing Life

I am a procrastinator. One who has mastered its art. I have always been so. I do important things at the very last minute, with the highest intensity. I have succeeded with this approach more than I should have. As a result, I’ve escaped the failure narrowly all the time. However, it doesn’t give me any joy or satisfaction. It leaves a “I could have done better” feelings inside all the time. And its something that I am not proud of.

Well, enough with that. There has to be a stop to this and I have to make some serious changes to get out of this nasty habit. I’m reading some books and blog posts to understand it better. I’m also trying to develop a system that, if followed properly, will allow me to avoid this.

So far I am adopting the following changes in my life:

I am hopeful I can stick to these changes and get better in this. I’ll review this every two weeks and will make any adjustments that I see fit.

Photo by Kyle Szegedi.

(Re) start of blogging

Its been some time since I’ve last posted anything in my blog – it would happen that I’d feel like posting something in the blog but never actually write anything. Sometimes I’d log into WP Admin to write, but then I’d feel the pressure of upgrading WP to latest version, upgrading the plugins, curate the old posts – so many tasks to take care before I get the peace of mind to write a new piece.

Today I got some time on hand due to me being on home for Eid-ul-Azha and I decided to ease my blogging and thus I moved my old blog to a subdomain ( and installed a fresh WP to start with 🙂

So, what would I write now that I have removed the clutter ? I am planning to write some posts on Symfony2, some on the recent challenges I faced, some on my religious views – that’s so far I can see it for now.

Concurrent HTTP requests in PHP using pecl_http

The pecl_http extension has a little gem that can be handy at times – HttpRequestPool. Using this, you can send concurrent HTTP requests and can gain efficiency in fetching non-related data at once. For example, from an external source if your application needs to retrieve an user’s profile, their order history and current balance, you can send parallel requests to the API and get everything together.

Here is a simple example code to illustrate that:

Amazon S3 integration with Symfony2 and Gaufrette

Its amazing how Symfony2 has created an ecosystem where you can just add a bundle to your application and in minutes, you are able to make use of functionality/integration that would take you days to add if you had to code it up yourself. A while ago, I needed to add S3 CDN support to our Loosemonkies application so that the avatars uploaded by job seekers and company logos uploaded by employers would be stored in a globally accessible CDN. I started looking for a ready-to-use bundle that I can just add to the project, beef up a few config and everything else would just work. However, this time the task seemed a bit tough.

After much searching and evaluating, I stumbled upon this wonderful post. The author has shown here how to use Gaufrette and the Amazon AWS bundle together to achieve what I was looking for. It took me a while to follow the steps and finally I integrated it. Then I had a new requirement where I would need to upload assets that have already been sitting in the local, so I would add an additional method in the PhotoUploader class to handle it – uploadFromUrl. It would guess the mime type of the file by extension, as we do not have the mime type handed to us by PHP. It worked beautifully and we were all set.

Sharing the code here in case others find it useful.


Optimizing variables cache in Drupal 6

In Drupal 6, a number of caching strategies are incorporated to handle large traffic. One of them is the serialization of the whole variable table. It is being cached in the database and gets extracted into global $conf variable in each invoke.

In one of our production sites, we faced hard time to keep up with the memory requirement of PHP for the unserialization of this variable from the cache. The variables table was so large that we had to assign around 1GB memory to each PHP thread so that the value can be unserialized without memory exhaustion. This made it much harder to scale the application.

So, we decided to do something about it and successfully handled it by doing the following:

1. First of all, we installed the memcache module to move the cache storage from DB to memory

2. We then edited the memcache module’s cache_get and cache_set functions to store/retrieve individual items from the variables array and split/join them when requested.

3. This requires a memcache call for each of the items in the variable array, but consumes a small amount of memory as there is no huge unserialize operation going on.

4. We performed a few tests to see if the site is working as before, and found its working perfectly!

Here is the code in case you are facing similar issue: (more…)

JSONize your PHP classes using traits

I was having a discussion with a colleague regarding how to add generic JSON based representation to a number of classes without making a big effort. The immediate solution that came in my mind is to use the Traits for it (introduced in PHP 5.4).

So I wrote the following example for him:




The result of running the above code will be like this: