RPC over a message queue

Message queues are really wonderful things, I'm not going to get into the many, many uses for them, but over the past year or so I've been experimenting with a fairly simple implementation which allows PHP to perform RPC requests over one.

If there was a tao of scalability, almost right at the beginning would be about reducing your overall application into small units of work with as little reliance on each other (well, from a programming perspective this makes sense too).

The reason being that when you want to scale it will not usually be the entire application at once, but rather specific elements don't scale as well as others as you need to make more resources available for that task and distribute load accordingly.

Traditional approaches like web load balancers sort of half solve this, but in a less optimal way because there is less feedback (it may be unavailable completely) about when the server is ready to handle tasks, how loaded it really is and if there's any arbitrary cap (relating to an SLA or similar) which cannot be breached.

ActiveMQ & STOMP

The message broker/queue ActiveMQ and the STOMP protocol, when combined together are very powerful simply because their one of the few combinations which make this technology available to the PHP world.

STOMP is a simple text based protocol (very similar to HTTP) used for interacting with message queues, and ActiveMQ supports this out of the box.

Traditionally message queues were expensive or only available to the C++/Java/.NET world, this is slowly changing though with more open protocols and production grade servers like ActiveMQ.

Download

Get yourself a copy of ActiveMQ version 4 or version 5 from the ActiveMQ website, extract it and run the "bin/activemq" script. Out of the box it's configured to provide in-memory queues (perfect for testing) with most common protocol listeners.

Secondly, get my ProMQ library and examples, which require PHP5 with the sockets extension loaded.

Running the Examples

With ActiveMQ started and listening for STOMP connections, unpacking the ProMQ library and examples we can begin with a quick demo.

Start the client and server side of the examples, connecting to the ActiveMQ server:

# ./test-server.php 192.168.50.105 &
# ./test-client.php 192.168.50.105

And you'll get something like:

Remote function called with argument: Hello

0.0144910812378
Result is:
Array
(
    [Whee] => 123
)

Which took ~0.01s to process, quite an expensive operation compared to calling the same function locally. However this will scale up until the message queue falls over which on my test machine (a beefy SPARC server) is around 10k operations per second, which with some tuning aparently can go a lot higher.


Leave a Reply



About

Harry is a professional developer and sysadmin from London, UK.

He's an atheist, employed at PixelMags LLC, a socialist and has a pragmatic outlook on life, love and religion.

Bookmarks

I'm constantly finding interesting stuff, here are some of the things I've bookmarked recently:

HarryR on Faves.com