<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Udi Dahan - The Software Simplist &#187; Messaging</title>
	<atom:link href="http://www.udidahan.com/category/messaging/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.udidahan.com</link>
	<description>Enterprise Development Expert &#38; SOA Specialist</description>
	<lastBuildDate>Sun, 08 Jan 2012 12:45:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>NServiceBus Videos Online</title>
		<link>http://www.udidahan.com/2011/06/26/nservicebus-videos-online/</link>
		<comments>http://www.udidahan.com/2011/06/26/nservicebus-videos-online/#comments</comments>
		<pubDate>Sun, 26 Jun 2011 08:13:36 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Community]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[MSMQ]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Presentations]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1480</guid>
		<description><![CDATA[When I was at NDC a couple of weeks ago, I got together with Carl Franklin and we recorded a DNR-TV episode on NServiceBus. If you&#8217;re looking for a zero-to-sixty, code-centric explanation of NServiceBus &#8211; this is it.
NServiceBus on DNR-TV
For some more advanced stuff, I suggest looking at the Hidden NServiceBus Gems talk that I [...]]]></description>
			<content:encoded><![CDATA[<p>When I was at NDC a couple of weeks ago, I got together with Carl Franklin and we recorded a DNR-TV episode on NServiceBus. If you&#8217;re looking for a zero-to-sixty, code-centric explanation of NServiceBus &#8211; this is it.</p>
<p><a href="http://dnrtv.com/default.aspx?ShowID=202">NServiceBus on DNR-TV</a></p>
<p>For some more advanced stuff, I suggest looking at the Hidden NServiceBus Gems talk that I gave at Skills Matter the week after. Here we get into all sorts of things that you won&#8217;t tend to find by yourself through regular usage of NServiceBus.</p>
<p><a href="http://skillsmatter.com/podcast/home/hidden-nservicebus-gems/js-1884">Hidden NServiceBus Gems</a></p>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2011/06/26/nservicebus-videos-online/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When to avoid CQRS</title>
		<link>http://www.udidahan.com/2011/04/22/when-to-avoid-cqrs/</link>
		<comments>http://www.udidahan.com/2011/04/22/when-to-avoid-cqrs/#comments</comments>
		<pubDate>Fri, 22 Apr 2011 20:32:50 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[Simplicity]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1441</guid>
		<description><![CDATA[It looks like that CQRS has finally &#8220;made it&#8221; as a full blown &#8220;best practice&#8221;.
Please accept my apologies for my part in the overly-complex software being created because of it.
I&#8217;ve tried to do what I could to provide a balanced view on the topic with posts like Clarified CQRS and Race Conditions Don&#8217;t Exist.
It looks [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/image20.png" alt="which way?" title="which way?" style="float:right; margin-left:10px; margin-bottom:10px;" />It looks like that CQRS has finally &#8220;made it&#8221; as a full blown &#8220;best practice&#8221;.</p>
<p>Please accept my apologies for my part in the overly-complex software being created because of it.</p>
<p>I&#8217;ve tried to do what I could to provide a balanced view on the topic with posts like <a href="http://www.udidahan.com/2009/12/09/clarified-cqrs/">Clarified CQRS</a> and <a href="http://www.udidahan.com/2010/08/31/race-conditions-dont-exist/">Race Conditions Don&#8217;t Exist</a>.</p>
<p>It looks like that wasn&#8217;t enough, so I&#8217;ll go right out and say it:</p>
<p>Most people using CQRS (and <a href="http://codebetter.com/gregyoung/2010/02/20/why-use-event-sourcing/">Event Sourcing</a> too) shouldn&#8217;t have done so.</p>
<h3>Should we really go back to N-Tier?</h3>
<p>When not using CQRS (which is the majority of the time), you don&#8217;t need N-Tier either.</p>
<p>You see, if you&#8217;re not in a collaborative domain then you don&#8217;t have multiple writers to the same logical set of data as an inherent property of your domain. As such, having a single database where all data lives isn&#8217;t really necessary. </p>
<p>Data is inherently partitioned by who owns it.</p>
<p>Let&#8217;s take the online shopping cart as an example. There aren&#8217;t any use cases where users operate on each others&#8217; carts &#8211; ergo, not collaborative, therefore not a good candidate for CQRS. Same goes for user profiles, and tons of other cases.</p>
<p>So why is it that we need a separate tier to run our business logic?</p>
<p>Originally, the application server tier was introduced for improved scalability, but specifically around managing the connection pool to the database. Increasing numbers of clients (when each had its own user/account for connecting to the database) caused problems. Luckily, most web applications side-step this problem &#8211; that is, until someone got the idea that the web server was only supposed to run the UI layer, and the Business Logic layer would be on a separate application server tier.</p>
<p>Rubbish &#8211; see Fowler&#8217;s First Law of Distribution: Don&#8217;t.</p>
<p>Keep it all on one tier. Same goes for smart clients.<br />
No, Silverlight, you don&#8217;t count &#8211; architecturally speaking, you&#8217;re a glorified browser.</p>
<h3>But what about scalability?</h3>
<p>In a non-collaborative domain, where you can horizontally add more database servers to support more users/requests/data at the same time you&#8217;re adding web servers &#8211; there is no real scalability problem (caveat, until you&#8217;re Amazon/Google/Facebook scale). </p>
<p>Database servers can be cheap &#8211; if using MySQL/SQL Express/others. </p>
<h3>But what about the built-in event-log CQRS/ES gives us?</h3>
<p>Architectural gold-plating / stealing from the business.</p>
<p>Who put you in a position to decide that development time and resources should be diverted from short-term business-value-adding features to support a non-functional requirement that the business didn&#8217;t ask for?</p>
<p>If you sat down with them, explaining the long-term value of having an archive of all actions in the system, and they said OK, build this into the system from the beginning, that would be fine. Most people who ask me about CQRS and/or Event Sourcing skip this step.</p>
<p>Finally, you can usually implement this specific requirement with some simple interception and logging. Don&#8217;t over-engineer the solution. If using messaging, you can get this by turning on journaling, or if you want to centralize this archive, NServiceBus can forward all messages to a specific queue. </p>
<p>Don&#8217;t forget that this storage has a cost &#8211; including administration. Nothing is free.</p>
<h3>What about the &#8220;proof of correctness&#8221; in Event Sourcing</h3>
<p>I&#8217;ve heard statements made that when you use the events that flowed into/through your system <b>AS</b> your system&#8217;s data, rather than transforming those events to some other schema (relational or otherwise) and storing the result &#8211; you can prove that your system behaves correctly.</p>
<p>Let me put it this way:</p>
<p>No programming technique used by humans will prevent those same humans from creating bugs.<br />
No testing technique used by humans will prevent those same humans from not catching those bugs.<br />
* Automated tests &#8211; see programming technique.</p>
<p>While having a full archive of all events can allow us to roll the system back to some state, fix a bug, and roll forwards, that assumes that we&#8217;re in a closed system. We have users which are outside the system. If a user made a decision based on data influenced by the bug, there&#8217;s no automated way for us to know that, or correct for it as we roll forwards.</p>
<p>In short, we&#8217;re interested in the business&#8217; behavior &#8211; as composed of user and system behavior. No proof can exist.</p>
<h3>Umm, so where should we use it</h3>
<p>If you&#8217;ve uncovered a scenario where you&#8217;re wondering &#8220;first-one-wins, or last-one-wins&#8221;, that&#8217;s often a good candidate for a place where CQRS <b>could</b> make sense. Then re-read my <a href="http://www.udidahan.com/2010/08/31/race-conditions-dont-exist/">Race Conditions Don&#8217;t Exist post</a>.</p>
<p>Also, CQRS should not be your top-level architectural pattern &#8211; that would be SOA.<br />
CQRS, if used at all, would be used inside a service boundary only.</p>
<p>Given that SOA guides us away from having a given 3rd normal form entity exist in any one service, it is unlikely that the building blocks of your CQRS design will be those kinds of entities. Most 3rd normal form one-to-many and many-to-many relationships simply do not exist when doing SOA and CQRS properly.</p>
<p>Therefore, I&#8217;m sorry to say that most sample application you&#8217;ll see online that show CQRS are architecturally wrong. I&#8217;d also be extremely wary of frameworks that guide you towards an entity-style aggregate root CQRS model.</p>
<h3>In Summary</h3>
<p>So, when should you avoid CQRS?</p>
<p>The answer is most of the time.</p>
<p>Here&#8217;s the strongest indication I can give you to know that you&#8217;re doing CQRS correctly: Your aggregate roots are sagas.</p>
<p>And the biggest caveat &#8211; the above are generalizations, and can&#8217;t necessarily be true for every specific scenario. If you&#8217;re Greg Young, then you probably can (and will) decide on your own on these matters. For everybody else, please take these warnings to heart. There have been far too many clients that have come to me all mixed up with their use CQRS in areas where it wasn&#8217;t warranted.</p>
<p>If you want to know everything you need to know to apply CQRS appropriately, please come to <a href="http://www.udidahan.com/training/#Advanced_Distributed_System_Design">my course</a> &#8211; there is so much unlearning to do first that just can&#8217;t happen via a series of blog posts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2011/04/22/when-to-avoid-cqrs/feed/</wfw:commentRss>
		<slash:comments>34</slash:comments>
		</item>
		<item>
		<title>Integration: How and Where</title>
		<link>http://www.udidahan.com/2011/04/08/integration-how-and-where/</link>
		<comments>http://www.udidahan.com/2011/04/08/integration-how-and-where/#comments</comments>
		<pubDate>Fri, 08 Apr 2011 09:18:41 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[BizTalk]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1428</guid>
		<description><![CDATA[One of the topics that comes up a lot in the context of an Enterprise Service Bus (ESB) is that of integration. Unfortunately, many people take their ideas of reuse and design their integration as being done from a single place &#8211; both logical and physical. That unfortunately creates a bottleneck for all integration activities, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/integration.jpg" alt="integration" title="integration" width="200" height="150" style="float:right; margin-left:10px; margin-bottom:10px;" />One of the topics that comes up a lot in the context of an Enterprise Service Bus (ESB) is that of integration. Unfortunately, many people take their ideas of reuse and design their integration as being done from a single place &#8211; both logical and physical. That unfortunately creates a bottleneck for all integration activities, where some are likely to be higher priority than others.</p>
<h3>The ugly truth</h3>
<p>You don&#8217;t need an ESB for integration.</p>
<p>There. I&#8217;ve said it. </p>
<p>Most of the ESB products on the market, in focusing on integration, are addressing the wrong problem.</p>
<h3>What integration is about</h3>
<p>There are three primary components to integration &#8211; data format translation, protocol bridging, and logic. Let&#8217;s take the Agile &#8220;simplest solution that could possibly work&#8221; motto and apply it to these elements:</p>
<ol>
<li>Data format translation<br/>
<ul>
<li>Use something like <a href="http://www.altova.com/mapforce.html">MapForce</a> (from Altova, the guys behind XMLSpy).</li>
<li>At under $1200 for a dev license and handling mapping to/from XML, EDI, flat files, and relational databases, you can host the resulting mapping in any endpoint, as XSLT or even Java/C# &#8211; no need to have &#8220;the bus&#8221; do this for you.</li>
</ul>
</li>
<li>Protocol bridging<br/>
<ul>
<li>Use something like <a href="http://www.nsoftware.com/subscriptions/">/n software</a></li>
<li>At about $1500 for a dev license, you get solutions for FTP, HTTP, SMTP, POP, IMAP, LDAP, DNS, RSS, SMS, Jabber, SOAP, WebDav, etc and, again &#8211; you can host the DLLs in any endpoint you want. Don&#8217;t need expensive buses for this</li>
</ul>
</li>
<li>Logic<br/><br />
This is all you &#8211; no technology can do this for you. If anything, the most important thing is to get all the ugly mapping and protocol bridging stuff out of the way and let you focus on your logic. This is Single Responsibility Principle (un)common sense.
</li>
</ol>
<h3>Digging deeper into the logic</h3>
<p>The interesting thing about most of the protocol stuff mentioned above is that they&#8217;re inherently <b>unreliable</b> and also their performance at runtime is unknowable due to the total load on the target server.</p>
<p>We wouldn&#8217;t want any of the above situations to cause our integration to &#8220;get stuck&#8221;. As such, it is best we think of our integration logic as a long-running process that manages other endpoints which do the actual protocol bridging and data transformations.</p>
<p>This is one of the areas where NServiceBus, with its <a href="http://www.nservicebus.com/Sagas.aspx">sagas</a> can actually help a lot. Just like the other pieces of integration mentioned above, these sagas can run on any endpoint.</p>
<p>You could alternatively look at other technologies like BizTalk and Business Process Execution Language (BPEL) engines, though many of those are designed to be physically centralized (just like many of the ESBs out there).</p>
<p>By the way, if you do want to use NServiceBus together with BizTalk, Michael Stephenson has published some great white papers on getting the two to work together. His latest is about integrating NServiceBus into BizTalk&#8217;s RFID processes &#8211; <a href="http://social.technet.microsoft.com/wiki/contents/articles/biztalk-rfid-amp-nservicebus.aspx">check it out</a>.</p>
<h3>Putting it all together</h3>
<p>When we take all of these pieces and look at them in a cohesive architecture, here&#8217;s what it can look like:</p>
<p><img src="http://www.udidahan.com/wp-content/uploads/DistributedIntegration.jpg" alt="Distributed Integration" title="Distributed Integration" width="600" height="385" class="alignnone size-full wp-image-1432" /></p>
<p>As you can see, integration is physically distributed across multiple endpoints.</p>
<p>Not only that, but the integration logic is kept separate from the protocol bridging and data transformation, enabling independent versioning of each. Just as important, it makes it <b>much</b> easier to unit test that the integration logic is correct as we don&#8217;t have to simulate the target technologies.</p>
<h3>Costs</h3>
<p>As you can see, you can get integration capabilities just as powerful as if you went with something like BizTalk, but without creating a single point of failure in your architecture. In terms of costs, it&#8217;s also quite a bit cheaper.</p>
<p>For a high availability BizTalk deployment, you&#8217;ll be paying <a href="http://www.microsoft.com/biztalk/en/us/pricing-licensing.aspx">over $40,000 per CPU</a> for the Enterprise Edition, not including the extra $7000 per CPU for SQL Server Standard Edition (clustered). For a clustered 4-CPU mid-size deployment, you&#8217;d be in the area of $200,000.</p>
<p>For the distributed integration solution above, you&#8217;d be paying around $2700 in dev licenses (for /n software and Altova MapForce), and $500 per core for NServiceBus Standard Edition. The reason there&#8217;s per-core licensing for NServiceBus is that in a virtualized environment, you&#8217;ll be provisioning virtual cores to your virtual machines. No reason to pay for a quad-core CPU when all you&#8217;re using is a single core. You can also use any number of cores running NServiceBus Express Edition at no cost, so a mid-size deployment with say 8 cores running Standard Edition, and another 24 cores running Express Edition would cost (with the volume discount) an additional $3800 &#8211; a total cost of $6,500.</p>
<p>BizTalk: $200,000 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NServiceBus: $6,500</p>
<p>&#8216;Nuff said.</p>
<h3>In closing</h3>
<p>We&#8217;ve been bitten by centralized architectures before.</p>
<p>By having our integration distributed, we can version, upgrade, and scale each piece independently.</p>
<p>You&#8217;ve seen how we can use simple, lightweight, and inexpensive technologies to create distributed integration solutions just as powerful and robust as the centralized ESB vendor-offerings out there, but at a tiny fraction of the cost.</p>
<p>Next time the topic of integration is brought up, you&#8217;ll know not to be suckered in by re-branded EAI brokers.</p>
<p><a href="http://www.nservicebus.com/">Learn more about NServiceBus</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2011/04/08/integration-how-and-where/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Bus and Broker Pub/Sub Differences</title>
		<link>http://www.udidahan.com/2011/03/24/bus-and-broker-pubsub-differences/</link>
		<comments>http://www.udidahan.com/2011/03/24/bus-and-broker-pubsub-differences/#comments</comments>
		<pubDate>Thu, 24 Mar 2011 21:22:13 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[BizTalk]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[MSMQ]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1421</guid>
		<description><![CDATA[One of the things which often confuses people using NServiceBus for the first time is that it only allows an endpoint to subscribe to a given event from a single other publishing endpoint. The rule that there can only be a single publisher for a given event type is one of the things that differentiates [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/difference_of_opinion.jpg" alt="differences" title="differences" width="250" height="150" style="float:right; margin-left:10px; margin-bottom:10px;" />One of the things which often confuses people using NServiceBus for the first time is that it only allows an endpoint to subscribe to a given event from a single other publishing endpoint. The rule that there can only be a single publisher for a given event type is one of the things that differentiates buses from brokers, though both obviously allow you to have multiple subscribers.</p>
<h3>Brokers</h3>
<p>Message brokers, more broadly known and used on the Java platform, don&#8217;t come with this constraint. For example, when using ActiveMQ, you can have any number of endpoints come to the broker and publish a message under a given topic. </p>
<p>So where&#8217;s the problem?</p>
<p>It&#8217;s all about accountability.</p>
<p>Let&#8217;s say you&#8217;ve subscribed to a given topic, and have received two events &#8211; one telling you that the price of bananas next week will be $1/kg and another telling you that it&#8217;ll be $2/kg. </p>
<p>Which one is right?</p>
<p>Especially given that those events may have been published by any other endpoint via the broker.</p>
<p>Is it first one wins? Last one wins? How about first one sent vs. first one received? Ditto for last. As a subscriber, can you really be held accountable for having the logic to choose the right one? Shouldn&#8217;t this responsibility have fallen to the publishing side?</p>
<p>This is one of the big drawbacks of the broker, hub and spoke architecture. No responsibility. No single source of truth &#8211; unless everybody&#8217;s going to some central database, in which case &#8211; what&#8217;s the point of all this messaging anyway?</p>
<h3>Buses</h3>
<p>The Bus Architectural Style is all about accountability. If you are going to publish an event, you are accountable for the correctness of the data in that event &#8211; there is no central database that a subscriber can go to &#8220;just in case&#8221;. And the only way that you can be held accountable, is if you have full responsibility &#8211; ergo, you&#8217;re the only one who can publish that type of event.</p>
<p>If you say bananas are going to cost $1/kg next week, that&#8217;s that. Subscribers will not hear from anybody else on that topic.</p>
<p>Now, this is not to say that you can&#8217;t have more than one <b>physical</b> publishing endpoint.</p>
<p>You see, buses differentiate between the logical and the physical. Brokers tend to assume that the physical hub-and-spoke topology is also the logical.</p>
<p>In a bus, while there can only be one <b>logical</b> endpoint publishing a given type of event, that endpoint can be physically scaled out across multiple machines. It is the responsibility of the bus to provide infrastructure facilities to allow for that to happen in such a way that to subscribers, it still appears as if there is really only one publishing endpoint.</p>
<p>The same is true about the subscriber &#8211; one logical subscribing endpoint may be scaled out across multiple machines.</p>
<h3>Product Mix-ups</h3>
<p>Unfortunately, there are many broker-style technologies out there that are being marketed under the banner of the Enterprise Service Bus. While some products have the ability to be deployed in both a centralized and distributed fashion (sometimes called &#8220;federated&#8221; or &#8220;embedded&#8221; mode), many do not enforce the &#8220;single publishing endpoint per event-type&#8221; rule.</p>
<p>Without this constraint, it is just too easy to make mistakes.</p>
<h3>NServiceBus</h3>
<p>By enforcing this constraint, we see the same kind of question appear on the discussion group time and time again:</p>
<p>&#8220;I have an Audit event that I&#8217;d like all of my machines to publish, and have one machine subscribe to them all, but NServiceBus won&#8217;t let me. How do I make NServiceBus support this scenario?&#8221;</p>
<p>And the answer is the same every time:</p>
<p>&#8220;You should have all the machines <b>Send</b> the Audit message (configured to go to the single machine handling that message), and not Publish. It is not an event until its been handled by the endpoint responsible for it.&#8221;</p>
<p>The semantics of the message matter a lot.</p>
<p>When looking at Service-Oriented Architecture, these messages are the contract and, as any lawyer will tell you, contracts need to be explicit and the intentions really need to be spelled out &#8211; otherwise the contract is practically worthless.</p>
<h3>In closing</h3>
<p>Friction is sometimes a good thing &#8211; it prevents us from making mistakes. It keeps cars on the road. And because that&#8217;s not enough friction, we introduce curbs as well.</p>
<p>If you&#8217;re looking for a service bus technology for your next project, check that it&#8217;ll give you the friction that you need to keep everybody safe. Really check what it is that the vendors are offering you &#8211; more often than not, it&#8217;s some ESB lipstick on a broker pig.</p>
<p>To learn more about how NServiceBus supports this kind of publish/subscribe, <a href="http://www.nservicebus.com/PubSub.aspx">click here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2011/03/24/bus-and-broker-pubsub-differences/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Careful with Content-Based Routing</title>
		<link>http://www.udidahan.com/2011/03/20/careful-with-content-based-routing/</link>
		<comments>http://www.udidahan.com/2011/03/20/careful-with-content-based-routing/#comments</comments>
		<pubDate>Sun, 20 Mar 2011 15:32:00 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1416</guid>
		<description><![CDATA[Every once in a while I get clients who ask me why NServiceBus doesn&#8217;t support content-based routing. My answer sometimes surprises them, &#8220;because it is a dangerous pattern that should usually be avoided&#8221;.
Content-Based Routing and ESBs
Since content-based routing often appears on feature lists for various ESBs, many people consider them to be a necessary part [...]]]></description>
			<content:encoded><![CDATA[<p>Every once in a while I get clients who ask me why NServiceBus doesn&#8217;t support content-based routing. My answer sometimes surprises them, &#8220;because it is a dangerous pattern that should usually be avoided&#8221;.</p>
<h2>Content-Based Routing and ESBs</h2>
<p>Since content-based routing often appears on feature lists for various ESBs, many people consider them to be a necessary part of systems built on SOA principles. The pattern also appears in the book Enterprise Integration Patterns, which apparently is also a convincing reason to use it, even though the book <a href="http://www.eaipatterns.com/ContentBasedRouter.html">specifically states</a>:</p>
<blockquote><p>
&#8220;When implementing a Content-Based Router, special caution should be taken to make the routing function easy to maintain as the router can become a point of frequent maintenance.&#8221;
</p></blockquote>
<p>That&#8217;s right &#8211; maintenance nightmare.</p>
<h2>For Example</h2>
<p>Let&#8217;s take a real-world example &#8211; a trading system where users can put orders for stock and for forex (money from other countries). </p>
<p>We&#8217;d start by creating a message that can be sent by a client:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> PlaceOrder : IMessage
{
    <span class="kwrd">public</span> OrderTypeEnum OrderType { get; set; }
    <span class="kwrd">public</span> <span class="kwrd">string</span> Code { get; set; }
    <span class="kwrd">public</span> <span class="kwrd">double</span> Amount { get; set; }
}

<span class="kwrd">public</span> <span class="kwrd">enum</span> OrderTypeEnum
{
    Stock,
    Forex
}</pre>
<p>Clients would send messages by using code like the following:</p>
<pre class="csharpcode">
Bus.Send&lt;PlaceOrder&gt;(m =&gt; {
    m.OrderType = OrderTypeEnum.Stock;
    m.Code = <span class="str">"MSFT"</span>;
    m.Amount = 300;
    m.SetHeader("AccountId", myAccountId);
});</pre>
<p>The header is the way that clients identify themselves in the system.</p>
<p>Let&#8217;s say that the logic for handling the different types of orders is different, but also that we&#8217;d like the logic to be deployed to different endpoints. One reason we might want to do this is so that we can independently scale each of these endpoints. This is where it would appear we&#8217;d need some content-based routing &#8211; having some code that looks at the OrderType property and decides where to route based on its value.</p>
<p>Before we get into solutions, let&#8217;s make this more involved.</p>
<p>Not only do we want to route based on OrderType, but we want to use the account ID in the header to check in our database (or via a web service) if the account belongs to one of our VIP customers, and if so, it should be given higher priority. </p>
<h2>Content-Based Routing and Brokers</h2>
<p>We can see that if we were to go with a content-based routing solution, this would drive our architecture to a hub-and-spoke model where the hub becomes quite large and complex, as well as likely becoming a bottleneck in terms of performance.</p>
<p>This logically centralized place through which all communication flows defines the Broker architectural style &#8211; not that of a Bus. In the Bus architectural style, there is no logical (or physical) hub. You can think of it as a kind of peer-to-peer setup. Just like you wouldn&#8217;t want ethernet getting involved in applicative routing decisions, neither should your bus get involved.</p>
<h2>Business-Topology Mapping Solutions</h2>
<p>Instead, when following the Bus architectural style &#8211; we look at mapping stable business characteristics to bus-level topology. At one level, that would mean defining two different message types for our different types of trades:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">abstract</span> <span class="kwrd">class</span> PlaceOrder
{
    <span class="kwrd">public</span> <span class="kwrd">string</span> Code { get; set; }
    <span class="kwrd">public</span> <span class="kwrd">double</span> Amount { get; set; }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> PlaceStockOrder : PlaceOrder { }

<span class="kwrd">public</span> <span class="kwrd">class</span> PlaceForexOrder : PlaceOrder { }</pre>
<p>Once we have two different message types, then we can configure the client to have those statically sent to different endpoints like this:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">UnicastBusConfig</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">MessageEndpointMappings</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">Messages</span><span class="kwrd">="Messages.PlaceStockOrder, Messages"</span> <span class="attr">Endpoint</span><span class="kwrd">="Stock"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">Messages</span><span class="kwrd">="Messages.PlaceForexOrder, Messages"</span> <span class="attr">Endpoint</span><span class="kwrd">="Forex"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">MessageEndpointMappings</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">UnicastBusConfig</span><span class="kwrd">&gt;</span></pre>
<p>And when it comes to handling our VIP customers, the recommendation would be to have those customers be served by a different set of web servers &#8211; we wouldn&#8217;t want a sudden flux in regular customers stealing all the HTTP connections from our VIP customers. Then we&#8217;d statically configure our VIP front-end to talk to our VIP back-end like this:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">&lt;</span><span class="html">UnicastBusConfig</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">MessageEndpointMappings</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">Messages</span><span class="kwrd">="Messages.PlaceStockOrder, Messages"</span> <span class="attr">Endpoint</span><span class="kwrd">="VIP_Stock"</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">Messages</span><span class="kwrd">="Messages.PlaceForexOrder, Messages"</span> <span class="attr">Endpoint</span><span class="kwrd">="VIP_Forex"</span> <span class="kwrd">/&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">MessageEndpointMappings</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">UnicastBusConfig</span><span class="kwrd">&gt;</span></pre>
<p>And the logic to identify VIP customers would be done in the login screen which, from there, would direct the user to the appropriate web server farm.</p>
<h2>Static vs. Dynamic</h2>
<p>It may appear that the above statically configured solution is less flexible then the afore-mentioned hub-and-spoke content-based routing solution. And that&#8217;s probably correct. But the question is, are the business requirements (better called &#8220;objectives&#8221; in this case) likely to change? If we make the technological solution 10x more flexible than the business needs, but at the cost of maintainability (read time to market), we probably haven&#8217;t used the right tool for the job.</p>
<p>Many times we can find stable business objectives and align the topology of our solution with them. Not all that is dynamic and flexible is necessarily better than that which is static.</p>
<p>I&#8217;d go so far to say that if a solution makes heavy use of content-based routing, it is likely a more fragile solution as implementations of stable business objectives and volatile requirements are mixed up together. </p>
<h2>In Closing</h2>
<p>NServiceBus intentionally does not support content-based routing so as not to make it easy for developers to make architectural blunders that could require full-system rewrites a couple of years down the road.</p>
<p>If you want to learn more about these kinds of architectural principles, I suggest coming to my <a href="http://www.udidahan.com/training/">course</a>. I&#8217;m afraid that New York City is already sold out, but I&#8217;ll be coming back to the US again around October. Stockholm, London, Sydney, and Oslo are now all open for registration.</p>
<p>Hope to see you there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2011/03/20/careful-with-content-based-routing/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Polymorphism and Messaging</title>
		<link>http://www.udidahan.com/2011/01/13/polymorphism-and-messaging/</link>
		<comments>http://www.udidahan.com/2011/01/13/polymorphism-and-messaging/#comments</comments>
		<pubDate>Thu, 13 Jan 2011 08:52:48 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[BizTalk]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1389</guid>
		<description><![CDATA[One of the questions that came up from my NServiceBus &#8211; .NET Service Bus Smackdown post was about the Polymorphic Message Dispatch and Polymorphic Message Routing features. People wanted to know what those are, why they&#8217;re important, and if other technologies (specifically WCF and BizTalk) support them.
Messaging Basics
First of all, when building a system using [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/polymorphism.jpg" alt="polymorphism" title="polymorphism" width="213" height="131" style="float:right; margin-left:10px; margin-bottom:10px;" />One of the questions that came up from my <a href="http://www.udidahan.com/2010/08/04/nservicebus-net-service-bus-smackdown/">NServiceBus &#8211; .NET Service Bus Smackdown post</a> was about the Polymorphic Message Dispatch and Polymorphic Message Routing features. People wanted to know what those are, why they&#8217;re important, and if other technologies (specifically WCF and BizTalk) support them.</p>
<h2>Messaging Basics</h2>
<p>First of all, when building a system using messaging, you don&#8217;t have methods that are invoked on some remote object (a.k.a &#8220;service&#8221;) to which you pass parameters. Instead, you use some generic piece of infrastructure (in the world of Java, this is most commonly a Message Broker) to send a message where a message can be thought of as a serializable class. Here&#8217;s an example of a message:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> UserCreated : IMessage
{
    <span class="kwrd">public</span> Guid UserId { get; set; }
    <span class="kwrd">public</span> <span class="kwrd">string</span> Name { get; set; }
}</pre>
<p>This message would be published using NServiceBus like this:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
bus.Publish&lt;UserCreated&gt;( m =&gt;
{
    m.UserId = Guid.NewGuid();
    m.Name = <span class="str">"John Smith"</span>;
});
</pre>
<p>This can be contrasted with RPC models like WCF where you need to define a &#8220;service&#8221; that has methods on it, where those methods accepts parameters. Sometimes developers try to make this more generic by having a single &#8220;service&#8221; with one method on it named something like &#8220;Process&#8221; where the parameter it accepts is of the type &#8220;object&#8221;, or they introduce generics like this: Process&lt;T&gt;(T message);</p>
<p>This is where Polymorphic Message Dispatch comes in:</p>
<h2>Polymorphic Message Dispatch</h2>
<p>While you can pull of the WCF generics thing, one thing that is more difficult (without writing your own dispatch model) is to have a pipeline of classes which can be invoked based on their relationship to the type passed in. Using NServiceBus, both of the following message handlers will be invoked when UserCreated arrives:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> Persistence : IHandleMessages&lt;UserCreated&gt;
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(UserCreated message) { }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Audit : IHandleMessages&lt;IMessage&gt;
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(IMessage message) { }
}</pre>
<p>Now some might say that WCF, BizTalk, and the .NET Service Bus allow you to do auditing in their own internal pipeline, and that&#8217;s true. The place where this becomes more powerful is when you need to build V2 of your system, and the publisher now publishes a slightly different event &#8211; that a user was created as a part of a campaign, requiring the subscriber to register statistics about the campaign. Of course, this event also means that a user was created. Here&#8217;s how you&#8217;d do that with NServiceBus:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> UserCreatedFromCampaign : UserCreated
{
    <span class="kwrd">public</span> Guid CampaignId { get; set; }
}

<span class="rem">//publisher code</span>
bus.Publish&lt;UserCreatedFromCampaign&gt;( m =&gt;
{
    m.UserId = Guid.NewGuid();
    m.Name = <span class="str">"John Smith"</span>;
    m.CampaignId = theCampaignId;
}

<span class="rem">//subscriber code</span>
<span class="kwrd">public</span> <span class="kwrd">class</span> Statistics : IHandleMessages&lt;UserCreatedFromCampaign&gt;
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(UserCreatedFromCampaign message) { }
}</pre>
<p>The important part is what you don&#8217;t see &#8211; since UserCreatedFromCampaign inherits from UserCrated, the Persistence handler we had from V1 will also be invoked, and so will the Audit handler of course. You don&#8217;t have to make your new code call the old code like you would in a method based dispatch model. This makes sure that the coupling in your service layer code remains constant over time as you grow the functionality of your system.</p>
<p>This was one of the main benefits mentioned by Rackspace in their use of NServiceBus (<a href="http://www.nservicebus.com/Customers.aspx">here</a>):</p>
<blockquote><p>&#8220;The main benefit NServiceBus has brought us so far is developer scalability due to lower coupling and higher consistency in our code.&#8221;</p></blockquote>
<p>But, when looking at the above scenario, we can obviously expect that all sorts of things can happen in relation to campaigns &#8211; it is a separate concern, and thus should be handled by a separate subscriber. And this bring us to&#8230;</p>
<h2>Polymorphic Message Routing</h2>
<p>The challenge that we have here is that we no longer have a hierarchy where something clearly belongs on top of something else. We have users created and activities happening related to campaigns &#8211; that may happen in any combination. By having separate subscribers, we could then introduce new handlers/subscribers to our environment without touching or taking down any of the other subscribers. Here&#8217;s what the subscribers would look like:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">class</span> Persistence : IHandleMessages&lt;UserCreated&gt;
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(UserCreated message) { }
}

<span class="kwrd">public</span> <span class="kwrd">class</span> Statistics : IHandleMessages&lt;CampaignActivityOccurred&gt;
{
    <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(CampaignActivityOccurred message) { }
}</pre>
<p>But if each of the above messages were a class, how could we define a message which inherited from both?</p>
<p>Before answering that, we need to understand why the publisher wouldn&#8217;t just publish both of the above messages. You see, the publisher can&#8217;t make any assumptions about its subscribers &#8211; it could be that one of them has logic that correlates across both of these messages that could end up counting the occurrence as happening twice rather than once, possibly charging the account associated with the campaign twice. Publishing two messages results in two transactions when there really should have been one.</p>
<p>So, here&#8217;s how to define messages so that we can have multiple inheritance:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<pre class="csharpcode">
<span class="kwrd">public</span> <span class="kwrd">interface</span> UserCreated : IMessage
{
    Guid UserId { get; set; }
    <span class="kwrd">string</span> Name { get; set; }
}

<span class="kwrd">public</span> <span class="kwrd">interface</span> CampaignActivityOccurred : IMessage
{
    Guid CampaignId { get; set; }
    Guid ActivityId { get; set; }
}

<span class="kwrd">public</span> <span class="kwrd">interface</span> UserCreatedFromCampaign
                 : UserCreated,
                   CampaignActivityOccurred
{
}</pre>
<p>And when the publisher publishes UserCreatedFromCampaign, the event would be routed to both the UserCreated subscriber and the CampaignActivityOccurred subscriber. The power of this approach is felt as we handle new requirements around purchases made related to a campaign. Now we can have another event which inherits from CampaignActivityOccurred and not have to worry since the existing subscriber will be routed those messages automatically.</p>
<p>Since WCF doesn&#8217;t have publish/subscribe capabilities, we might as well move along.</p>
<p>Not to throw a burning match on an ocean of oil, but REST doesn&#8217;t really support this either.</p>
<h2>Not Content-Based Routing</h2>
<p>This may sound like the <a href="http://www.eaipatterns.com/ContentBasedRouter.html">content-based router pattern from EIP</a> (CBR), but it&#8217;s not. The important difference is that there isn&#8217;t some part of the routing that depends on the structure of the messages. The major drawback of CBR is that it creates a central place in your system that needs to be changed any time *syntactic* changes happen to message structure *in addition to* to changes in the subscribers.</p>
<p>Now, this is where the BizTalk guys would say that &#8220;that&#8217;s why we can do message transformations&#8221;, and then the subscribers wouldn&#8217;t need to be changed. However, can we really know when getting a requirement that the change is syntactic and not semantic? I mean, it&#8217;s quite common that changes to message structures happen together with changes to processing logic. </p>
<p>You may be beginning to get the feeling that more and more logic is being sucked out of the subscribers into some monolithic black hole that is likely going to be unmaintainable and quite slow. </p>
<p>This is one of the main differences between using a bus and a broker &#8211; a bus supports the correct distribution of logic keeping the system loosely coupled; brokers are useful integration engines when you absolutely can&#8217;t change the applications being integrated. Enterprise Application Integration (EAI) brokers don&#8217;t usually make good Enterprise Service Bus (ESB) technology.</p>
<h2>In Closing</h2>
<p>NServiceBus has all sorts of features you didn&#8217;t know you needed until you saw what life could be like when you had them. Most of these features don&#8217;t have snazzy drag-and-drop demos that make people ooh-and-aah and TechEds and PDCs, but they&#8217;re really necessary to avoid finding yourself in yet another big-ball-of-mud code base telling your manager/customer (again) that it would be faster to rebuild the system from scratch than to implement that new requirement in the old one.</p>
<p>Take NServiceBus for a spin and <a href="http://www.nservicebus.com/">see for yourself</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2011/01/13/polymorphism-and-messaging/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>NServiceBus 2.5 Released</title>
		<link>http://www.udidahan.com/2010/12/31/nservicebus-2-5-released/</link>
		<comments>http://www.udidahan.com/2010/12/31/nservicebus-2-5-released/#comments</comments>
		<pubDate>Fri, 31 Dec 2010 16:12:44 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[EDA]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[MSMQ]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[Reliability]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1376</guid>
		<description><![CDATA[Just before we usher in the new year, I&#8217;m happy to announce the release of NServiceBus version 2.5.

Yes, there&#8217;s a new logo, and the website&#8217;s been redesigned.
It&#8217;s been a long time coming &#8211; the previous version (2.0) was released in March.
I&#8217;m really quite excited about this version as it rolls up all the bug fixes [...]]]></description>
			<content:encoded><![CDATA[<p>Just before we usher in the new year, I&#8217;m happy to announce the release of NServiceBus version 2.5.</p>
<p><a href="http://www.NServiceBus.com"><img src="http://www.nservicebus.com/img/nServiceBus_Logo.png" width="330" height="77" style="border:none;" alt="Go to the NServiceBus website" title="Go to the NServiceBus website" /></a></p>
<p>Yes, there&#8217;s a new logo, and the website&#8217;s been redesigned.<br />
It&#8217;s been a long time coming &#8211; the previous version (2.0) was released in March.</p>
<p>I&#8217;m really quite excited about this version as it rolls up all the bug fixes and enhancements that customers have asked for as they ran version 2.0 under the most severe types of production environments. The next thing that is a big deal that many have been asking for is a <strong>licensed</strong> version of NServiceBus &#8211; that is, the ability to purchase a commercial license and receive support.</p>
<p>We all know how managers like having a throat to choke.</p>
<p>And now they&#8217;ll have one &#8211; NServiceBus Ltd is the company that will be providing licensing, services, and support for all customers&#8217; NServiceBus needs. After more than 33,000 downloads and over 1000 developers in the community, the demand has really grown. Who would&#8217;ve thought all this would happen when I started NServiceBus 4 years ago (before it even had a name).</p>
<h3>Why NServiceBus is better than WCF for your distributed systems</h3>
<p>This question comes up repeatedly for people hearing about NServiceBus for the first time.</p>
<p>The answer is simple &#8211; reliability.</p>
<p>A system built with NServiceBus is so much more reliable to all kinds of production conditions than WCF that it&#8217;s hardly a fair comparison at all. While WCF can be configured to provide something kind of close to the same level of reliability, you need to do a fair amount of spelunking through the various options of netMsmqBinding to get it right. </p>
<p>The second reason to use NServiceBus instead of WCF is publish/subscribe.</p>
<p>The ability to make use of events and the observer pattern not just to achieve loose coupling within a single process, but across many processes, machines, and sites. Can you imagine going back to programming without events? Shudder. But that&#8217;s exactly what it&#8217;s like to use WCF in your distributed system. NServiceBus brings you the best of object-oriented programming but in a distributed and reliable infrastructure.</p>
<h3>Don&#8217;t wait any longer</h3>
<p>Take NServiceBus for a spin.</p>
<p>But things may look a bit different after you do&#8230;</p>
<p><img src="http://www.udidahan.com/wp-content/uploads/RedPillBluePill.jpg" alt="RedPillBluePill" title="RedPillBluePill" width="595" height="239" class="alignnone size-full wp-image-1384" /></p>
<p><a href="http://www.NServiceBus.com">http://www.NServiceBus.com</a></p>
<p>And have a happy New Year.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/12/31/nservicebus-2-5-released/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>NServiceBus &#8211; .NET Service Bus Smackdown</title>
		<link>http://www.udidahan.com/2010/08/04/nservicebus-net-service-bus-smackdown/</link>
		<comments>http://www.udidahan.com/2010/08/04/nservicebus-net-service-bus-smackdown/#comments</comments>
		<pubDate>Wed, 04 Aug 2010 11:24:39 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[MSMQ]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1322</guid>
		<description><![CDATA[I get this question quite often: &#8220;what is the difference between NServiceBus and the .NET Service Bus from Microsoft?&#8221; And I&#8217;m afraid the answer is that the two technologies were designed to handle a very different set of problems. 
The .NET Service Bus was designed to bridge internet communications using the cloud to enable a [...]]]></description>
			<content:encoded><![CDATA[<p>I get this question quite often: &#8220;what is the difference between NServiceBus and the .NET Service Bus from Microsoft?&#8221; And I&#8217;m afraid the answer is that the two technologies were designed to handle a very different set of problems. </p>
<p>The .NET Service Bus was designed to bridge internet communications using the cloud to enable a variety of devices to communicate using a WCF-remote-procedure-call type of API. NServicebus was designed to simplify the design of on-premise distributed systems using reliable messaging.</p>
<p>Still, people seem to want a kind of comparison, so here&#8217;s a quick one off-the-top-of-my-head:</p>
<table style="text-align:center; font-size:12px;" border="1">
<tr style="font-weight:bold; font-size:16px;">
<td>Feature</td>
<td>.NET Service Bus</td>
<td>NServiceBus</td>
</tr>
<tr>
<td>Cloud-based messaging</td>
<td>Yes</td>
<td>No</td>
</tr>
<tr>
<td>Internet-spanning communication</td>
<td>Yes</td>
<td>Yes &#8211; via additional gateway process</td>
</tr>
<tr>
<td>Lightweight client support</td>
<td>Yes</td>
<td>Yes &#8211; via exposed WCF endpoint</td>
</tr>
<tr>
<td>Full duplex communication</td>
<td>Yes</td>
<td>Yes &#8211; not including lightweight clients</td>
</tr>
<tr>
<td>Publish / Subscribe support</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>Interop with non-.Net platforms</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>Maximum message size</td>
<td>64 KB</td>
<td>4012 KB</td>
</tr>
<tr>
<td>Long-running stateful processes</td>
<td>Using WF on top</td>
<td>Yes</td>
</tr>
<tr>
<td>On-premise messaging</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Client can send messages if server is offline</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Poison message detection and dispatching</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Poison messages re-processing</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Subscriptions persist after restart</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Polymorphic message dispatch</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Polymorphic message routing</td>
<td>No</td>
<td>Yes</td>
</tr>
<tr>
<td>Message-driven unit testing</td>
<td>No</td>
<td>Yes</td>
</tr>
</table>
<p>This is not meant to be an exhaustive list, or even an accurate representation of the relative strengths and weaknesses of the technologies because, as I said, they&#8217;re designed for different purposes. </p>
<p>One could even plug the .NET Service Bus into NServiceBus instead of its MSMQ transport to get broad reach where required, and then switching back to the on-premise strengths of MSMQ. The pluggability of NServiceBus makes it easy to swap out almost all implementation components like the subscription storage, transport, authorization mechanisms, containers, etc.</p>
<p>For more information on the .NET Service Bus see the <a href="http://www.microsoft.com/windowsazure/appfabric/">Azure AppFabric page</a>. Juval Lowey also has a nice article on it up on MSDN magazine <a href="http://msdn.microsoft.com/en-us/magazine/dd569756.aspx">here</a>.</p>
<p>For more information on NServiceBus see the <a href="http://www.NServiceBus.com">NServiceBus site</a>. Also take a look at the pages giving you comparisons to WCF and BizTalk.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/08/04/nservicebus-net-service-bus-smackdown/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>NServiceBus Presentation Now Online</title>
		<link>http://www.udidahan.com/2010/06/09/nservicebus-presentation-now-online/</link>
		<comments>http://www.udidahan.com/2010/06/09/nservicebus-presentation-now-online/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 21:43:51 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Community]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Presentations]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1298</guid>
		<description><![CDATA[Last April I was in Bergen Norway for some consulting and training and I also gave my first NServiceBus presentation to a user group. I don&#8217;t particularly like giving NServiceBus-specific presentations, preferring to talk about the patterns and concepts of service-based architectures and service buses &#8211; NServiceBus is just an implementation. Ultimately, that&#8217;s what happened [...]]]></description>
			<content:encoded><![CDATA[<p>Last April I was in Bergen Norway for some consulting and training and I also gave my first NServiceBus presentation to a user group. I don&#8217;t particularly like giving NServiceBus-specific presentations, preferring to talk about the patterns and concepts of service-based architectures and service buses &#8211; NServiceBus is just an implementation. Ultimately, that&#8217;s what happened in the presentation &#8211; in the first half (or so) I talked about the theory, and in the second I demonstrated that theory with NServiceBus.</p>
<p>Currently, the video is being graciously hosted by Jon Torresdal on his blog, so let&#8217;s hope that the bandwidth holds up.</p>
<p>Get it <a href="http://blog.torresdal.net/2010/06/08/NNUGPresentationUdiDahanOnNServiceBus.aspx">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/06/09/nservicebus-presentation-now-online/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>ESB Differences Between Java and .NET</title>
		<link>http://www.udidahan.com/2010/03/29/esb-differences-between-java-and-net/</link>
		<comments>http://www.udidahan.com/2010/03/29/esb-differences-between-java-and-net/#comments</comments>
		<pubDate>Mon, 29 Mar 2010 13:53:52 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[BPM]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[MSMQ]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Podcast]]></category>
		<category><![CDATA[Pub/Sub]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1212</guid>
		<description><![CDATA[At QCon London a couple of weeks ago I had a chat with Ross Mason, the founder of Mule &#8211; the open source Java ESB. After a while, I realized that NServiceBus is a bit different from Mule ESB in terms of scope.
While Mule has many features in terms of connectivity and integration, NServiceBus provides [...]]]></description>
			<content:encoded><![CDATA[<p>At QCon London a couple of weeks ago I had a chat with Ross Mason, the founder of <a href="http://www.mulesoft.com/mule-esb-open-source-esb">Mule &#8211; the open source Java ESB</a>. After a while, I realized that NServiceBus is a bit different from Mule ESB in terms of scope.</p>
<p>While Mule has many features in terms of connectivity and integration, NServiceBus provides platform interop only. One could say that this is a product of the different backgrounds I and Ross come from.  </p>
<p>On the other hand, the saga capabilities in NServiceBus for handling long-running processes are considered to be BPM functionality on the Java side of the industry, and as such, Mule does not have them.</p>
<p>In terms of other enterprise features like management and monitoring, Mule is more mature, but NServiceBus holds its own in terms of high availability and actually surpasses Mule with the grid and scale-out capabilities of its distributor.</p>
<p>Anyway, I think it&#8217;s about time each of these parts was explicitly described so that companies already invested in Java ESB tools will know what they&#8217;re getting with NServiceBus.</p>
<p>Until then, I hope this podcast describing the full spectrum of NServiceBus, from top level SOA services to in-the-weeds transaction management, will provide more information about what it is and why you might want to use it:</p>
<p><a href="http://deepfriedbytes.com/deepfriedbytes/podcast/episode-49-getting-the-right-message-about-nservicebus-with-udi-dahan/">Deep Fried Bytes, episode 49 &#8211; Getting the right message about NServiceBus with Udi Dahan</a>.</p>
<p>Comments most welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/03/29/esb-differences-between-java-and-net/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>CQRS Video Online</title>
		<link>http://www.udidahan.com/2010/02/26/cqrs-video-online/</link>
		<comments>http://www.udidahan.com/2010/02/26/cqrs-video-online/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 09:42:45 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[Community]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[Presentations]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1184</guid>
		<description><![CDATA[A couple of weeks ago I gave a talk on Command/Query Responsibility Segregation in London. 
The recording of the talk is online here.
There is one important thing that I didn&#8217;t have enough time to cover, but I want you to keep in mind as you&#8217;re watching this. It is that CQRS is applicable only *within* [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of weeks ago I gave a talk on Command/Query Responsibility Segregation in London. </p>
<p>The recording of the talk is online <a href="http://skillsmatter.com/podcast/open-source-dot-net/udi-dahan-command-query-responsibility-segregation/rl-311">here</a>.</p>
<p>There is one important thing that I didn&#8217;t have enough time to cover, but I want you to keep in mind as you&#8217;re watching this. It is that CQRS is applicable only *within* the context of a single service/BC &#8211; NOT across or between them.</p>
<p>Let me know what you think.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/02/26/cqrs-video-online/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>NServiceBus 2.0 Release Candidate 2 Available</title>
		<link>http://www.udidahan.com/2010/02/01/nservicebus-2-0-release-candidate-2-available/</link>
		<comments>http://www.udidahan.com/2010/02/01/nservicebus-2-0-release-candidate-2-available/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 13:13:21 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[ESB]]></category>
		<category><![CDATA[MSMQ]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Pub/Sub]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1173</guid>
		<description><![CDATA[So it&#8217;s been about 6 months since my last NServiceBus post and since then about 1000 new people have subscribed to this blog so they might not know anything about it. For a bit of history, see the post (from almost exactly a year ago) describing the 1.9 release of NServiceBus here.
What&#8217;s New
The quickly approaching [...]]]></description>
			<content:encoded><![CDATA[<p>So it&#8217;s been about 6 months since my last NServiceBus post and since then about 1000 new people have subscribed to this blog so they might not know anything about it. For a bit of history, see the post (from almost exactly a year ago) describing the 1.9 release of NServiceBus <a href="http://www.udidahan.com/2009/02/07/nservicebus-19/">here</a>.</p>
<h2>What&#8217;s New</h2>
<p>The quickly approaching next release of NServiceBus will be version 2.0 and is a big step from 1.9. After 2 betas and 2 release candidates, this version has had a longer stabilization period than any of the versions so far (1.4-1.9). Many of my clients are already using it in production and are very pleased with it. I&#8217;ve heard similar reports from others in the community (now with over 500 members in the discussion group). There have been almost 10,000 downloads since the version 1.9 release and in every country I visit I meet people using NServiceBus in new and interesting applications.</p>
<p>With my appearance on <a href="http://www.hanselminutes.com/default.aspx?showID=194">Hanselminutes</a>, many in the mainstream .NET industry have started taking a look at NServiceBus. That, and the fact that Microsoft&#8217;s Oslo technology has now taken a very data-driven turn (rather than its original service-oriented direction).</p>
<p>Interestingly enough, I&#8217;ve been hearing more and more reports about people using NServiceBus as a developer-friendly API on top of other technologies. This includes BizTalk and even Neuron. I never thought that people would take the pluggability of NServiceBus that far.</p>
<h2>So, what is NServiceBus?</h2>
<p>Well, it&#8217;s a service bus, y&#8217;know, like an ESB &#8211; just an open-source one.</p>
<p>All kidding aside, in a nutshell, it gives you an easy way to integrate transactional messaging into your applications.</p>
<p>One of the reasons why you might want to do that is so that you don&#8217;t lose messages containing valuable data when IIS recycles your AppDomain, every 15-20 minutes (as I wrote about in <a href="http://msdn.microsoft.com/en-us/magazine/cc663023.aspx">this MSDN magazine article</a>).</p>
<p>There are many other nice things in there, like the ability to unit test your service layers and long-running processes but you can read more about that here&#8230;</p>
<h2>Documentation</h2>
<p>One of the biggest differences to NServiceBus in this release is <b>documentation</b>.</p>
<p>A lot of work has gone into the <a href="http://www.NServiceBus.com">NServiceBus.com</a> site to help developers hit the ground running with NServiceBus, including the more advanced aspects of <a href="http://www.nservicebus.com/Distributor.aspx">transparent scale-out with the distributor</a> and <a href="http://www.nservicebus.com/Gateway.aspx">multi-site communications</a>.</p>
<p>There is still work to be done in this area but feedback so far has been extremely positive (except for some grumblings from certain old-timers saying that if they could figure it out by themselves, well, you know the rest).</p>
<h2>In Closing</h2>
<p>If you&#8217;re building a distributed enterprise .NET system, take 5 minutes, download it, and see transactional publish/subscribe messaging working on your machine without any big heavy-weight middleware.</p>
<p><a href="http://www.NServiceBus.com">www.NServiceBus.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/02/01/nservicebus-2-0-release-candidate-2-available/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Clarified CQRS</title>
		<link>http://www.udidahan.com/2009/12/09/clarified-cqrs/</link>
		<comments>http://www.udidahan.com/2009/12/09/clarified-cqrs/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 14:57:19 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Autonomous Services]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1149</guid>
		<description><![CDATA[
After listening how the community has interpreted Command-Query Responsibility Segregation I think that the time has come for some clarification. Some have been tying it together to Event Sourcing. Most have been overlaying their previous layered architecture assumptions on it. Here I hope to identify CQRS itself, and describe in which places it can connect [...]]]></description>
			<content:encoded><![CDATA[<p><img src="/wp-content/uploads/clarification.png" style="float:right; margin-left:10px; margin-bottom:10px" alt="clarification" title="clarification" /><br />
After listening how the community has interpreted Command-Query Responsibility Segregation I think that the time has come for some clarification. Some have been tying it together to Event Sourcing. Most have been overlaying their previous layered architecture assumptions on it. Here I hope to identify CQRS itself, and describe in which places it can connect to other patterns.</p>
<p><a href="/wp-content/uploads/Clarified_CQRS.pdf">Download as PDF</a> &#8211; this is quite a long post.</p>
<h3>Why CQRS</h3>
<p>Before describing the details of CQRS we need to understand the two main driving forces behind it: collaboration and staleness.</p>
<p>Collaboration refers to circumstances under which multiple actors will be using/modifying the same set of data &#8211; whether or not the intention of the actors is actually to collaborate with each other. There are often rules which indicate which user can perform which kind of modification and modifications that may have been acceptable in one case may not be acceptable in others. We&#8217;ll give some examples shortly. Actors can be human like normal users, or automated like software. </p>
<p>Staleness refers to the fact that in a collaborative environment, once data has been shown to a user, that same data may have been changed by another actor &#8211; it is stale. Almost any system which makes use of a cache is serving stale data &#8211; often for performance reasons. What this means is that we cannot entirely trust our users decisions, as they could have been made based on out-of-date information.</p>
<p>Standard layered architectures don&#8217;t explicitly deal with either of these issues. While putting everything in the same database may be one step in the direction of handling collaboration, staleness is usually exacerbated in those architectures by the use of caches as a performance-improving afterthought.</p>
<h3>A picture for reference</h3>
<p>I&#8217;ve given some talks about CQRS using this diagram to explain it:</p>
<p><img src="/wp-content/uploads/cqrs.png" width="500" height="319" alt="CQRS" title="CQRS" /></p>
<p>The boxes named AC are Autonomous Components. We&#8217;ll describe what makes them autonomous when discussing commands. But before we go into the complicated parts, let&#8217;s start with queries:</p>
<h3>Queries</h3>
<p>If the data we&#8217;re going to be showing users is stale anyway, is it really necessary to go to the master database and get it from there? Why transform those 3rd normal form structures to domain objects if we just want data &#8211; not any rule-preserving behaviors? Why transform those domain objects to DTOs to transfer them across a wire, and who said that wire has to be exactly there? Why transform those DTOs to view model objects?</p>
<p>In short, it looks like we&#8217;re doing a heck of a lot of unnecessary work based on the assumption that reusing code that has already been written will be easier than just solving the problem at hand. Let&#8217;s try a different approach:</p>
<p>How about we create an additional data store whose data can be a bit out of sync with the master database &#8211; I mean, the data we&#8217;re showing the user is stale anyway, so why not reflect in the data store itself. We&#8217;ll come up with an approach later to keep this data store more or less in sync.</p>
<p>Now, what would be the correct structure for this data store? How about just like the view model? One table for each view. Then our client could simply SELECT * FROM MyViewTable (or possibly pass in an ID in a where clause), and bind the result to the screen. That would be just as simple as can be. You could wrap that up with a thin facade if you feel the need, or with stored procedures, or using <a href="http://automapper.codeplex.com/">AutoMapper</a> which can simply map from a data reader to your view model class. The thing is that the view model structures are already wire-friendly, so you don&#8217;t need to transform them to anything else.</p>
<p>You could even consider taking that data store and putting it in your web tier. It&#8217;s just as secure as an in-memory cache in your web tier. Give your web servers SELECT only permissions on those tables and you should be fine.</p>
<h3>Query Data Storage</h3>
<p>While you can use a regular database as your query data store it isn&#8217;t the only option. Consider that the query schema is in essence identical to your view model. You don&#8217;t have any relationships between your various view model classes, so you shouldn&#8217;t need any relationships between the tables in the query data store.</p>
<p>So do you actually need a <i>relational</i> database?</p>
<p>The answer is no, but for all practical purposes and due to organizational inertia, it is probably your best choice (for now).</p>
<h3>Scaling Queries</h3>
<p>Since your queries are now being performed off of a separate data store than your master database, and there is no assumption that the data that&#8217;s being served is 100% up to date, you can easily add more instances of these stores without worrying that they don&#8217;t contain the exact same data. The same mechanism that updates one instance can be used for many instances, as we&#8217;ll see later.</p>
<p>This gives you cheap horizontal scaling for your queries. Also, since your not doing nearly as much transformation, the latency per query goes down as well. Simple code is fast code.</p>
<h3>Data modifications</h3>
<p>Since our users are making decisions based on stale data, we need to be more discerning about which things we let through. Here&#8217;s a scenario explaining why:</p>
<p>Let&#8217;s say we have a customer service representative who is one the phone with a customer. This user is looking at the customer&#8217;s details on the screen and wants to make them a &#8216;preferred&#8217; customer, as well as modifying their address, changing their title from Ms to Mrs, changing their last name, and indicating that they&#8217;re now married. What the user doesn&#8217;t know is that after opening the screen, an event arrived from the billing department indicating that this same customer doesn&#8217;t pay their bills &#8211; they&#8217;re delinquent. At this point, our user submits their changes.</p>
<p>Should we accept their changes?</p>
<p>Well, we should accept some of them, but not the change to &#8216;preferred&#8217;, since the customer is delinquent. But writing those kinds of checks is a pain &#8211; we need to do a diff on the data, infer what the changes mean, which ones are related to each other (name change, title change) and which are separate, identify which data to check against &#8211; not just compared to the data the user retrieved, but compared to the current state in the database, and then reject or accept. </p>
<p>Unfortunately for our users, we tend to reject the whole thing if any part of it is off. At that point, our users have to refresh their screen to get the up-to-date data, and retype in all the previous changes, hoping that this time we won&#8217;t yell at them because of an optimistic concurrency conflict.</p>
<p>As we get larger entities with more fields on them, we also get more actors working with those same entities, and the higher the likelihood that something will touch some attribute of them at any given time, increasing the number of concurrency conflicts. </p>
<p>If only there was some way for our users to provide us with the right level of granularity and intent when modifying data. That&#8217;s what commands are all about.</p>
<h3>Commands</h3>
<p>A core element of CQRS is rethinking the design of the user interface to enable us to capture our users&#8217; intent such that making a customer preferred is a different unit of work for the user than indicating that the customer has moved or that they&#8217;ve gotten married. Using an Excel-like UI for data changes doesn&#8217;t capture intent, as we saw above.</p>
<p>We could even consider allowing our users to submit a new command even before they&#8217;ve received confirmation on the previous one. We could have a little widget on the side showing the user their pending commands, checking them off asynchronously as we receive confirmation from the server, or marking them with an X if they fail. The user could then double-click that failed task to find information about what happened.</p>
<p>Note that the client <i>sends</i> commands to the server &#8211; it doesn&#8217;t publish them. Publishing is reserved for events which state a fact &#8211; that something has happened, and that the publisher has no concern about what receivers of that event do with it.</p>
<h3>Commands and Validation</h3>
<p>In thinking through what could make a command fail, one topic that comes up is validation. Validation is different from business rules in that it states a context-independent fact about a command. Either a command is valid, or it isn&#8217;t. Business rules on the other hand are context dependent.</p>
<p>In the example we saw before, the data our customer service rep submitted was valid, it was only due to the billing event arriving earlier which required the command to be rejected. Had that billing event not arrived, the data would have been accepted.</p>
<p>Even though a command may be valid, there still may be reasons to reject it.</p>
<p>As such, validation can be performed on the client, checking that all fields required for that command are there, number and date ranges are OK, that kind of thing. The server would still validate all commands that arrive, not trusting clients to do the validation.</p>
<h3>Rethinking UIs and commands in light of validation</h3>
<p>The client can make of the query data store when validating commands. For example, before submitting a command that the customer has moved, we can check that the street name exists in the query data store.</p>
<p>At that point, we may rethink the UI and have an auto-completing text box for the street name, thus ensuring that the street name we&#8217;ll pass in the command will be valid. But why not take things a step further? Why not pass in the street ID instead of its name? Have the command represent the street not as a string, but as an ID (int, guid, whatever).</p>
<p>On the server side, the only reason that such a command would fail would be due to concurrency &#8211; that someone had deleted that street and that that hadn&#8217;t been reflected in the query store yet; a fairly exceptional set of circumstances. </p>
<h3>Reasons valid commands fail and what to do about it</h3>
<p>So we&#8217;ve got a well-behaved client that is sending valid commands, yet the server still decides to reject them. Often the circumstances for the rejection are related to other actors changing state relevant to the processing of that command.</p>
<p>In the CRM example above, it is only because the billing event arrived first. But &#8220;first&#8221; could be a millisecond before our command. What if our user pressed the button a millisecond earlier? Should that actually change the <b>business outcome</b>? Shouldn&#8217;t we expect our system to behave the same when observed from the outside?</p>
<p>So, if the billing event arrived second, shouldn&#8217;t that revert preferred customers to regular ones? Not only that, but shouldn&#8217;t the customer be notified of this, like by sending them an email? In which case, why not have this be the behavior for the case where the billing event arrives first? And if we&#8217;ve already got a notification model set up, do we really need to return an error to the customer service rep? I mean, it&#8217;s not like they can do anything about it <b>other than notifying the customer</b>.</p>
<p>So, if we&#8217;re not returning errors to the client (who is already sending us valid commands), maybe all we need to do on the client when sending a command is to tell the user &#8220;thank you, you will receive confirmation via email shortly&#8221;. We don&#8217;t even need the UI widget showing pending commands. </p>
<h3>Commands and Autonomy</h3>
<p>What we see is that in this model, commands don&#8217;t need to be processed immediately &#8211; they can be queued. How fast they get processed is a question of Service-Level Agreement (SLA) and not architecturally significant. This is one of the things that makes that node that processes commands autonomous from a runtime perspective &#8211; we don&#8217;t require an always-on connection to the client.</p>
<p>Also, we shouldn&#8217;t need to access the query store to process commands &#8211; any state that is needed should be managed by the autonomous component &#8211; that&#8217;s part of the meaning of autonomy.</p>
<p>Another part is the issue of failed message processing due to the database being down or hitting a deadlock. There is no reason that such errors should be returned to the client &#8211; we can just rollback and try again. When an administrator brings the database back up, all the message waiting in the queue will then be processed successfully and our users receive confirmation.</p>
<p>The system as a whole is quite a bit more robust to any error conditions.</p>
<p>Also, since we don&#8217;t have queries going through this database any more, the database itself is able to keep more rows/pages in memory which serve commands, improving performance. When both commands and queries were being served off of the same tables, the database server was always juggling rows between the two.</p>
<h3>Autonomous Components</h3>
<p>While in the picture above we see all commands going to the same AC, we could logically have each command processed by a different AC, each with it&#8217;s own queue. That would give us visibility into which queue was the longest, letting us see very easily which part of the system was the bottleneck. While this is interesting for developers, it is critical for system administrators.</p>
<p>Since commands wait in queues, we can now add more processing nodes behind those queues (using the distributor with NServiceBus) so that we&#8217;re only scaling the part of the system that&#8217;s slow. No need to waste servers on any other requests.</p>
<h3>Service Layers</h3>
<p>Our command processing objects in the various autonomous components actually make up our service layer. The reason you don&#8217;t see this layer explicitly represented in CQRS is that it isn&#8217;t really there, at least not as an identifiable logical collection of related objects &#8211; here&#8217;s why:</p>
<p>In the <a href="http://en.wikipedia.org/wiki/Multitier_architecture">layered architecture</a> (AKA 3-Tier) approach, there is no statement about dependencies between objects within a layer, or rather it is implied to be allowed. However, when taking a command-oriented view on the service layer, what we see are objects handling different types of commands. Each command is independent of the other, so why should we allow the objects which handle them to depend on each other?</p>
<p>Dependencies are things which should be avoided, unless there is good reason for them.</p>
<p>Keeping the command handling objects independent of each other will allow us to more easily version our system, one command at a time, not needing even to bring down the entire system, given that the new version is backwards compatible with the previous one.</p>
<p>Therefore, keep each command handler in its own VS project, or possibly even in its own solution, thus guiding developers away from introducing dependencies in the name of reuse (it&#8217;s a <a href="http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/">fallacy</a>). If you do decide <b>as a deployment concern</b>, that you want to put them all in the same process feeding off of the same queue, you can ILMerge those assemblies and host them together, but understand that you will be undoing much of the benefits of your autonomous components.</p>
<h3>Whither the domain model?</h3>
<p>Although in the diagram above you can see the domain model beside the command-processing autonomous components, it&#8217;s actually an implementation detail. There is nothing that states that all commands <i>must</i> be processed by the same domain model. Arguably, you could have some commands be processed by <a href="http://martinfowler.com/eaaCatalog/transactionScript.html">transaction script</a>, others using <a href="http://martinfowler.com/eaaCatalog/tableModule.html">table module</a> (AKA active record), as well as those using the <a href="http://martinfowler.com/eaaCatalog/domainModel.html">domain model</a>. Event-sourcing is another possible implementation.</p>
<p>Another thing to understand about the domain model is that it now isn&#8217;t used to serve queries. So the question is, why do you need to have so many relationships between entities in your domain model?</p>
<p>(You may want to take a second to let that sink in.)</p>
<p>Do we really need a collection of orders on the customer entity? In what command would we need to navigate that collection? In fact, what kind of command would need <i>any</i> one-to-many relationship? And if that&#8217;s the case for one-to-many, many-to-many would definitely be out as well. I mean, most commands only contain one or two IDs in them anyway.</p>
<p>Any aggregate operations that may have been calculated by looping over child entities could be pre-calculated and stored as properties on the parent entity. Following this process across all the entities in our domain would result in isolated entities needing nothing more than a couple of properties for the IDs of their related entities &#8211; &#8220;children&#8221; holding the parent ID, like in databases.</p>
<p>In this form, commands could be entirely processed by a single entity &#8211; viola, an aggregate root that is a consistency boundary.</p>
<h3>Persistence for command processing</h3>
<p>Given that the database used for command processing is not used for querying, and that most (if not all) commands contain the IDs of the rows they&#8217;re going to affect, do we really need to have a column for every single domain object property? What if we just serialized the domain entity and put it into a single column, and had another column containing the ID? This sounds quite similar to key-value storage that is available in the various cloud providers. In which case, would you really need an object-relational mapper to persist to this kind of storage? </p>
<p>You could also pull out an additional property per piece of data where you&#8217;d want the &#8220;database&#8221; to enforce uniqueness. </p>
<p>I&#8217;m not suggesting that you do this in all cases &#8211; rather just trying to get you to rethink some basic assumptions.</p>
<h3>Let me reiterate</h3>
<p>How you process the commands is an implementation detail of CQRS.</p>
<h3>Keeping the query store in sync</h3>
<p>After the command-processing autonomous component has decided to accept a command, modifying its persistent store as needed, it publishes an event notifying the world about it. This event often is the &#8220;past tense&#8221; of the command submitted:</p>
<p>MakeCustomerPerferredCommand -> CustomerHasBeenMadePerferredEvent</p>
<p>The publishing of the event is done transactionally together with the processing of the command and the changes to its database. That way, any kind of failure on commit will result in the event not being sent. This is something that should be handled by default by your message bus, and if you&#8217;re using MSMQ as your underlying transport, requires the use of transactional queues.</p>
<p>The autonomous component which processes those events and updates the query data store is fairly simple, translating from the event structure to the persistent view model structure. I suggest having an event handler per view model class (AKA per table). </p>
<p>Here&#8217;s the picture of all the pieces again:</p>
<p><img src="/wp-content/uploads/cqrs.png" width="500" height="319" alt="CQRS" title="CQRS" /></p>
<h3>Bounded Contexts</h3>
<p>While CQRS touches on many pieces of software architecture, it is still not at the top of the food chain. CQRS if used is employed within a bounded context (DDD) or a business component (SOA) &#8211; a cohesive piece of the problem domain. The events published by one BC are subscribed to by other BCs, each updating their query and command data stores as needed.</p>
<p>UI&#8217;s from the CQRS found in each BC can be &#8220;mashed up&#8221; in a single application, providing users a single composite view on all parts of the problem domain. Composite UI frameworks are very useful for these cases.</p>
<h3>Summary</h3>
<p>CQRS is about coming up with an appropriate architecture for multi-user collaborative applications. It explicitly takes into account factors like data staleness and volatility and exploits those characteristics for creating simpler and more scalable constructs.</p>
<p>One cannot truly enjoy the benefits of CQRS without considering the user-interface, making it capture user intent explicitly. When taking into account client-side validation, command structures may be somewhat adjusted. Thinking through the order in which commands and events are processed can lead to notification patterns which make returning errors unnecessary.</p>
<p>While the result of applying CQRS to a given project is a more maintainable and performant code base, this simplicity and scalability require understanding the detailed business requirements and are not the result of any technical &#8220;best practice&#8221;. If anything, we can see a plethora of approaches to apparently similar problems being used together &#8211; data readers and domain models, one-way messaging and synchronous calls.</p>
<p>Although this blog post is over 3000 words (a record for this blog), I know that it doesn&#8217;t go into enough depth on the topic (it takes about 3 days out of the 5 of my <a href="http://www.udidahan.com/training/">Advanced Distributed Systems Design course</a> to cover everything in enough depth). Still, I hope it has given you the understanding of why CQRS is the way it is and possibly opened your eyes to other ways of looking at the design of distributed systems.</p>
<p>Questions and comments are most welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/12/09/clarified-cqrs/feed/</wfw:commentRss>
		<slash:comments>114</slash:comments>
		</item>
		<item>
		<title>Search and Messaging</title>
		<link>http://www.udidahan.com/2009/11/01/search-and-messaging/</link>
		<comments>http://www.udidahan.com/2009/11/01/search-and-messaging/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 05:33:35 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[Usability]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1134</guid>
		<description><![CDATA[
One question that I get asked about quite a bit with relation to messaging is about search. Isn&#8217;t search inherently request/response? Doesn&#8217;t it have to return immediately? Wouldn&#8217;t messaging in this case hurt our performance?
While I tend to put search in the query camp in the when keeping the responsibility of commands and queries separate, [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/search.png" width="200" height="204" style="float:right; margin-left:10px; margin-bottom:10px;" alt="search" title="search" /><br />
One question that I get asked about quite a bit with relation to messaging is about search. Isn&#8217;t search inherently request/response? Doesn&#8217;t it have to return immediately? Wouldn&#8217;t messaging in this case hurt our performance?</p>
<p>While I tend to put search in the query camp in the when keeping the responsibility of commands and queries separate, and often recommend that those queries be done without messaging, there are certain types of search where messaging does make sense.</p>
<p>In this post, I&#8217;ll describe certain properties of the problem domain that make messaging a good candidate for a solution.</p>
<h3>Searching is besides the point &#8211; Finding is what it&#8217;s all about</h3>
<p>Remember that search is only a means to an end in the eyes of the user &#8211; they want to find something. One of the difficulties we users have is expressing what we want to find in ways that machines can understand.</p>
<p>In thinking about how we build systems to interact with users, we need to take this fuzziness into account. The more data that we have, the less homogeneous it is, the harder this problem becomes.</p>
<p>When talking about speed, while users are sensitive to the technical interactivity, the thing that matters most is the total time it takes for them to find what they want. If the result of each search screen pops up in 100ms, but the user hasn&#8217;t found what they&#8217;re looking for after clicking through 20 screens, the search function is ultimately broken.</p>
<p>Notice that the finding process isn&#8217;t perceived as &#8220;immediate&#8221; in the eyes of the user &#8211; the evaluation they do in their heads of the search results is as much a part of finding as the search itself.</p>
<p>Also, if the user needs to refine their search terms in order to find what they want, we&#8217;re now talking about a multi-request/multi-response process. There is nothing in the problem domain which indicates that finding is inherently request/response.</p>
<h3>Relationships in the data</h3>
<p>When bringing back data as the result of a search, what we&#8217;re saying is that there is a property which is the same across the result elements. But there may be more than one such property. For example, if we search for &#8220;blue&#8221; on Google Images, we get back pictures of the sky, birds, flowers, and more. Obvious so far &#8211; but let&#8217;s exploit the obvious a bit.</p>
<p>When the user sees that too many irrelevant results come back, they&#8217;ll want to refine their search. One way they can do that is to perform a new search and put in a more specific search phrase &#8211; like &#8220;blue sky&#8221;. Another way is for them to indicate this is by selecting an image and saying &#8220;not like this&#8221; or &#8220;more of these&#8221;. Then we can use the additional properties we know about those images to further refine the result group &#8211; either adding more images of one kind, or removing images of another.</p>
<p>Here&#8217;s something else that&#8217;s obvious:</p>
<p>Users often click or change their search before the entire result screen is shown. </p>
<p>It&#8217;s beginning to sound like users are already interacting with search in an asynchronous manner. What if we actually designed a system that played to that kind of interaction model?</p>
<h3>Data-space partitioning</h3>
<p>Once we accept the fact that the user is willing to have more results appear in increments, we can talk about having multiple servers processing the search in parallel. For large data spaces, it is unlikely for us to be able to store all the required meta data for search on one server anyway.</p>
<p>All we really need is a way to index these independent result-sets so that the user can access them. This can be done simply by allocating a GUID/UUID for the search request and storing the result-sets along with that ID.</p>
<h3>Browser interaction</h3>
<p>When the browser calls a server with the search request the first time, that server allocates an ID to that request, returns a URL containing that ID to the browser, and publishes an event containing the search term and the ID. Each of our processing nodes is subscribed to that event, performs the search on its part of the data-space, and writes its results (likely to a distributed cache) along with that ID. </p>
<p>The browser polls the above URL, which queries the cache (give me everything with this ID), and the browser sees which resources have been added since the last time it polled, and shows them to the user.</p>
<p>If the user clicks &#8220;more of these&#8221;, that initiates a new search request to the server, which follows the same pattern as before, just that the system is able to pull more relevant information. When implementing &#8220;not like this&#8221;, this performs a similar search but, instead of adding to the list of items shown, we&#8217;re removing items from the list shown based on the response from the server.</p>
<p>In this kind of user-system interaction model, having the user page through the result set doesn&#8217;t make very much sense as we&#8217;re not capturing the intent of the user, which is &#8220;you&#8217;re not showing me what I want&#8221;. By making it easy for the user to fine tune the result set, we get them closer to finding what they want. By performing work in parallel in a non-blocking manner on smaller sets of data, we greatly decrease the &#8220;time to first byte&#8221; as well as the time when the user can refine their search.</p>
<h3>But Google doesn&#8217;t work like that</h3>
<p>I know that this isn&#8217;t like the search UI we&#8217;ve all grown used to.</p>
<p>But then again, the search that you&#8217;re providing your users is more specific &#8211; not just pages on the web. If you&#8217;re a retailer allowing your users to search for a gift, this kind of &#8220;more like this, less like that&#8221; model is how users would interact with a real sales-person when shopping in a store. Why not model your system after the ways that people behave in the real world?</p>
<h3>In closing</h3>
<p>If we were to try to make use of messaging underneath &#8220;classical&#8221; search interaction models, it probably wouldn&#8217;t have been the greatest fit. If all we&#8217;re doing at a logical level is blocking RPC, then messaging would probably make the system slower. The real power that you get from messaging is being able to technically do things in parallel &#8211; that&#8217;s how it makes things faster. If you can find ways to see that parallelism in your problem domain, not only will messaging make sense technically &#8211; it will really be the only way to build that kind of system.</p>
<p>Learning how to disconnect from seeing the world through the RPC-tinted glasses of our technical past takes time. Focusing on the problem domain, seeing it from the user&#8217;s perspective without any technical constraints &#8211; that&#8217;s the key to finding elegant solutions. More often than not, you&#8217;ll see that the real world is non-blocking and parallel, and then you&#8217;ll be able to make the best use of messaging and other related patterns.</p>
<p>What are your thought? Post a comment and let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/11/01/search-and-messaging/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Progressive .NET Wrap-up</title>
		<link>http://www.udidahan.com/2009/09/07/progressive-net-wrap-up/</link>
		<comments>http://www.udidahan.com/2009/09/07/progressive-net-wrap-up/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 06:06:30 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Pub/Sub]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1103</guid>
		<description><![CDATA[So, I&#8217;ve gotten back from a most enjoyable couple of days in Sweden where I gave two half-day tutorials, the first being the SOA and UI composition talk I gave at the European Virtual ALT.NET meeting (which you can find online here) and the other on DDD in enterprise apps (the first time I&#8217;ve done [...]]]></description>
			<content:encoded><![CDATA[<p>So, I&#8217;ve gotten back from a most enjoyable couple of days in Sweden where I gave two half-day tutorials, the first being the SOA and UI composition talk I gave at the European Virtual ALT.NET meeting (which you can find online <a href="http://www.vimeo.com/5022174">here</a>) and the other on DDD in enterprise apps (the first time I&#8217;ve done this talk).</p>
<p>I&#8217;ve gotten some questions about my DDD presentation there based on <a href="http://codebetter.com/blogs/aaron.jensen/">Aaron Jensen&#8217;s</a> pictures:</p>
<p><img src="http://www.udidahan.com/wp-content/uploads/cqs_udi_dahan_presentation.jpg" alt="cqs_udi_dahan_presentation" title="cqs_udi_dahan_presentation" width="500" height="332" class="alignnone size-full wp-image-1104" /></p>
<p>Yes &#8211; I talk with my hands. All the time.</p>
<p>That slide is quite an important one &#8211; I talked about it for at least 2 hours.</p>
<p>Here it is again, this time in full:</p>
<p><img src="http://www.udidahan.com/wp-content/uploads/cqs.jpg" alt="cqs" title="cqs" width="500" height="374" class="alignnone size-full wp-image-1107" /></p>
<p>You may notice that the nice clean layered abstraction that the industry has gotten so comfortable with doesn&#8217;t quite sit right when looking at it from this perspective. The reason for that is that this perspective takes into account physical distribution while layers don&#8217;t.</p>
<p>I&#8217;ll have some more posts on this topic as well as giving a session in TechEd Europe this November.</p>
<p>Oh &#8211; and please do feel free to already send your questions in.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/09/07/progressive-net-wrap-up/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Hanselminutes on NServiceBus</title>
		<link>http://www.udidahan.com/2009/08/21/hanselminutes-on-nservicebus/</link>
		<comments>http://www.udidahan.com/2009/08/21/hanselminutes-on-nservicebus/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 19:53:11 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Community]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[MSMQ]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1087</guid>
		<description><![CDATA[
Yesterday me and Scott virtually sat down to have a chat about NServiceBus and service buses in general. While we didn&#8217;t get in to many of the more advanced parts, you may find it an interesting introduction to the topic as well as saving yourself the costly mistake of implementing a broker instead of a [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.hanselman.com/blog/images/author.jpg" style="float:right; margin-left:10px; margin-bottom:10px;" /><br />
Yesterday me and Scott virtually sat down to have a chat about NServiceBus and service buses in general. While we didn&#8217;t get in to many of the more advanced parts, you may find it an interesting introduction to the topic as well as saving yourself the costly mistake of implementing a broker instead of a bus (yes &#8211; they&#8217;re actually two different things).</p>
<p><a href="http://www.hanselminutes.com/default.aspx?showID=194">Take a listen.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/08/21/hanselminutes-on-nservicebus/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>A Queue Isn&#8217;t An Implementation Detail</title>
		<link>http://www.udidahan.com/2009/05/25/a-queue-isnt-an-implementation-detail/</link>
		<comments>http://www.udidahan.com/2009/05/25/a-queue-isnt-an-implementation-detail/#comments</comments>
		<pubDate>Mon, 25 May 2009 18:15:01 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[MSMQ]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1015</guid>
		<description><![CDATA[It&#8217;s hard to believe that this continues to pop up even as WCF is reaching its fourth version (emphasis mine):
&#8220;A common complaint is that the first call on a client object takes some disproportionately large amount of time, usually ten seconds or more, while successive calls are instantaneous. There are many reasons why this might [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s hard to believe that this continues to pop up even as WCF is reaching its fourth version (emphasis mine):</p>
<blockquote><p>&#8220;A common complaint is that the first call on a client object takes some disproportionately large amount of time, <b>usually ten seconds or more</b>, while successive calls are instantaneous. There are many reasons why this might happen so <b>there&#8217;s no generic resolution for this problem</b>.&#8221; &#8212; <a href="http://blogs.msdn.com/drnick/archive/2009/05/22/tripping-over-missing-servers.aspx">Nicholas Allen</a></p></blockquote>
<p>The thing is that there <b>IS</b> a generic solution to this problem.</p>
<p>It&#8217;s queued messaging.</p>
<p>The only thing is that you have to give up talking to your services as if they were regular objects &#8211; calling methods on them and expecting a response. In other words, designing a distributed systems isn&#8217;t like designing a regular OO system just with some WCF sprinkled on top.</p>
<p>Even when trying to do fire and forget messaging on top of WCF (void method calls with the OneWay attribute), the underlying channel can still block your thread, as Nick mentioned. </p>
<p>A queue isn&#8217;t an implementation detail.<br />
It&#8217;s the primary architectural abstraction of a distributed system.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/05/25/a-queue-isnt-an-implementation-detail/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Saga Persistence and Event-Driven Architectures</title>
		<link>http://www.udidahan.com/2009/04/20/saga-persistence-and-event-driven-architectures/</link>
		<comments>http://www.udidahan.com/2009/04/20/saga-persistence-and-event-driven-architectures/#comments</comments>
		<pubDate>Mon, 20 Apr 2009 11:50:44 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Autonomous Services]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=992</guid>
		<description><![CDATA[When working with clients, I run into more than a couple of people that have difficulty with event-driven architecture (EDA). Even more people have difficulty understanding what sagas really are, let alone why they need to use them. I&#8217;d go so far to say that many people don&#8217;t realize the importance of how sagas are [...]]]></description>
			<content:encoded><![CDATA[<p><img title="image" style="border-top-width: 0px; display: inline; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 10px 10px; border-right-width: 0px" height="128" alt="image" src="http://www.udidahan.com/wp-content/uploads/saga_persistence.jpg" width="200" align="right" border="0" />When working with clients, I run into more than a couple of people that have difficulty with event-driven architecture (EDA). Even more people have difficulty understanding what sagas really are, let alone why they need to use them. I&#8217;d go so far to say that many people don&#8217;t realize the importance of how sagas are persisted in making it all work (including the Workflow Foundation team).</p>
<h3>The common e-commerce example</h3>
<p>We accept orders, bill the customer, and then ship them the product.</p>
<p>Fairly straight-forward.</p>
<p>Since each part of that process can be quite complex, let&#8217;s have each step be handled by a service:</p>
<p>Sales, Billing, and Shipping. Each of these services will publish an event when it&#8217;s done its part. Sales will publish OrderAccepted containing all the order information &#8211; order Id, customer Id, products, quantities, etc. Billing will publish CustomerBilledForOrder containing the customer Id, order Id, etc. And Shipping will publish OrderShippedToCustomer with its data.</p>
<p>So far, so good. EDA and SOA seem to be providing us some value.</p>
<h3>Where&#8217;s the saga?</h3>
<p>Well, let&#8217;s consider the behavior of the Shipping service. It shouldn&#8217;t ship the order to the customer until it has received the CustomerBilledForOrder event as well as the OrderAccepted event. In other words, Shipping needs to hold on to the state that came in the first event until the second event comes in. And this is exactly what sagas are for.</p>
<p>Let&#8217;s take a look at the saga code that implements this. In order to simplify the sample a bit, I&#8217;ll be omitting the product quantities.</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>    <span class="kwrd">public</span> <span class="kwrd">class</span> ShippingSaga : Saga&lt;ShippingSagaData&gt;,</pre>
<pre><span class="lnum">   2:  </span>        ISagaStartedBy&lt;OrderAccepted&gt;,</pre>
<pre class="alt"><span class="lnum">   3:  </span>        ISagaStartedBy&lt;CustomerBilledForOrder&gt;</pre>
<pre><span class="lnum">   4:  </span>    {</pre>
<pre class="alt"><span class="lnum">   5:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(OrderAccepted message)</pre>
<pre><span class="lnum">   6:  </span>        {</pre>
<pre class="alt"><span class="lnum">   7:  </span>            <span class="kwrd">this</span>.Data.ProductIdsInOrder = message.ProductIdsInOrder;</pre>
<pre><span class="lnum">   8:  </span>        }</pre>
<pre class="alt"><span class="lnum">   9:  </span>&nbsp;</pre>
<pre><span class="lnum">  10:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(CustomerBilledForOrder message)</pre>
<pre class="alt"><span class="lnum">  11:  </span>        {</pre>
<pre><span class="lnum">  12:  </span>             <span class="kwrd">this</span>.Bus.Send&lt;ShipOrderToCustomer&gt;(</pre>
<pre class="alt"><span class="lnum">  13:  </span>                (m =&gt;</pre>
<pre><span class="lnum">  14:  </span>                {</pre>
<pre class="alt"><span class="lnum">  15:  </span>                    m.CustomerId = message.CustomerId;</pre>
<pre><span class="lnum">  16:  </span>                    m.OrderId = message.OrderId;</pre>
<pre class="alt"><span class="lnum">  17:  </span>                    m.ProductIdsInOrder = <span class="kwrd">this</span>.Data.ProductIdsInOrder;</pre>
<pre><span class="lnum">  18:  </span>                }</pre>
<pre class="alt"><span class="lnum">  19:  </span>                ));</pre>
<pre><span class="lnum">  20:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  21:  </span>            <span class="kwrd">this</span>.MarkAsComplete();</pre>
<pre><span class="lnum">  22:  </span>        }</pre>
<pre class="alt"><span class="lnum">  23:  </span>&nbsp;</pre>
<pre><span class="lnum">  24:  </span>        <span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">void</span> Timeout(<span class="kwrd">object</span> state)</pre>
<pre class="alt"><span class="lnum">  25:  </span>        {</pre>
<pre><span class="lnum">  26:  </span>            </pre>
<pre class="alt"><span class="lnum">  27:  </span>        }</pre>
<pre><span class="lnum">  28:  </span>    }</pre>
</div>
<p>First of all, this looks fairly simple and straightforward, which is good.<br/><br />
It&#8217;s also wrong, which is not so good.</p>
<p>One problem we have here is that events may arrive out of order &#8211; first CustomerBilledForOrder, and only then OrderAccepted. What would happen in the above saga in that case? Well, we wouldn&#8217;t end up shipping the products to the customer, and customers tend not to like that (for some reason).</p>
<p>There&#8217;s also another problem here. See if you can spot it as I go through the explanation of ISagaStartedBy&lt;T&gt;.</p>
<h3>Saga start up and correlation</h3>
<p>The &#8220;ISagaStartedBy&lt;T&gt;&#8221; that is implemented for both messages indicates to the infrastructure (NServiceBus) that when a message of that type arrives, if an existing saga instance cannot be found, that a new instance should be started up. Makes sense, doesn&#8217;t it? For a given order, when the OrderAccepted event arrives first, Shipping doesn&#8217;t currently have any sagas handling it, so it starts up a new one. After that, when the CustomerBilledForOrder event arrives for that same order, the event should be handled by the saga instance that handled the first event &#8211; not by a new one.</p>
<p>I&#8217;ll repeat the important part: &#8220;the event should be handled by the saga instance that handled the first event&#8221;.</p>
<p>Since the only information we stored in the saga was the list of products, how would we be able to look up that saga instance when the next event came in containing an order Id, but no saga Id?</p>
<p>OK, so we need to store the order Id from the first event so that when the second event comes along we&#8217;ll be able to find the saga based on that order Id. Not too complicated, but something to keep in mind.</p>
<p>Let&#8217;s look at the updated code:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>    <span class="kwrd">public</span> <span class="kwrd">class</span> ShippingSaga : Saga&lt;ShippingSagaData&gt;,</pre>
<pre><span class="lnum">   2:  </span>        ISagaStartedBy&lt;OrderAccepted&gt;,</pre>
<pre class="alt"><span class="lnum">   3:  </span>        ISagaStartedBy&lt;CustomerBilledForOrder&gt;</pre>
<pre><span class="lnum">   4:  </span>    {</pre>
<pre class="alt"><span class="lnum">   5:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(CustomerBilledForOrder message)</pre>
<pre><span class="lnum">   6:  </span>        {</pre>
<pre class="alt"><span class="lnum">   7:  </span>            <span class="kwrd">this</span>.Data.CustomerHasBeenBilled = <span class="kwrd">true</span>;</pre>
<pre><span class="lnum">   8:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   9:  </span>            <span class="kwrd">this</span>.Data.CustomerId = message.CustomerId;</pre>
<pre><span class="lnum">  10:  </span>            <span class="kwrd">this</span>.Data.OrderId = message.OrderId;</pre>
<pre class="alt"><span class="lnum">  11:  </span>&nbsp;</pre>
<pre><span class="lnum">  12:  </span>            <span class="kwrd">this</span>.CompleteIfPossible();</pre>
<pre class="alt"><span class="lnum">  13:  </span>        }</pre>
<pre><span class="lnum">  14:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  15:  </span>        <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(OrderAccepted message)</pre>
<pre><span class="lnum">  16:  </span>        {</pre>
<pre class="alt"><span class="lnum">  17:  </span>            <span class="kwrd">this</span>.Data.ProductIdsInOrder = message.ProductIdsInOrder;</pre>
<pre><span class="lnum">  18:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  19:  </span>            <span class="kwrd">this</span>.Data.CustomerId = message.CustomerId;</pre>
<pre><span class="lnum">  20:  </span>            <span class="kwrd">this</span>.Data.OrderId = message.OrderId;</pre>
<pre class="alt"><span class="lnum">  21:  </span>&nbsp;</pre>
<pre><span class="lnum">  22:  </span>            <span class="kwrd">this</span>.CompleteIfPossible();</pre>
<pre class="alt"><span class="lnum">  23:  </span>        }</pre>
<pre><span class="lnum">  24:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  25:  </span>        <span class="kwrd">private</span> <span class="kwrd">void</span> CompleteIfPossible()</pre>
<pre><span class="lnum">  26:  </span>        {</pre>
<pre class="alt"><span class="lnum">  27:  </span>            <span class="kwrd">if</span> (<span class="kwrd">this</span>.Data.ProductIdsInOrder != <span class="kwrd">null</span> &amp;&amp; <span class="kwrd">this</span>.Data.CustomerHasBeenBilled)</pre>
<pre><span class="lnum">  28:  </span>            {</pre>
<pre><span class="lnum">  29:  </span>                <span class="kwrd">this</span>.Bus.Send&lt;ShipOrderToCustomer&gt;(</pre>
<pre class="alt"><span class="lnum">  30:  </span>                   (m =&gt;</pre>
<pre><span class="lnum">  31:  </span>                   {</pre>
<pre class="alt"><span class="lnum">  32:  </span>                       m.CustomerId = <span class="kwrd">this</span>.Data.CustomerId;</pre>
<pre><span class="lnum">  33:  </span>                       m.OrderId = <span class="kwrd">this</span>.Data.OrderId;</pre>
<pre class="alt"><span class="lnum">  34:  </span>                       m.ProductIdsInOrder = <span class="kwrd">this</span>.Data.ProductIdsInOrder;</pre>
<pre><span class="lnum">  35:  </span>                   }</pre>
<pre class="alt"><span class="lnum">  36:  </span>                   ));</pre>
<pre><span class="lnum">  37:  </span>                <span class="kwrd">this</span>.MarkAsComplete();</pre>
<pre class="alt"><span class="lnum">  38:  </span>            }</pre>
<pre><span class="lnum">  39:  </span>        }</pre>
<pre class="alt"><span class="lnum">  40:  </span>    }</pre>
</div>
<p>And that brings us to&#8230;</p>
<h3>Saga persistence</h3>
<p>We already saw why Shipping needs to be able to look up its internal sagas using data from the events, but what that means is that simple blob-type persistence of those sagas is out. NServiceBus comes with an NHibernate-based saga persister for exactly this reason, though any persistence mechanism which allows you to query on something other than saga Id would work just as well.</p>
<p>Let&#8217;s take a quick look at the saga data that we&#8217;ll be storing and see how simple it is:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>    <span class="kwrd">public</span> <span class="kwrd">class</span> ShippingSagaData : ISagaEntity</pre>
<pre><span class="lnum">   2:  </span>    {</pre>
<pre class="alt"><span class="lnum">   3:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> Guid Id { get; set; }</pre>
<pre><span class="lnum">   4:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">string</span> Originator { get; set; }</pre>
<pre class="alt"><span class="lnum">   5:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> Guid OrderId { get; set; }</pre>
<pre><span class="lnum">   6:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> Guid CustomerId { get; set; }</pre>
<pre class="alt"><span class="lnum">   7:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> List&lt;Guid&gt; ProductIdsInOrder { get; set; }</pre>
<pre><span class="lnum">   8:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">bool</span> CustomerHasBeenBilled { get; set; }</pre>
<pre class="alt"><span class="lnum">   9:  </span>    }</pre>
</div>
<p>You might have noticed the &#8220;Originator&#8221; property in there and wondered what it is for. First of all, the ISagaEntity interface requires the two properties Id and Originator. Originator is used to store the return address of the message that started the saga. Id is for what you think it&#8217;s for. In this saga, we don&#8217;t need to send any messages back to whoever started the saga, but in many others we do. In those cases, we&#8217;ll often be handling a message from some other endpoint when we want to possibly report some status back to the client that started the process. By storing that client&#8217;s address the first time, we can then &#8220;ReplyToOriginator&#8221; at any point in the process.</p>
<p>The manufacturing sample that comes with <a href="http://www.NServiceBus.com">NServiceBus</a> shows how this works.</p>
<h3>Saga Lookup</h3>
<p>Earlier, we saw the need to search for sagas based on order Id. The way to hook into the infrastructure and perform these lookups is by implementing &#8220;IFindSagas&lt;T&gt;.Using&lt;M&gt;&#8221; where T is the type of the saga data and M is the type of message. In our example, doing this using NHibernate would look like this:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode">
<pre class="alt"><span class="lnum">   1:  </span>    <span class="kwrd">public</span> <span class="kwrd">class</span> ShippingSagaFinder : </pre>
<pre><span class="lnum">   2:  </span>        IFindSagas&lt;ShippingSagaData&gt;.Using&lt;OrderAccepted&gt;,</pre>
<pre class="alt"><span class="lnum">   3:  </span>        IFindSagas&lt;ShippingSagaData&gt;.Using&lt;CustomerBilledForOrder&gt;</pre>
<pre><span class="lnum">   4:  </span>    {</pre>
<pre class="alt"><span class="lnum">   5:  </span>        <span class="kwrd">public</span> ShippingSagaData FindBy(CustomerBilledForOrder message)</pre>
<pre><span class="lnum">   6:  </span>        {</pre>
<pre class="alt"><span class="lnum">   7:  </span>            <span class="kwrd">return</span> FindBy(message.OrderId)</pre>
<pre><span class="lnum">   8:  </span>        }</pre>
<pre class="alt"><span class="lnum">   9:  </span>&nbsp;</pre>
<pre><span class="lnum">  10:  </span>        <span class="kwrd">public</span> ShippingSagaData FindBy(OrderAccepted message)</pre>
<pre class="alt"><span class="lnum">  11:  </span>        {</pre>
<pre><span class="lnum">  12:  </span>            <span class="kwrd">return</span> FindBy(message.OrderId)</pre>
<pre class="alt"><span class="lnum">  13:  </span>        }</pre>
<pre><span class="lnum">  14:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  15:  </span>        <span class="kwrd">private</span> ShippingSagaData FindBy(Guid orderId)</pre>
<pre><span class="lnum">  16:  </span>        {</pre>
<pre class="alt"><span class="lnum">  17:  </span>            <span class="kwrd">return</span> sessionFactory.GetCurrentSession().CreateCriteria(<span class="kwrd">typeof</span>(ShippingSagaData))</pre>
<pre><span class="lnum">  18:  </span>                .Add(Expression.Eq(<span class="str">"OrderId"</span>, orderId))</pre>
<pre class="alt"><span class="lnum">  19:  </span>                .UniqueResult&lt;ShippingSagaData&gt;();</pre>
<pre><span class="lnum">  20:  </span>        }</pre>
<pre class="alt"><span class="lnum">  21:  </span>&nbsp;</pre>
<pre><span class="lnum">  22:  </span>        <span class="kwrd">private</span> ISessionFactory sessionFactory;</pre>
<pre class="alt"><span class="lnum">  23:  </span>&nbsp;</pre>
<pre><span class="lnum">  24:  </span>        <span class="kwrd">public</span> <span class="kwrd">virtual</span> ISessionFactory SessionFactory</pre>
<pre class="alt"><span class="lnum">  25:  </span>        {</pre>
<pre><span class="lnum">  26:  </span>            get { <span class="kwrd">return</span> sessionFactory; }</pre>
<pre class="alt"><span class="lnum">  27:  </span>            set { sessionFactory = <span class="kwrd">value</span>; }</pre>
<pre><span class="lnum">  28:  </span>        }</pre>
<pre class="alt"><span class="lnum">  29:  </span>    }</pre>
</div>
<p>For a performance boost, we&#8217;d probably index our saga data by order Id.</p>
<h3>On concurrency</h3>
<p>Another important note is that for this saga, if both messages were handled in parallel on different machines, the saga could get stuck. The persistence mechanism here needs to prevent this. When using NHibernate over a database with the appropriate isolation level (Repeatable Read &#8211; the default in NServiceBus), this &#8220;just works&#8221;. If/When implementing your own saga persistence mechanism, it is important to understand the kind of concurrency your business logic can live with.</p>
<p>Take a look at Ayende&#8217;s example for <a href="http://ayende.com/Blog/archive/2009/01/23/rhino-dht-concurrency-handling-example-ndash-the-phone-billing-system.aspx">mobile phone billing</a> to get a feeling for what that&#8217;s like.</p>
<h3>Summary</h3>
<p>In almost any event-driven architecture, you&#8217;ll have services correlating multiple events in order to make decisions. The saga pattern is a great fit there, and not at all difficult to implement. You do need to take into account that events may arrive out of order and implement the saga logic accordingly, but it&#8217;s really not that big a deal. Do take the time to think through what data will need to be stored in order for the saga to be fault-tolerant, as well as a persistence mechanism that will allow you to look up that data based on event data.</p>
<p>If you feel like giving this approach a try, but don&#8217;t have an environment handy for this, download <a href="http://www.NServiceBus.com">NServiceBus</a> and take a look at the samples. It&#8217;s really quick and easy to get set up.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/04/20/saga-persistence-and-event-driven-architectures/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Messaging ROI</title>
		<link>http://www.udidahan.com/2009/02/22/messaging-roi/</link>
		<comments>http://www.udidahan.com/2009/02/22/messaging-roi/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 10:12:59 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Scalability]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/02/22/messaging-roi/</guid>
		<description><![CDATA[There&#8217;s been some recent discussion as to the &#8220;cost&#8221; of messaging:
Greg Young asserts: 
&#8220;I believe that this shows there to be a rather negligible cost associated with the use of such a model. There is however a small cost, this cost however I believe only exists when one looks at the system in isolation.&#8221;

Ayende adds [...]]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s been some recent discussion as to the &#8220;cost&#8221; of messaging:</p>
<p>Greg Young <a href="http://codebetter.com/blogs/gregyoung/archive/2009/02/09/cost.aspx">asserts</a>:<a href="http://codebetter.com/blogs/gregyoung/archive/2009/02/09/cost.aspx"><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="79" alt="image" src="http://www.udidahan.com/wp-content/uploads/image54.png" width="79" align="right" border="0"></a> </p>
<blockquote><p>&#8220;I believe that this shows there to be a rather negligible cost associated with the use of such a model. There is however a small cost, this cost however I believe only exists when one looks at the system in isolation.&#8221;</p>
</blockquote>
<p>Ayende adds <a href="http://ayende.com/Blog/archive/2009/02/09/the-cost-of-messaging.aspx">his perspective</a>:<a href="http://ayende.com/Blog/archive/2009/02/09/the-cost-of-messaging.aspx"><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="77" alt="image" src="http://www.udidahan.com/wp-content/uploads/image55.png" width="85" align="right" border="0"></a> </p>
<blockquote><p>&#8220;The cost of messaging, and a very real one, comes when you need to understand the system. In a system where message exchange is the form of communication, it can be significantly harder to understand what is going on.&#8221;</p>
</blockquote>
<p>Of course, both these intelligent fellows are right. The reason for the apparent disparity in viewpoints has to do with which part of the following graph you look at. Ayende zooms in on the left side:</p>
<p><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="225" alt="left graph" src="http://www.udidahan.com/wp-content/uploads/image56.png" width="404" border="0"> </p>
<p>As systems get larger, though, the only way to understand them is by working at higher levels of abstraction. That&#8217;s where messaging really shines, as the incremental complexity remains the same by maintaining the same modularity as before:</p>
<p><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="232" alt="full graph" src="http://www.udidahan.com/wp-content/uploads/image57.png" width="404" border="0"> </p>
<p>In Ayende&#8217;s post, he follows the design I described a while back on using messaging for user management and <a href="http://www.udidahan.com/2007/11/10/asynchronous-high-performance-login-for-web-farms/">login for a high-scale web scenario</a>. In his comments, he agrees with the above stating:</p>
<blockquote><p>&#8220;I certainly think that a similar solution using RPC would be much more complex and likely more brittle.&#8221;</p>
</blockquote>
<p>I feel quite conservative in saying the most enterprise solutions fall on the right side of the intersection in the graph.</p>
<p>That being said, don&#8217;t underestimate the learning curve developers go through with messaging. While the mechanics are similar, the mindset is very different. Think about it like this:<a href="http://www.udidahan.com/wp-content/uploads/image58.png"><img style="border-right: 0px; border-top: 0px; margin: 5px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="100" alt="image" src="http://www.udidahan.com/wp-content/uploads/image-thumb36.png" width="80" align="right" border="0"></a> </p>
<blockquote><p>You&#8217;ve driven a car for years in the US. It&#8217;s practically second nature. Then you fly to the UK, rent a car, and all of a sudden, your brain is in meltdown. (or vice versa for those going from the UK to the US)</p>
</blockquote>
<h3>Summary</h3>
<p>If you are going down the messaging route, please be aware that there are shades of gray there as well. You don&#8217;t <em>have</em> to implement your user management and login the way I outlined in my post if you don&#8217;t require such high levels of scalability, but even lower levels of scalability can benefit from messaging.</p>
<p>Just as there isn&#8217;t a single correct design for non-messaging solutions, the same is true for those using messaging. Finding the right balance is tricky, and critical. </p>
<p>When the code is simple in every part of the system, and the asynchronous interactions are what provide for the necessary complexity the problem domain requires, that&#8217;s when you know you&#8217;ve got it just right.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/02/22/messaging-roi/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Lost Notifications? No Problem.</title>
		<link>http://www.udidahan.com/2008/12/07/lost-notifications-no-problem/</link>
		<comments>http://www.udidahan.com/2008/12/07/lost-notifications-no-problem/#comments</comments>
		<pubDate>Sun, 07 Dec 2008 09:46:05 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Autonomous Services]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[Reliability]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2008/12/07/lost-notifications-no-problem/</guid>
		<description><![CDATA[ One of the most common questions I get on the topic of pub/sub messaging is what happens if a notification is lost. Interestingly enough, there are some who almost entirely write-off this pattern because of this issue, preferring the control of request/response-exception. So, what should be done about lost messages? The short answer is [...]]]></description>
			<content:encoded><![CDATA[<p><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="148" alt="" src="http://www.udidahan.com/wp-content/uploads/image48.png" width="240" align="right" border="0"> One of the most common questions I get on the topic of pub/sub messaging is what happens if a notification is lost. Interestingly enough, there are some who almost entirely write-off this pattern because of this issue, preferring the control of request/response-exception. So, what should be done about lost messages? The short answer is durable messaging. The long answer is design.</p>
<h3>Durable Messaging</h3>
<p>In order to prevent a message from being lost when it is sent from a publisher to a subscriber, the message is written to disk on the publisher side, and then forwarded to the subscriber, where it is also written to disk. This store-and-forward mechanism enables our systems to gracefully recover from either side being temporarily unavailable.</p>
<p>In my <a href="http://msdn.microsoft.com/en-us/magazine/cc663023.aspx">MSDN article on this topic</a>, I outlined some problems with this approach. These problems are exacerbated for publishers. Imagine a publisher with 40 subscribers, publishing 10 messages a second, each containing 1MB of XML. If 10 of the subscribers are unavailable, that&#8217;s 100MB of data being written to the publisher&#8217;s disk every second, 6GB every minute. That&#8217;s liable to bring down a publisher before an administrator brews a cup of coffee.</p>
<p>Publishers have no choice but to throw away messages after a certain period of time.</p>
<h3>Publisher Contracts</h3>
<p>The whole issue of contracts and schema is considered one of the better understand parts of SOA. Unfortunately, the operational aspects of service contracts is hardly ever taken into account.</p>
<p>On top of the schema of the messages a service publishers, additional information is needed in the contract:</p>
<ol>
<li>How big will this message be?
<li>How often will it be published?
<li>How long will this message be stored if a subscriber is unavailable?</li>
</ol>
<p>This first two pieces of information are important for subscribers to do load and capacity planning. The last one is the most important as it dictates the required availability and fault-tolerance characteristic of subscribers.</p>
<h3>For Example</h3>
<p>In the canonical retail scenario, when our sales service accepts an order, it publishes an order accepted event. Other services subscribed to this event include shipping, billing, and business intelligence.</p>
<p>While shipping and billing are highly available and able to keep up with the rate at which orders are accepted, the business intelligence service is not. BI has two main parts to it &#8211; a nightly batch that does the number crunching, and a UI for reporting off of the results of that number crunching. Some even do the reporting in a semi-offline fashion, emailing reports back to the user when they&#8217;re ready.</p>
<p>Furthermore, nobody&#8217;s going to invest in servers for making BI highly available.</p>
<p>And wasn&#8217;t the whole point of this publish/subscribe messaging to keep our services autonomous? That not all services have to have the same level uptime?</p>
<p>Houston, do we have a problem.?</p>
<h3>Data Freshness</h3>
<p>There is a glimmer of light in all this doom and gloom.</p>
<p>Not all services have the same data freshness requirements.</p>
<p>The business intelligence service above doesn&#8217;t need to know about orders the second they&#8217;re accepted. A daily roll-up would be fine, and an hourly roll-up bring us that much closer to &#8220;real time business intelligence&#8221;.</p>
<p>So, while BI is ready to accept the sales message schema, it would like a slightly different contract around it &#8211; less messages per unit of time, more data in each message.</p>
<p>From the operational perspective of the sales service, it would be cost effective to have less &#8220;online&#8221; subscribers. It could even take things a few steps further. Instead of using the regular messaging backbone for transmitting these hourly messages, it could use FTP. The data could even be zipped to take up even less space. Since the total data size is less than the corresponding online stream, is stored on cheaper, large storage, and the number of subscribers for this zipped, hourly update is fairly small, these messages can be kept around far longer.</p>
<p>If you&#8217;ve heard about <a href="http://martinfowler.com/articles/consumerDrivenContracts.html">consumer-driven contracts</a>, this is it.</p>
<p>Note that we&#8217;re still talking about the same logical message schema.</p>
<h3>Summary</h3>
<p>It&#8217;s not that lost notifications aren&#8217;t a problem.</p>
<p><a href="http://en.wikipedia.org/wiki/Tesseract"><img style="margin: 0px 0px 10px 10px" src="http://upload.wikimedia.org/wikipedia/commons/5/55/Tesseract.gif" align="right"></a>
<p>It&#8217;s that they feed the design process in such a way that the resulting service ecosystem is set up in such a way that notifications won&#8217;t get lost. I know that that sounds kind of recursive, but that&#8217;s how it works. Either subscribers take care of their SLA allowing them to process the online stream of events, or they should subscribe to a different pipe (which will have different SLA requirements, but maybe they can deal with those).</p>
<p>It make sense to have multiple pipes for the same logical schema.</p>
<p>It&#8217;s practically a necessity to make pub/sub a feasible solution.</p>
<p>&nbsp;</p>
<hr size="1">
<h3>Related Content</h3>
<blockquote><p><a href="http://msdn.microsoft.com/en-us/magazine/cc663023.aspx">MSDN article on messaging and lost messages</a></p>
<p><a href="http://www.udidahan.com/2008/07/17/durable-messaging-dilemmas/">Durable messaging dilemmas</a></p>
<p><a href="http://www.udidahan.com/2008/10/22/additional-logic-required-for-service-autonomy/">Additional logic required for service autonomy</a></p>
<p><a href="http://www.udidahan.com/2008/11/01/soa-eda-and-cep-a-winning-combo/">More in depth example on events and pub/sub between services</a></p>
<p><a href="http://martinfowler.com/articles/consumerDrivenContracts.html">Consumer-Driven Contracts</a></p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/12/07/lost-notifications-no-problem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Command Query Separation and SOA</title>
		<link>http://www.udidahan.com/2008/08/11/command-query-separation-and-soa/</link>
		<comments>http://www.udidahan.com/2008/08/11/command-query-separation-and-soa/#comments</comments>
		<pubDate>Mon, 11 Aug 2008 13:18:40 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Autonomous Services]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Smart Client]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2008/08/11/command-query-separation-and-soa/</guid>
		<description><![CDATA[One of the common questions I receive from people starting to use nServiceBus is how one-way messaging fits with showing the user a grid (or list) of data. Thinking about publish/subscribe usually just gets them even more confused. Trying to resolve all this with Service Oriented Architecture leaves them wondering &#8211; why bother?

In regular client-server [...]]]></description>
			<content:encoded><![CDATA[<p>One of the common questions I receive from people starting to use nServiceBus is how one-way messaging fits with showing the user a grid (or list) of data. Thinking about publish/subscribe usually just gets them even more confused. Trying to resolve all this with Service Oriented Architecture leaves them wondering &#8211; why bother?</p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="267" alt="client server" src="http://www.udidahan.com/wp-content/uploads/image38.png" width="477" border="0" /></p>
<p>In regular client-server development, the server is responsible for providing the client with all CRUD (create, read, update, and delete) capabilities. However, when users look at data they do not often require it to be up to date to the second (given that they often look at the same screen for several seconds to minutes at a time). As such, retrieving data from the same table as that being used for highly consistent transaction processing creates contention resulting in poor performance for all CRUD actions under higher load.</p>
<h4>A Scalable Solution </h4>
<p>One of the common answers to this question is for the server/service to publish a message when data changes (say, as the result of processing a message) and for clients to subscribe to these messages. When such a notification arrives at a client, the client would cache the data it needs. Then, when the user wants to see a grid of data, that data is already on the client. Of course, this solution doesn&#8217;t work so well for older client machines (like some point of service devices) or if there are millions of rows of data.</p>
<p>The thing is that this solution is one implementation of a more general pattern &#8211; command query separation (CQS).</p>
<h4>Command Query Separation</h4>
<p>Wikipedia <a href="http://en.wikipedia.org/wiki/Command-query_separation">describes</a> CQS as a pattern where &quot;&#8230; every method should either be a <i>command</i> that performs an action, or a <i>query</i> that returns data to the caller, but not both. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.&quot;</p>
<p>Martin Fowler is less strict about the use of CQS <a href="http://martinfowler.com/bliki/CommandQuerySeparation.html">allowing for exceptions</a>: &quot;Popping a stack is a good example of a modifier that modifies state. Meyer correctly says that you can avoid having this method, but it is a useful idiom. So I prefer to follow this principle when I can, but I&#8217;m prepared to break it to get my pop.&quot;</p>
<p>So, how does separating commands from queries and SOA help at all in getting data to and from a UI? The answer is based on Pat Helland&#8217;s thinking as described in his article <a href="http://msdn.microsoft.com/en-us/library/ms954587.aspx">Data on the Inside vs. Data on the Outside</a>.</p>
<h4>Services Cross Boxes </h4>
<p>The biggest lie around SOA is that services run.</p>
<p>Let that sink in a second.</p>
<p>Sure services have runnable components, but that&#8217;s not why they&#8217;re important. </p>
<p>I&#8217;ll skip the <a href="http://www.udidahan.com/first-time-here/#soa">books of background</a> and cut to the chase:</p>
<blockquote><p>Services communicate with each other using publish/subscribe and one-way messaging. Services have components inside them. Inside a service, these components can communicate with each using synchronous RPC, or any other mechanism. Also, <em>these components can reside on different machines</em>.</p>
</blockquote>
<p>This is broader than just scaling out a service. There can be service components running on the client as well as the server.</p>
<h4>SOA &amp; CQS</h4>
<p>Combining these two concepts together, here&#8217;s what comes out:</p>
<p><img src="http://www.nservicebus.com/img/CQS.png" /> </p>
<p>In this solution there are two services that span both client and server &#8211; one in charge of commands (create, update, delete), the other in charge of queries (read). These services communicate only via messages &#8211; one cannot access the database of the other. </p>
<p>The command service publishes messages about changes to data, to which the query service subscribes. When the query service receives such notifications, it saves the data in its own data store which may well have a different schema (optimized for queries like a star schema).</p>
<p>The client component which is in charge of showing grids of data to the user behaves the same as it would in a regular layered/tiered architecture, using synchronous blocking request/response to get its data &#8211; SOA doesn&#8217;t change that.</p>
<h4>Composite Applications </h4>
<p>Although the client side components of both the command and query services are hosted in the same process, they are very much independent of each other. That being said, from an interoperability perspective (the one that most people attribute to SOA), all of the client-side components will likely be developed using the same technology &#8211; although there are already ways to <a href="http://www.udidahan.com/2007/05/28/netjava-interop-is-not-a-reason-for-soa/">host Java code in .NET</a> and vice-versa. </p>
<p>Of course, once we talk about web UI&#8217;s things are a bit different &#8211; but still similar. While web-server-side there may be a level of independence, for browser side inter-component communications we&#8217;re still likely to target javascript. There, I&#8217;ve managed to say something technical supporting mashups and SOA without lying through my teeth.</p>
<p>On the Microsoft side with the recent release of the Composite Application Guidance &amp; Library (pronounced &quot;<a href="http://www.codeplex.com/CompositeWPF">prism</a>&quot;) I hope that more of these principles will be reaching the &quot;smart client&quot;. The command pattern is especially critical in maintaining the separation while enabling communication to still occur so I&#8217;m glad that, as one of the Prism advisors, I was able to simplify that part (<a href="http://codebetter.com/blogs/glenn.block/">Glenn</a> still has nightmares about that rooftop conversation).</p>
<h4>Publish / Subscribe</h4>
<p>In the &quot;scalable solution&quot; section up top I mentioned how publish/subscribe to the smart client is really just one implementation of CQS and SOA. So, how different is it really?</p>
<p><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="241" alt="smart client pub/sub" src="http://www.udidahan.com/wp-content/uploads/image39.png" width="554" border="0" /> </p>
<p>Well, there will probably be a different technology mapping. Instead of a star-schema OLAP product, we might simply store the published data in memory on the client. That is, if you designed your components to be technology agnostic.</p>
<p>In terms of the use of nServiceBus, the same component is going to be subscribing to the same type of message &#8211; all that&#8217;s different is that now every client will be having data pushed to them rather than this occurring server-side only. </p>
<p>You could have the same code deployed differently in the same system &#8211; stronger clients subscribing themselves, weaker ones using a remote server. Web servers would probably be considered stronger clients. This kind of flexible deployment has proven to be extremely valuable for my larger clients. The added benefit of enabling users to work (view data) even while offline (somewhere there&#8217;s no WIFI) is just icing on the cake.</p>
<h4>A Word of Warning</h4>
<p>Once the client starts receiving notifications, and handling those on a background thread (as it should) the code becomes susceptible to deadlocks and data races. Juval does a good job of outlining <a href="http://www.udidahan.com/2008/04/11/wcf-smart-clients-and-deadlocks/">some of those</a> with respect to the use of WCF. Prism <a href="http://www.udidahan.com/2008/06/09/prism-occasionally-connected/">doesn&#8217;t provide any assurances</a> in this area either.</p>
<h4>Summary</h4>
<p>NServiceBus is not designed to be used for any and all types of communication in a given architecture. In the examples above, nServiceBus handles the publish/subscribe but leaves the synchronous RPC to existing solutions like WCF. Not only that, but synchronous RPC does have its place in architecture, just not across service boundaries. In all cases, data is served to users from a store different from that which transaction processing logic uses.</p>
<p>Command Query Separation is not only a good idea at the method/class level but has advantages at the SOA/System level as well &#8211; yet another good idea from 20 years ago that services build upon. Making use of CQS requires understanding your data and its uses &#8211; SOA builds on that by looking into data volatility and the freshness business requirements around it.</p>
<p>Finally, designing the components of your services in such a way that their dependency on technology is limited buys a lot of flexibility in terms of deployment and, consequently, significant performance and scalability gains.</p>
<p>Simple, it is. Easy, it is not.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/08/11/command-query-separation-and-soa/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Scaling Long Running Web Services</title>
		<link>http://www.udidahan.com/2008/07/30/scaling-long-running-web-services/</link>
		<comments>http://www.udidahan.com/2008/07/30/scaling-long-running-web-services/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 12:06:38 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2008/07/30/scaling-long-running-web-services/</guid>
		<description><![CDATA[While I was at TechEd USA I had an attendee, Will, come up and ask me an interesting question about how to handle web service calls that can take a long time to complete. He has a number of these kinds of requests ranging from computationally intensive tasks to those requiring sifting through large amounts [...]]]></description>
			<content:encoded><![CDATA[<p>While I was at TechEd USA I had an attendee, Will, come up and ask me an interesting question about how to handle web service calls that can take a long time to complete. He has a number of these kinds of requests ranging from computationally intensive tasks to those requiring sifting through large amounts of data. What Will was having problems with was preventing too many of these resource-intensive tasks from running concurrently (causing increased memory usage, paging, and eventually the server becoming unavailable). </p>
<p>For comparison later, here&#8217;s a diagram showing the trivial interaction:</p>
<p><a href="http://www.udidahan.com/wp-content/uploads/image30.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="354" alt="image" src="http://www.udidahan.com/wp-content/uploads/image-thumb26.png" width="484" border="0"></a> </p>
<p>One solution that he&#8217;d tried was to set up the web server to throttle those requests and keep a much smaller maximum thread-pool size for that application pool. The unfortunate side effect of that solution was that clients would get &#8220;turned away&#8221; by a not-so-pleasant Connection Refused exception.</p>
<p>Will had been to my <a href="http://www.udidahan.com/2008/06/06/web-scalability-slides-and-code/">web scalability talk</a> and was curious about how I was using queues behind my web services. I&#8217;ve also heard this question from people just getting started with <a href="http://www.nServiceBus.com">nServiceBus</a> when looking at the Web Services Bridge sample. Here&#8217;s the code that&#8217;s in the sample and in just a second I&#8217;ll tell you why you shouldn&#8217;t do this:</p>
<p><!-- code formatted by http://manoli.net/csharpformat/ --></p>
<div class="csharpcode" style="overflow: scroll; width: 95%">
<pre class="alt">[WebMethod]</pre>
<pre><span class="kwrd">public</span> ErrorCodes Process(Command request)</pre>
<pre class="alt">{</pre>
<pre>    <span class="kwrd">object</span> result = ErrorCodes.None;</pre>
<pre class="alt">&nbsp;</pre>
<pre>    IAsyncResult sync = Global.Bus.Send(request).Register(</pre>
<pre class="alt">        <span class="kwrd">delegate</span>(IAsyncResult asyncResult)</pre>
<pre>          {</pre>
<pre class="alt">              CompletionResult completionResult = asyncResult.AsyncState <span class="kwrd">as</span> CompletionResult;</pre>
<pre>              <span class="kwrd">if</span> (completionResult != <span class="kwrd">null</span>)</pre>
<pre class="alt">              {</pre>
<pre>                  result = (ErrorCodes) completionResult.ErrorCode;</pre>
<pre class="alt">              }</pre>
<pre>          },</pre>
<pre class="alt">          <span class="kwrd">null</span></pre>
<pre>          );</pre>
<pre class="alt">&nbsp;</pre>
<pre>    sync.AsyncWaitHandle.WaitOne();</pre>
<pre class="alt">&nbsp;</pre>
<pre>    <span class="kwrd">return</span> (ErrorCodes)result;</pre>
<pre class="alt">}</pre>
</div>
<p>Let me repeat, this is demo-ware. Do not use this in production.</p>
<p>What&#8217;s happening is that in this web service call we&#8217;re putting a message in a queue for some other process/machine to process. When that processing is complete, we&#8217;ll get a message back in our local queue (which you don&#8217;t see) which is correlated to our original request, firing off the callback. We block the web method from completing (using the WaitOne call) thus keeping the HTTP connection to the client open.</p>
<p>The problem here is that we&#8217;re wasting resources (the HTTP connection and the thread) while waiting for a response which, as already mentioned, can take a long time. In B2B or other server to server integration environments there are all sorts of middleware solutions that help us solve these problems, however in Will&#8217;s case browsers needed to interact with this web service. All he had was HTTP.</p>
<h4>HTTP Solutions</h4>
<p>Another attendee who was listening in (sorry I don&#8217;t remember your name) said that he was solving similar problems using polling but that he was having scalability problems as well.</p>
<p>What often surprises my clients when we deal with these same issues is that I <em>do</em> suggest a polling based solution, but one that still uses messaging, and this is what I described to Will:</p>
<p>Since we can&#8217;t actually push a message to a browser over HTTP from our server when processing is complete, the browser itself will be responsible for pulling the response. We still don&#8217;t want to leave costly resources like HTTP connections open a long time, however if the browser is going to polling for a response, we&#8217;ll need some way to correlate those following requests with the original one. What we&#8217;re going to do is use the <a href="http://www.smallmemory.com/almanac/PyaraliEtc98.html">Asynchronous Completion Token</a> pattern, and later I&#8217;ll show how to optimize it for web server technology. </p>
<h4>Basic Polling</h4>
<p><a href="http://www.udidahan.com/wp-content/uploads/image31.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="453" alt="image" src="http://www.udidahan.com/wp-content/uploads/image-thumb27.png" width="574" border="0"></a> </p>
<p>When the browser calls the web service, the web service will generate a Guid, put it in the message that it sends for processing, and return that guid to the browser. When the processing of the message is complete, the result will be written to some kind of database, indexed by that guid. The browser will periodically call another web method, passing in the guid it previously received as a parameter. That web method will check the database for a response using the guid, returning null if no response is there. If the browser receives a null response, it will &#8220;sleep&#8221; a bit and then retry.</p>
<p>One of the problems with this solution is that polling uses up server resources &#8211; both on the web server and our DB; threads, memory, DB connections. A better solution would decrease the resource cost of the polling. Let&#8217;s use the fundamental building blocks of the web to our advantage &#8211; HTTP GET and resources:</p>
<h4>REST-full Polling</h4>
<p>Instead of using a guid to represent the id of the response, let&#8217;s consider the REST principle of &#8220;everything&#8217;s a resource&#8221;. That would mean that the response itself would be a resource. And since every resource has a URI, we might as well use that URI in lieu of the guid. So, instead of our web service returning a guid, let&#8217;s return a URI &#8211; something like:</p>
<p><a href="http://www.acme.com/responses/88ec5359-a5d8-4491-a570-3bfe469f3a64.xml">http://www.acme.com/responses/88ec5359-a5d8-4491-a570-3bfe469f3a64.xml</a></p>
<p>As you can see, the guid is still there. So, what&#8217;s different?</p>
<p><a href="http://www.udidahan.com/wp-content/uploads/image32.png"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="486" alt="image" src="http://www.udidahan.com/wp-content/uploads/image-thumb28.png" width="574" border="0"></a> </p>
<p>What&#8217;s different is that instead of having the processing code write the response to the database, it writes it to a resource. This can be done by writing some XML to a file on the SAN in the case of a webfarm. Also, the browser wouldn&#8217;t need to call a web service to get the response, it would just do an HTTP GET on the URI. If the it gets an HTTP 404, it would sleep and retry as before. The reason that the SAN is needed is that, as the browser polls, it may have its requests arrive at various web servers so the response needs to be accessible from any one of them. </p>
<blockquote>
<p>Just as an aside, it would be better to free the processing node as quickly as possible and have something else write the response to the SAN. That would be done simply by sending a message from the processing node that would be handled by a different node that all it did was write responses to disk.</p>
</blockquote>
<p>The reason that the URI makes a difference is that serving &#8220;static&#8221; resources is something that web servers do <em>extremely efficiently</em> without requiring any managed resources (like ASP.NET threads). That&#8217;s a big deal.</p>
<p>We&#8217;re still using HTTP connections for the polling but that&#8217;s something whose effect can be mitigated to a certain degree.</p>
<h4>Timed REST-full Polling<a href="http://www.udidahan.com/wp-content/uploads/image33.png"><img style="border-right: 0px; border-top: 0px; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="221" alt="image" src="http://www.udidahan.com/wp-content/uploads/image-thumb29.png" width="134" align="right" border="0"></a> </h4>
<p>Since various requests can take varying amounts of time to process, it&#8217;s difficult to know at what rate the browser should poll. So, why don&#8217;t we have the web service <em>tell it</em>. As a part of the response to the original web service call, instead of just returning a URI, we could also return the polling interval &#8211; 1 second, 5 seconds, whatever is appropriate for the type of request. This value could easily be configurable [RequestType, PollingInterval].</p>
<p>An even more advanced solution would allow you to change these values dynamically. The advantage that would be gained would be that your operations team could better manage the load on your servers. When a large number of users are hitting your system, you could decrease the rate at which your servers would be polled, thus leaving more HTTP connections for other users. </p>
<h4>Scaling and Adaptive Polling</h4>
<p>You&#8217;d probably also want to scale out the number of processing nodes behind your queue. The nice thing is that you could change the polling interval as you scale the various processing nodes per request type providing better responsiveness for the more critical requests. Once we add virtualization, things get really fun:</p>
<p>We had separate queues per request type, so that we could easily see the load we were under for each type of request. That way, we could scale out the processing nodes per request type as well as change the polling interval. By virtualizing our processing nodes, and writing scripts to monitor queue sizes, we had those scripts automatically provisioning (and de-provisioning) nodes as well as changing the polling interval of the browsers.</p>
<p>This had the enormous benefit of the system automatically shifting resources to provide the appropriate relative allocation for the current load as its macroscopic make-up changed.</p>
<h4>Summary</h4>
<p>Will was well-pleased with the solution which, although more complicated than what he had originally tried, was flexible enough to meet his needs. As opposed to pure server-based solutions, here we make more use of the browser (writing our own Javascript) instead of putting our faith in some Ajax-y library. That&#8217;s not to say that you couldn&#8217;t wrap this up into a library &#8211; in essence, it is a kind of messaging transport for browser to server communication allowing duplex conversations.</p>
<p>In fact, what could be done is to return multiple responses to the browser over a long period of time. In the response that comes back to the browser could be an additional URI where the next response will be. This can be used for reporting the status of a long running process, paging results, and in many other scenarios.</p>
<p>And, one parting thought, could this not be used for all browser to web service communication?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/07/30/scaling-long-running-web-services/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
		<item>
		<title>Durable Messaging Dilemmas</title>
		<link>http://www.udidahan.com/2008/07/17/durable-messaging-dilemmas/</link>
		<comments>http://www.udidahan.com/2008/07/17/durable-messaging-dilemmas/#comments</comments>
		<pubDate>Thu, 17 Jul 2008 22:18:47 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Availability]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Reliability]]></category>
		<category><![CDATA[Scalability]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2008/07/17/durable-messaging-dilemmas/</guid>
		<description><![CDATA[I&#8217;ve received some great feedback on my MSDN article and some really great questions that I think more people are wondering about, so I think I&#8217;ll try to do a post per question and see how that goes.
Libor asks:
&#8220;Would you recommend using durable messaging for systems where there are similar requirements with respect to data [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve received some great feedback on my <a href="http://msdn.microsoft.com/en-us/magazine/cc663023.aspx">MSDN article</a> and some really great questions that I think more people are wondering about, so I think I&#8217;ll try to do a post per question and see how that goes.<img style="margin: 10px 0px 10px 10px" height="175" src="http://www.ewashtenaw.org/government/departments/cmhpsm/provider_information/Provider%20Training%20Resources/images/scales_of_justice.jpg" width="143" align="right"></p>
<p>Libor asks:</p>
<blockquote><p>&#8220;Would you recommend using durable messaging for systems where there are similar requirements with respect to data reliability as you had – ie. not losing any messages? If so, then why didn&#8217;t the final version of your solution use it? If not, can you explain why?&#8221;</p>
</blockquote>
<p>The answer is, as always, it depends, but here&#8217;s on what it depends:</p>
<p>When designing a system, we need to take a good, hard look at how we manage state, and what properties that state has. In a system of reasonable size we can expect various families of state with respect to their business value, data volatility, and fault-tolerance window. Each family needs to be treated differently. While durable messaging may be suitable for one, it may be overkill or underkill for another.</p>
<p>So, here&#8217;s what we&#8217;re going to be looking at:</p>
<ol>
<li>Business Value</li>
<li>Data Volatility</li>
<li>Fault-Tolerance Window</li>
</ol>
<h4>Business Value</h4>
<p>When talking about business value, I want to talk about what it means &#8220;not losing any messages&#8221;. The question is under what conditions will the messages not be lost, or rather, what are the threshold conditions where messages may start getting lost. If all our datacenters are nuked, we will lose data. It&#8217;s likely the business is OK with that (as much as can be expected under those circumstances). If a single server goes down, it&#8217;s likely the business would not be OK with losing messages containing financial data. However if a message requesting the health of a server were to get lost under those same conditions, that would probably be alright. In other words, what does that message represent in business terms.</p>
<h4>Data Volatility</h4>
<p><img style="margin: 0px 10px 0px 0px" height="150" src="http://www.classicdriver.com/upload/images/_de/3161/img02.jpg" width="270" align="left">Data volatility also has an impact. Let&#8217;s say that we&#8217;re building a financial trading system. The time that it takes us to respond to an event (message) that the cost of a certain financial instrument has changed, and the message that we send requesting to buy that security is critical. Let&#8217;s say that has to be done in under 10ms. Now, some failure has occurred preventing our message from reaching its destination for 20ms. What should we do with that message? Should we keep it around, making sure it doesn&#8217;t get lost? Not in this domain. On the contrary, that message should be thrown away as its &#8220;business lifetime&#8221; has been exceeded. Furthermore, even during that original period of 10ms, the use of durable messaging may make it close to impossible to maintain our response times.</p>
<h4>Fault-Tolerance Window</h4>
<p>These two topics feed into the third and more architectural one &#8211; fault-tolerance window: what period of time do we require fault tolerance, and with respect to how many (and what kind of) faults? This will lead us into an analysis of to how many machines do we need to copy a message before we release the calling thread. We&#8217;d also look at in which datacenters those machines reside. This will also impact (or be impacted by) the kinds of links we have to these datacenters if we want to maintain response times. These numbers will need to change when the system identifies a disaster &#8211; degrading itself to a lower level of fault-tolerance after a hurricane knocks out a datacenter, and returning to normal once it comes back up.</p>
<h4>Re-Evaluating Durable Messaging</h4>
<p>Durable messaging may be used at various points in each part of the solution, but we need to look at message size, the rate those messages are being written to disk, how fast the disk is, how much available disk we have (so we don&#8217;t make things worse in the case of degraded service), etc. Companies like Amazon also take into account disk failure rates, replacement rates (disks aren&#8217;t replaced <em>immediately</em> you know), and many other factors when making these decisions<img style="margin: 10px 0px 10px 10px;" height="231" alt="image" src="http://udidahan.weblogs.us/wp-content/uploads/image-thumb25.png" width="143" align="right" border="0"> </p>
<h4>Summary</h4>
<p>Our job as architects when designing the system is to find that cost-benefit balance for the various parts of the system according to these very applicative parameters. No, it&#8217;s not easy. No, cloud computing will not magically solve all of this for us. But, we are getting more technical tools to work with, operations staff is getting better at working with us in the design phase, and our thought processes more rigorous in dealing with the scary conditions of the real world. </p>
<p>To your question, Libor, as to why we didn&#8217;t eventually use durable messaging in our solution, the answer is that we solved the overall state management problem by setting up an applicative protocol with our partners which was resilient in the face of faults by using idempotent messages that could be resent as many times as necessary. You can read more about it <a href="http://udidahan.weblogs.us/2008/04/10/scalability-article-up-on-infoq/">here</a>. This solution isn&#8217;t viable for other kinds of interactions but was just what we needed to get the job done.</p>
<p>Hope that helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/07/17/durable-messaging-dilemmas/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>[Podcast] Highly Scalable Web Architectures</title>
		<link>http://www.udidahan.com/2008/06/19/podcast-highly-scalable-web-architectures/</link>
		<comments>http://www.udidahan.com/2008/06/19/podcast-highly-scalable-web-architectures/#comments</comments>
		<pubDate>Thu, 19 Jun 2008 20:42:34 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2008/06/19/podcast-highly-scalable-web-architectures/</guid>
		<description><![CDATA[For those people who couldn&#8217;t come to TechEd USA and didn&#8217;t see my talks on how to build highly scalable web architectures, you&#8217;re in luck &#8211; Craig, the man behind the Polymorphic Podcast sat down with me and we chatted about what the problems, common solutions, and effective tactics there are in this space. For [...]]]></description>
			<content:encoded><![CDATA[<p>For those people who couldn&#8217;t come to TechEd USA and didn&#8217;t see my talks on how to build highly scalable web architectures, you&#8217;re in luck &#8211; Craig, the man behind the <a href="http://polymorphicpodcast.com">Polymorphic Podcast</a> sat down with me and we chatted about what the problems, common solutions, and effective tactics there are in this space. For those of you who <em>were</em> at TechEd and still <em>didn&#8217;t</em> come to my talk &#8211; <em>what were you thinking?!</em></p>
<p> <img src='http://www.udidahan.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://polymorphicpodcast.com/shows/scaletheweb/">Check it out.</a></p>
<p>Some of this stuff is a bit counter-intuitive (and not readily supported by the tools available in Visual Studio) so please, do feel free to ask questions (in the comments below).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/06/19/podcast-highly-scalable-web-architectures/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NServiceBus Performance</title>
		<link>http://www.udidahan.com/2008/05/21/nservicebus-performance/</link>
		<comments>http://www.udidahan.com/2008/05/21/nservicebus-performance/#comments</comments>
		<pubDate>Wed, 21 May 2008 07:08:05 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[MSMQ]]></category>
		<category><![CDATA[Messaging]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Scalability]]></category>

		<guid isPermaLink="false">http://udidahan.weblogs.us/2008/05/21/nservicebus-performance/</guid>
		<description><![CDATA[I&#8217;ve gotten this question several times already but now companies are beginning to look for performance comparisons in making decisions around the use of nServiceBus. It&#8217;s often compared to straight WCF, BizTalk, and now Neuron ESB. In Sam&#8217;s recent post he posts to a case study of Neuron doing 28 million messages an hour. That&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve gotten this question several times already but now companies are beginning to look for performance comparisons in making decisions around the use of nServiceBus. It&#8217;s often compared to straight WCF, BizTalk, and now Neuron ESB. In Sam&#8217;s <a href="http://samgentile.com/blogs/samgentile/archive/2008/05/19/new-and-notable-243.aspx">recent post</a> he posts to a case study of Neuron doing 28 million messages an hour. That&#8217;s far more than I&#8217;ve ever heard quoted for BizTalk.</p>
<h3>Disclaimer</h3>
<p>Before giving some numbers, please keep in mind that high performance of system infrastructure does not necessarily by itself mean that the system above it is running that fast. For instance, you may have server heartbeats running really quickly but the time it takes to save a purchase order borders on a minute. So, please, take all benchmarks with a grain of salt, or two, or a whole shaker-full.</p>
<p>While I&#8217;m not at liberty to say on which specific domain/company these numbers were measured, I can say that we had the full gamut of &#8220;stateless services&#8221;, statefull services (sagas), number crunching, large data sets, many users, complex visualization, etc. Also, this wasn&#8217;t the largest installation of nServiceBus that I&#8217;m aware of, but its the one I have the most specific numbers for.</p>
<h3>Setup</h3>
<p>OK, so using the default nServiceBus distribution using MSMQ, on servers where the queue files themselves were on separate SCSI RAID disks, we were pumping around 1000 durable, transactionally processed messages per second, per server. That means that similar to the Neuron case, no messages would be lost in the case of a single fault per server per window (time to replace a failed disk set at 3 hours from failure, through detection, to replacement per site &#8211; but that&#8217;s more an operational staffing concern, not the technology itself). </p>
<p>So, that&#8217;s 3.6 million messages per hour per server, at full load. We had a total of 98 servers doing these kinds of processing, not including web servers, databases, etc. Keep in mind that web servers would be communicating with other servers using nServiceBus, but that would maybe be an unfair comparison to the Neuron numbers.</p>
<h3>Server Breakdown</h3>
<p>Anyway, the 48 number crunching servers (blade centers) we had were at full load, so we were pumping more than 170 million messages there. Keep in mind that those servers had a really fast backbone so weren&#8217;t held up by IO. Your environment may be different.</p>
<p>Another 30 (regular pizza boxes) were doing our sagas. Saga state was stored in a distributed in-memory &#8220;cache&#8221;, so once again IO wasn&#8217;t an issue for processing those messages. We were at about 70% utilization there, coming to just over 100 million messages an hour.</p>
<p>The last 20 were clustered boxes (fairly expensive) that handled the various nServiceBus distributor and timeout manager processes were at full load since they handled control messages for all the servers as well as dynamically routing the load. However, on those boxes we used much higher performance disks for the messages, since they had to feed everything else, capable of doing, on average, around 5000 messages a second. That adds up to 360 million messages an hour.</p>
<h3>Unnecessary Durability</h3>
<p>Later, we moved a bunch of messages that didn&#8217;t need all that durability and transactionality off the disks, pushing the total throughput over 1 billion messages an hour. That was about 100 million per hour durable, 900 million per hour non-durable. You can guess that we were left with plenty of IO to spare at that point while we weren&#8217;t yet pushing the limit of our memory.</p>
<p>One thing that&#8217;s important to understand is the size of the messages that didn&#8217;t require durability was less than 1MB, with most weighing in under 10KB. Also, since most of those messages were published, less state management was required around them, enabling us to further improve performance.</p>
<h3>Summary</h3>
<p>NServiceBus didn&#8217;t give us all that by itself. It was the result of skilled architects, developers, and operations staff working together for many iterations, deploying, monitoring, re-designing, etc. You need to understand your technology, your hardware, and your specific performance, availability, and fault-tolerance requirements if you want to get anywhere.</p>
<p>There&#8217;s no magic.</p>
<p>I didn&#8217;t see the number or kinds of servers involved in the Neuron case study so this wasn&#8217;t ever really a comparison. Nor or we talking about the same system here. </p>
<p>So, please, don&#8217;t base your decisions on arbitrary numbers. Spend some time setting up a scaled down version of your target architecture with all the relevant technologies and <em>measure</em>. Be aware that you want high performance end to end, not just of the messaging part. At times, it makes sense to actively throw away messages (of the non-durable, published kind) to help a server come online faster especially after a restart.</p>
<p>Thus ends the tale of another &#8220;benchmark&#8221;.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2008/05/21/nservicebus-performance/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

