<div dir="ltr"><div><span style="font-family:arial,sans-serif;font-size:13px">Hi,</span></div><div><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="font-family:arial,sans-serif;font-size:13px">An application I'm working on has a similar requirement regarding message persistence. We elected to use RabbitMQ. One notable difference between this app and the one you're describing is we're not moving a message between states as in a work-flow model. But as Dmitry said below, "if the message persistency is a MUST then you have to look into a reliable message broker..." or some other way to manage persistence.</span></div>
<div style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div style><span style="font-family:arial,sans-serif;font-size:13px">I may have missed it, but in Jesper's reply I didn't see the persistence requirement addressed. Jesper has a nice write-up "On Erlang, State and Crashes that you might find useful (</span><font face="arial, sans-serif"><a href="http://jlouisramblings.blogspot.com/2010/11/on-erlang-state-and-crashes.html">http://jlouisramblings.blogspot.com/2010/11/on-erlang-state-and-crashes.html</a>). </font></div>
<div style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px"><br></span></div><div style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">Cheers,</span></div>
<div style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">Rich</span></div><div style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px"><br>
</span></div><font face="arial, sans-serif">Date: Tue, 22 Jul 2014 22:58:08 +0300</font><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">From: Dmitry Kolesnikov <</span><a href="mailto:dmkolesnikov@gmail.com" style="font-family:arial,sans-serif;font-size:13px">dmkolesnikov@gmail.com</a><span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">To: Miles Fidelman <</span><a href="mailto:mfidelman@meetinghouse.net" style="font-family:arial,sans-serif;font-size:13px">mfidelman@meetinghouse.net</a><span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">Cc: erlang-questions Questions <</span><a href="mailto:erlang-questions@erlang.org" style="font-family:arial,sans-serif;font-size:13px">erlang-questions@erlang.org</a><span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">Subject: Re: [erlang-questions] design pattern question for messaging</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px"> system</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">Message-ID: <</span><a href="mailto:B61EC9A8-DC4B-48F1-84A2-D8FCE65C3994@gmail.com" style="font-family:arial,sans-serif;font-size:13px">B61EC9A8-DC4B-48F1-84A2-D8FCE65C3994@gmail.com</a><span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">Content-Type: text/plain; charset=windows-1252</span><br style="font-family:arial,sans-serif;font-size:13px"><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">Hello,</span><br style="font-family:arial,sans-serif;font-size:13px"><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">"Except, that model kind of falls down because Erlang message are unreliable by design, and don't persist in the event of a process crash (much less a node crash).?</span><br style="font-family:arial,sans-serif;font-size:13px">
<br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">This is a trade off you have to accept. If the message persistency is MUST then you have to look into reliable message broker solution then.</span><br style="font-family:arial,sans-serif;font-size:13px">
<br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">I think you already articulate the basic pattern, which is used in many applications</span><br style="font-family:arial,sans-serif;font-size:13px">
<br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px"> ?[ mq ]?[ worker ]?[ mq ]-[ worker ]?</span><br style="font-family:arial,sans-serif;font-size:13px"><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">You are right if you keep messages within mailbox then crash of process destroys mailbox. Thus, you need an intermediate process to hold messages. The intermediate process does not do any work except enqueue / dequeue operation. Therefore, the number of failures is limited. The worker is pool of processes Jesper Louis gave you list pool libraries, I can give one more if you need ;-)</span><br style="font-family:arial,sans-serif;font-size:13px">
<br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">I am using a similar pattern at one of my application. The biggest problem is node crash due to OOM or external factors. I am trying to solve it by duplicating the processing path into N-distinct node (my app uses last-write wins but you might use other conflict resolution technique).</span><br style="font-family:arial,sans-serif;font-size:13px">
<br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">You can extend the pattern by having persistent mq to check-point intermediate results to survive node crash but I found it complicated.</span><br style="font-family:arial,sans-serif;font-size:13px">
<br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">?[ p-mq ]?[ worker ]?[ mq ]?[ worker ]?[ mq ]?[ worker ]?[ p-mq ]-</span><br style="font-family:arial,sans-serif;font-size:13px">
<br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">Best Regards,</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">Dmitry</span><br style="font-family:arial,sans-serif;font-size:13px">
<br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">On 22 Jul 2014, at 14:53, Miles Fidelman <</span><a href="mailto:mfidelman@meetinghouse.net" style="font-family:arial,sans-serif;font-size:13px">mfidelman@meetinghouse.net</a><span style="font-family:arial,sans-serif;font-size:13px">> wrote:</span><br style="font-family:arial,sans-serif;font-size:13px">
<br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> Hi Folks,</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">> So far, I've mostly been experimenting w/ Erlang, and using Erlang-based technology (notably CouchDB). As I'm thinking about a new application, I'm having trouble getting my hands around an appropriate design pattern. I wonder if anybody might be able to point me in the right direction.</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> The application is message handling (back to that in a minute). I realize that I have a pretty good idea how to handle some kinds of applications in a highly concurrent fashion, such as:</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">> - modeling/simulation (obviously, each entity - such as a vehicle - is a process) - this is what led me to Erlang in the first place</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">> - protocol engines as state machines - e.g., spawn a process for each tcp connection</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> - transaction systems - spawn a process for each transaction</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">> - transaction oriented</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">> But I'm looking at a work flow application that maps onto a paper-forms-based model. It's a classic queuing system - work elements move from queue to queue as they're worked on. The obvious first thought is:</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">> - a process for each queue</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> - a worker process for each work step</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">> - a message for each piece of work-in-process -- moving from queue to queue via the worker processes</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> Except, that model kind of falls down because Erlang message are unreliable by design, and don't persist in the event of a process crash (much less a node crash).</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> My first two thoughts are:</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">> - spawn a process for each queue entry, pass around the PIDs</span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> - use Mnesia to hold the queues</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> But neither of those feels quite right. This must be a solved problem, but I'm hitting a blind spot. So... what is the design pattern for queuing systems and/or reliable message passing in Erlang?</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> Any good examples to look at? Good presentation slides or reference materials to review?</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> Thanks very much,</span><br style="font-family:arial,sans-serif;font-size:13px">
<span style="font-family:arial,sans-serif;font-size:13px">></span><br style="font-family:arial,sans-serif;font-size:13px"><span style="font-family:arial,sans-serif;font-size:13px">> Miles Fidelman</span><br></div>