{"id":2282,"date":"2011-12-11T01:39:16","date_gmt":"2011-12-11T00:39:16","guid":{"rendered":"http:\/\/www.devco.net\/?p=2282"},"modified":"2011-12-13T12:48:20","modified_gmt":"2011-12-13T11:48:20","slug":"common-messaging-patterns-using-stomp","status":"publish","type":"post","link":"https:\/\/www.devco.net\/archives\/2011\/12\/11\/common-messaging-patterns-using-stomp.php","title":{"rendered":"Common Messaging Patterns Using Stomp – Part 1"},"content":{"rendered":"
As most people who follow this blog know I’m quite a fan of messaging based architectures. I am also a big fan of Ruby and I like the simplicity of the Stomp Gem to create messaging applications rather than some of the more complex options like those based on Event Machine (which I am hard pressed to even consider Ruby) or the various AMQP ones.<\/p>\n
So I wanted to do a few blog posts on basic messaging principals and patterns and how to use those with the Stomp gem in Ruby. I think Ruby is a great choice for systems development – it’s not perfect by any stretch – but it’s a great replacement for all the things systems people tend to reach to Perl for.<\/p>\n
Message Orientated Middleware represents a new way of inter process communications, different from previous approaches that were in-process, reliant on file system sockets or even TCP or UDP sockets. While consumers and producers connect to the middleware using TCP you simply cannot really explain how messaging works in relation to TCP. It’s a new transport that brings with it its own concepts, addressing and reliability.<\/p>\n
There are some parallels to TCP\/IP wrt to reliability as per TCP and unreliability as per UDP but that’s really where it ends – Messaging based IPC is very different and best to learn the semantics. TCP is to middleware as Ethernet frames are to TCP, it’s just one of the possible ways middleware brokers can communicate and is at a much lower level and works very differently.<\/p>\n
Why use MOM<\/strong> There are many other reasons but for me these are the big ticket items – especially the 2nd one. <\/p>\n Note of warning though while mature brokers are fast, scalable and reliably they are not some magical silver bullet. You might be able to handle 100s of thousands of messages a second on commodity hardware but it has limits and trade offs. Enable persistence or reliable messaging and that number drops drastically.<\/p>\n Even without enabling reliability or persistence you can easily do dumb things that overwhelm your broker – they do not scale infinitely and each broker has design trade offs. Some dedicate single threads to topics\/queues that can become a bottleneck, others do not replicate queues across a cluster and so you end up with SPOFs that you might not have expected. <\/p>\n Message passing might appear to be instantaneous but they do not defeat the speed of light, it’s only fast relative to the network distance, network latencies and latencies in your hardware or OS kernel.<\/p>\n If you wish to design a complex application that relies heavily on your middleware for HA and scaling you should expect to spend as much time learning, tuning, monitoring, trending and recovering from crashes as you might with your DBMS, Web Server or any other big complex component of your system.<\/p>\n Types of User<\/strong> Types of Message<\/strong> There are some riffs on these 2 types of message source – you get short lived private destinations, queues that vanish soon as all subscribers are gone, you get topics that behave like queues and so forth. The basic principals you need to know are just Topics and Queues and detail on these can be seen below, the rest builds on these.<\/p>\n Topics<\/strong> There might be some buffers involved which means if you’re a bit slow to consume messages you will have a few 100 or 1000 there waiting depending on your settings, but this is just a buffer it’s not really a store and shouldn’t be relied on. If your process crash the buffer is lost. If the buffer overflow messages are lost.<\/p>\n The use of topic is often described as having Publish and Subscribe<\/em> semantics since consumers Subscribe and every subscriber will get messages that are published. <\/p>\n Topics are often used in cases where you do not need reliable handling of your data. A stock symbol or high frequency status message from your monitoring system might go over topics. If you miss the current stock price soon enough the next update will come that would supersede the previous one so why would you queue them, perfect use for a broadcast based system.<\/p>\n Queues<\/strong> This style of message is often used to create async workers that does some kind of long running task. <\/p>\n Imagine you need to convert many documents from MS Word to PDF – maybe after someone uploaded it to your site. You would create each job request in a queue and your converter process consumes the queue. If the converter is too slow or you need more capacity you simply need to add more consumers – perhaps even on different servers – and the middleware will ensure the traffic is load shared across the consumers.<\/p>\n You can therefore focus on a single function process – convert document to PDF – and the horizontal scalability comes at a very small cost on the part of the code since the middleware handles most of that for you. Messages are stored in the broker reliably and if you choose can even survive broker crashes and server reboots.<\/p>\n Additionally queues generally have a transaction metaphor, you start a transaction when you begin to process the document and if you crash mid processing the message will be requeued for later processing. To avoid a infinite loop of a bad message that crash all Consumers the brokers will also have a Dead Letter Queue where messages that have been retried too many times will go to sit in Limbo for an administrator to investigate.<\/p>\n These few basic features enable you to create software that resilient to failure, scalable and not susceptible to thundering herd problems. You can easily monitor the size of queues and know if your workers are not keeping up so you can provision more worker capacity – or retire unneeded capacity.<\/p>\n Demonstration<\/strong> Note I am using CoilMQ here instead of the Ruby Stompserver since Stompserver has some bug with queues – they just don’t work right at all.<\/p>\n <\/code><\/p>\n At this point you have a working Stomp server that is listening on port 61613, you can just ^C it when you are done. If you want to do the stuff below using more than one machine then add to the command line for stompserver -b 0.0.0.0<\/em> and make sure port 61613 is open on your machine. The exercises below will work fine on one machine or twenty.<\/p>\n To test topics we first create multiple consumers, I suggest you do this in Screen and open multiple terms, for each terminal set the GEM_HOME<\/em> as above.<\/p>\n Start 2 or 3 of these, these are consumers on the topic: <\/code><\/p>\n Now we’ll create 1 producer and send a few messages, just type into the console I typed 1, 2, 3 (ignore the warnings about deprecation):<\/p>\n <\/code><\/p>\n You should see these messages showing up on each of your consumers at roughly the same time and all your consumers should have received each message.<\/p>\n Now try the same with \/queue\/my.input<\/em> instead of the topic and you should see that the messages are distributed evenly across your consumers.<\/p>\n You should also try to create messages with no consumers present and then subscribe consumers to the queue or topic, you’ll notice the difference in persistence behavior between topics and queues right away<\/p>\n
\nThere are many reasons but it comes down to promoting a style of applications that scales well. Mostly it does this by a few means:<\/p>\n\n
\nThere are basically 2 terms that are used to describe actors in a message orientated system. You have software that produce messages called Producers<\/em> and ones consuming them called Consumers<\/em>. Your application might be both a producer and a consumer but these terms are what I’ll use to describe the roles of various actors in a system.<\/p>\n
\nIn the Stomp world there really are only two types of message destinations. Message destinations have names like \/topic\/my.input<\/em> or \/queue\/my.input<\/em>. Here we have 2 message sources – the one is a Topic and the other is a Queue. The format of these names might even change between broker implementations.<\/p>\n
\nA topic is basically a named broadcast zone. If I produce a single message into a topic called \/topic\/my.input<\/em> and there are 10 consumers of that topic then all 10 will get a copy of the message. Messages are not stored when you aren’t around to read them – it’s a stream of messages that you can just tap into as needed. <\/p>\n
\nInstead of broadcasting messages queues will store messages if no-one is around to consume them and a queue will load balance the work load across consumers.<\/p>\n
\nPlaying with these concepts is very easy, you need a middleware broker and the Stomp library for Ruby, follow the steps below to install both in a simple sandbox that you can delete when you’re done. I’ll assume you installed Ruby, Rubygems and Python with your OS package management. <\/p>\n<\/p>\n
\r\n$ export GEM_HOME=\/tmp\/gems\r\n$ export PYTHONPATH=\/tmp\/python\r\n$ gem install stomp\r\n$ mkdir \/tmp\/python; easy_install -d \/tmp\/python CoilMQ\r\n$ \/tmp\/python\/coilmq\r\n<\/pre>\n
\n<\/p>\n
\r\n$ STOMP_HOST=localhost STOMP_PORT=61613 \/tmp\/gems\/bin\/stompcat \/topic\/my.input\r\nConnecting to stomp:\/\/localhost:61613 as \r\nGetting output from \/topic\/my.input\r\n<\/pre>\n
<\/p>\n
\r\n$ STOMP_HOST=localhost STOMP_PORT=61613 \/tmp\/gems\/bin\/catstomp \/topic\/my.input\r\nConnecting to stomp:\/\/localhost:61613 as\r\nSending input to \/topic\/my.input\r\n1\r\n2\r\n3\r\n<\/pre>\n