Coding PHP Zend Framework

Queueing With Zend Queue and MemcacheQ

I was bored last night so I thought I’d enhance one of my applications with a little bit of Queueing. (And for the record both Queueing and Queuing are valid spellings depending on your locale)

The concept is fairly straight forward. Normally when a web application runs, it runs in a linear pattern. It starts, it does stuff, it does more stuff, then it finishes with a web page. That’s pretty much it. The problem is, the user has to wait for it to do stuff and more stuff. And when they have to wait for more and more stuff, it gets slow. If some of that stuff can be offloaded because it has no affect on what the user sees right now – why not? That’s where a Queue comes in to play. Send a command to a queue that something else can process for you.

So enter Zend Queue. A feature of Zend Framework that will let you use various queue systems to your advantage. I chose MemcacheQ because it was simple to install and I knew that I’d need not do anything to my configurations to utilize it. MemcacheQ is a light-weight, fast, persistant queueing system (It’ll remember what it has when it gets shut down) based on Memcache’s protocol. So accessing the data in the queue is a piece of cake.

I tried using Zend_Queue_Adapter_Memcacheq in Zend Framework 1.10.8 – but it failed. It doesn’t seem to work properly with MemcacheQ 0.2.0 which is the latest version. That lead me to write my own little adapter – based on the existing one – to fix the incompatibilities. I also added automatic serialization to the messages sent to the queue. This allows me to send useful items, instead of just strings. I implemented the queue system in a way that I can now do queued inserts and updates on database rows wherever I see the need.  It’s also setup in a way that if sending a message to the queue fails, it will do the desired action right away (slower for the user, but no loss of data.)  After that I was set.

I send my messages to my queue as described in Zend Framework’s documentation ($queue->send($bundle)); My bundle is a small array containing a few useful items. One: a class name, two: a method to call, three: parameters for the method. And that’s it. Now I’m queueing magical commands.  I need to process the queue.

I then wrote a queue processor. This processor is a very small command line tool that utilizes my existing Zend Framework applications configuration – so it has all the access it needs to the application database and code base. The queue processor pulls in a small set of bundles from the queue – executes them – and waits for a short period of time and then processes more.  In the event that a command fails, the queue processor will re-queue the item and wait to try again.  This is to prevent data loss.  If for some reason the database is unavailable the updates and inserts will persist in the queue until they can be executed properly.

I hope I’ve explained it in enough detail that you can understand the process. If not, leave a comment and I’ll try and clarify it. I’m not posting any code just yet because it’s not “pretty.”

So how well did it do? Judge for yourself. You can tell when I enabled it.


Typecasting with double-not (!!) vs. (bool)

I was asked a strange question today.  “What’s return !! $row; do?” And I had never really been witness to the syntax of the double-not ‘!!’ because really, to me, it’s a bit strange and contradictory for the coding styles I’m used to.  So I looked it up a bit more to see if it was actually faster than proper type casting with (bool)$row in php.  Nobody had any real results. I ran a quick test and sure enough using !! is faster… for arrays.  Other than that, the time is so close it’s insignificant.

Here’s the quick ‘n dirty results:

Time testing (bool) vs. !! conversions – 1,000,000 interations

converted array with (bool) done in 1.06559991837 seconds.
converted array with !! done in 0.534655094147 seconds.

converted ” with (bool) done in 0.572640895844 seconds.
converted ” with !! done in 0.523654937744 seconds.

converted (int) with (bool) done in 0.49324798584 seconds.
converted (int) with !! done in 0.5086581707 seconds.

converted object with (bool) done in 0.564023017883 seconds.
converted object with !! done in 0.556074857712 seconds.

I had to do one million iterations just so the times would add up to something substantial.  Really, the difference is only faster if you happen to want to get a quick ‘true or false’ from an array.  The rest are so insignificant it’s not worth the confusion of a double-not.  Personally, I’d rather see an actual check of $row to see if it contains something useful before returning true or false just based on it being set.  But that’s just me.


A Pinch of Performance (Apache Tuning)

If you’re using rewrite rules and other special configuration directives on your website, like any Zend Framework site does, you might want to consider the following:

Avoiding .htaccess and ‘AllowOverride [not none]’ on large websites.