<?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; Architecture</title>
	<atom:link href="http://www.udidahan.com/category/architecture/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.udidahan.com</link>
	<description>Enterprise Development Expert &#38; SOA Specialist</description>
	<lastBuildDate>Tue, 31 Aug 2010 09:56:46 +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>Race Conditions Don&#8217;t Exist</title>
		<link>http://www.udidahan.com/2010/08/31/race-conditions-dont-exist/</link>
		<comments>http://www.udidahan.com/2010/08/31/race-conditions-dont-exist/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 09:56:46 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[BPM]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[Workflow]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1335</guid>
		<description><![CDATA[Not in the business world anyway.
The problem is that, as software developers, we&#8217;re all too quick to accept them at face value. We don&#8217;t question the requirements &#8211; in all fairness, it was never our job to do so. We were the ones that implemented them, preferably quickly. 
For example
Let&#8217;s say we get the requirement [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/crossing-the-finish-line1.jpg" alt="crossing-the-finish-line" title="crossing-the-finish-line" width="200" height="159" style="float:right; margin-left:10px; margin-bottom:10px;"  />Not in the business world anyway.</p>
<p>The problem is that, as software developers, we&#8217;re all too quick to accept them at face value. We don&#8217;t question the requirements &#8211; in all fairness, it was never our job to do so. We were the ones that implemented them, preferably quickly. </p>
<h3>For example</h3>
<p>Let&#8217;s say we get the requirement the following requirements:</p>
<p>1. If the order was already shipped, don&#8217;t let the user cancel the order.<br />
2. If the order was already cancelled, don&#8217;t let the user ship the order.</p>
<p>The race condition here is when we have two users who are looking at the same order, which is neither cancelled nor shipped yet, and each submits a command &#8211; one to ship the order, the other to cancel it.</p>
<p>In these cases, the code is simple &#8211; just an if statement before performing the relevant command.</p>
<h3>So what&#8217;s the problem</h3>
<p>A microsecond difference in timing shouldn&#8217;t make a difference to core business behaviors. Which means that we&#8217;ve actually got here is a bug in the requirements. Users are actually dictating solutions here rather than requirements.</p>
<p>Let&#8217;s ask our stakeholders, &#8220;why shouldn&#8217;t we let users cancel a shipped order? I mean, the users don&#8217;t want the products.&#8221;</p>
<p>And the stakeholders would respond with something like, &#8220;well, we don&#8217;t want to refund the user&#8217;s money then. Or, at least, not all their money. Well, maybe if they return the products in their original packaging, *then* we could give a full refund.&#8221;</p>
<p>And as we drilled deeper, &#8220;when do refunds need to be given? Right away, in the same transaction?&#8221;</p>
<p>The stakeholders would explain, &#8220;no, refunds don&#8217;t need to be given right away.&#8221;</p>
<p>It turns out we were missing the concept of a refund, as well as assuming that all things needed to be processed and enforced immediately. Once we dug into the requirements, we found that there is actually plenty of time to allow both transactions to go through. We just need to add some checks during shipping&#8217;s long-running process to see if the order was cancelled, and then to cut the process short.</p>
<h3>So is everything a long-running process then?</h3>
<p>That&#8217;s actually a fair question &#8211; long-running processes are a lot more common than at first appears.</p>
<p>What we&#8217;re seeing is that cancellation is now a command that has no reason to fail &#8211; just like <a href="http://www.udidahan.com/2009/12/09/clarified-cqrs">CQRS</a> tells us. When this command is performed, it publishes the OrderCancelled event, which the billing service subscribes to. </p>
<p>Billing then starts a long-running process (a saga, in <a href="http://www.NServiceBus.com">NServiceBus</a> lingo), also listening to events from the shipping process, ultimately making a decision when a refund should be given, and for how much.</p>
<h3>Deeper business analysis</h3>
<p>As we discuss matters more with our business stakeholders, we hear that most orders are actually cancelled within an hour of being submitted. It is quite rare for orders to be cancelled days later.</p>
<p>In which case, we could look at modeling the acceptance of an order as a long-running process itself.</p>
<p>When a user places an order, we don&#8217;t immediately publish an event indicating the acceptance of an order, instead a saga is kicked off &#8211; which opens up a timeout for an hour later. If a cancellation command arrives during that period of time, the user gets a full refund (seeing as we didn&#8217;t charge anything since billing didn&#8217;t get the accepted event to begin with), and the saga just shuts itself down. If the timeout occurs an hour later, and the saga didn&#8217;t get a cancel command, then the order is actually accepted and the event is published.</p>
<p>Yes, sagas are everywhere, once you learn to see with business eyes, and no race conditions are left.</p>
<h3>In closing</h3>
<p>Any time you see requirements that indicate a race condition, dig deeper.</p>
<p>What you&#8217;re likely to find are some additional business concepts as well as the introduction of time and the creation of long-running business processes. The implementation at that point will pivot from being trivial if-statements to being richer sagas.</p>
<p>Keep an eye out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/08/31/race-conditions-dont-exist/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Evolving Loosely-Coupled Frameworks &amp; Apps</title>
		<link>http://www.udidahan.com/2010/07/14/evolving-loosely-coupled-frameworks-and-apps/</link>
		<comments>http://www.udidahan.com/2010/07/14/evolving-loosely-coupled-frameworks-and-apps/#comments</comments>
		<pubDate>Wed, 14 Jul 2010 13:10:18 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Simplicity]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1312</guid>
		<description><![CDATA[This post will be less of a big-concept type posts I usually do, and more of a tip for people building and maintaining infrastructure and frameworks either open-source or internally for their companies. I&#8217;m going to illustrate this with NServiceBus as it is a large enough code base to have significant complexity and open so [...]]]></description>
			<content:encoded><![CDATA[<p>This post will be less of a big-concept type posts I usually do, and more of a tip for people building and maintaining infrastructure and frameworks either open-source or internally for their companies. I&#8217;m going to illustrate this with NServiceBus as it is a large enough code base to have significant complexity and open so that you can go and take a look yourself. Trying to include some example in here would be just too small to be useful or for the point to come across.</p>
<h3>Some background</h3>
<p>As a cohesive framework, NServiceBus makes it quite easy for developers to pick and choose which settings they want turned on and off. Being built as a loosely-coupled set of components that don&#8217;t know about each other has always kept the internal complexity low. But as the NServiceBus API has been evolving over the years, and the functionality offered has increased, some interesting challenges have popped up as the codebase has been refactored. </p>
<h3>The challenge</h3>
<p>The UnicastBus class has grown too large and it&#8217;s time to refactor something out. Coincidentally, users have been asking for a better &#8220;header&#8221; story for messages &#8211; the ability to specify static headers that will be appended to all messages being sent (useful for things like security tokens), as well as per message headers. So, we want to refactor all the header management out to its own component independent of the UnicastBus class.</p>
<p>So, here&#8217;s the issue. So far, users have specified &#8220;.UnicastBus()&#8221; as a part of the fluent code-configuration, and shouldn&#8217;t have to change that &#8211; they shouldn&#8217;t need to know that header management is now a separate component. But then how can the new component bootstrap itself into the startup, such that it gets all the dependency injection facilities of the rest of the framework? Remember that the component doesn&#8217;t know which container technology is being used (since the user can swap it out) or when the container has been set.</p>
<h3>The solution</h3>
<p>The only part of the framework that knows about when all DI configuration is set is the configuration component, thus it will have to be the one that invokes the new component (without knowing about it). Introduce an interface (say INeedInitialization) and scan all the types loaded looking for classes which implement that type, register them into the container, and invoke them. Have the new component implement that interface, and in its initialization have it hook into the events and/or pipelines of other parts of the system.</p>
<h3>Other uses</h3>
<p>One historically problematic area in NServiceBus has been people forgetting to call &#8220;.LoadMessageHandlers()&#8221;. This can now be wired in automatically by a class in the UnicastBus component via the same mechanism.</p>
<p>A new feature coming in the next version is the &#8220;data bus&#8221;, a component which will allow sending large quantities of data through the bus without going through the messaging pipelines. This will help people get around the 4MB limit of MSMQ and, even more importantly, the much smaller 8KB limit of Azure. We will be able to introduce the functionality transparently with the same mechanism.</p>
<p>As an extension point, developers can now enrich the NServiceBus framework with their own capabilities and make those available via the contrib project to the community at large. This is better than the IWantToRunAtStartup interface that was only available for those using the generic host (which excluded web apps) and gives a consistent extensibility story for all uses.</p>
<h3>Summary</h3>
<p>Extensibility has always been a challenge when writing object-oriented code and dependency injection techniques have helped, but sometimes you need a bit more to take things to the next level while maintaining a backwards-compatible API.</p>
<p>Like I said, not a ground-shaking topic but something quite necessary in creating loosely-coupled frameworks and applications. Once you know it&#8217;s there, it isn&#8217;t really a big deal. If you didn&#8217;t know to do it, you may have been contorting your codebase in all kinds of ways to try to achieve similar things.</p>
<p>If you want to take a look at the code, you can find the SVN repository here: https://nservicebus.svn.sourceforge.net/svnroot/nservicebus/trunk/</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/07/14/evolving-loosely-coupled-frameworks-and-apps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>High Availability Presentation</title>
		<link>http://www.udidahan.com/2010/06/21/high-availability-presentation/</link>
		<comments>http://www.udidahan.com/2010/06/21/high-availability-presentation/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 06:36:34 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Availability]]></category>
		<category><![CDATA[Presentations]]></category>
		<category><![CDATA[Reliability]]></category>
		<category><![CDATA[The Team]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1308</guid>
		<description><![CDATA[OK &#8211; this is the last one, I promise. Well, for now, anyway.
Earlier this month at TechEd North America I gave a fairly new presentation that was only delivered once before (at the Connected Systems User Group in London) and I&#8217;m happy to say is now online for your viewing pleasure.
High Availability &#8211; A Contrarian [...]]]></description>
			<content:encoded><![CDATA[<p>OK &#8211; this is the last one, I promise. Well, for now, anyway.</p>
<p>Earlier this month at TechEd North America I gave a fairly new presentation that was only delivered once before (at the Connected Systems User Group in London) and I&#8217;m happy to say is now online for your viewing pleasure.</p>
<p><a href="http://www.msteched.com/2010/NorthAmerica/ARC308">High Availability &#8211; A Contrarian View</a></p>
<p>Comments? Thoughts? Let me know.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/06/21/high-availability-presentation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Server Naming and Configuration Conflicts</title>
		<link>http://www.udidahan.com/2010/06/05/server-naming-and-configuration-conflicts/</link>
		<comments>http://www.udidahan.com/2010/06/05/server-naming-and-configuration-conflicts/#comments</comments>
		<pubDate>Sat, 05 Jun 2010 12:59:13 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Management]]></category>
		<category><![CDATA[Simplicity]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1289</guid>
		<description><![CDATA[In my work with clients the topic of how to handle the movement of software from one environment to another inevitably comes up. Sometimes this is in the context of NServiceBus but the problem is more generic. The faster that an organization is able to get software out the door, the more agile they can [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/Configuration_icon_by_obsilion.png" alt="Configuration" title="Configuration" width="225" height="221" style="float:right;" />In my work with clients the topic of how to handle the movement of software from one environment to another inevitably comes up. Sometimes this is in the context of NServiceBus but the problem is more generic. The faster that an organization is able to get software out the door, the more agile they can be. </p>
<p>Unfortunately, there is one tiny little mistake that I see almost everywhere that gets in the way, and that&#8217;s going to be the topic of this post.</p>
<h3>The Problem</h3>
<p>Let&#8217;s say you have a standard web app environment &#8211; some web servers, application servers, and a database server. Your web servers need to send messages to the application servers. So far, so good.</p>
<p>In your test environment, you have an application server called AS_01_Test, and your web servers are configured to send it messages. However, in your staging environment the application server fulfilling that same role is called AS_01_Stage. This creates a configuration problem &#8211; you need to change the config of your web servers as you move the web app from Test to Staging.</p>
<p>I&#8217;ve seen companies doing all sorts of creative things to get around this problem &#8211; some of them involve putting all configuration settings in a database so that they can be centrally managed and visualized. I&#8217;d like to suggest an alternative approach.</p>
<h3>What if&#8230;</h3>
<p>What if server names were the same across all environments?</p>
<p>Well, you wouldn&#8217;t need to change configuration as you moved the system between environments. That&#8217;s a good thing.</p>
<p>But how can that be? Wouldn&#8217;t there be a conflict if there were two machines with the same name?</p>
<p>The answer is that there wouldn&#8217;t be a conflict if the machines were on different networks. Not all machines have to be on the same network. We can set up as many networks / virtual networks as we like. And it is clear that we don&#8217;t need machines in one environment / network to talk to machines in another environment. I mean, under no circumstances would we want web servers in our test environment to talk to application servers in the production environment.</p>
<p>These separate networks provide much needed isolation, beyond solving the server naming problem.</p>
<h3>In closing</h3>
<p>It&#8217;s really a tiny thing when you think about &#8211; multiple networks. But that&#8217;s exactly why software developers overlook it so often &#8211; because it&#8217;s not a &#8220;software solution&#8221; to the configuration problem we perceive as a &#8220;software problem&#8221;.</p>
<p>I wrote about related multi-environment configuration issues in this earlier post: <a href="http://www.udidahan.com/2009/08/15/convention-over-configuration-the-next-generation/">Convention over Configuration &#8211; The Next Generation</a></p>
<p>I&#8217;m happy to say that this functionality is now in NServiceBus called &#8220;profiles&#8221; and you can read more about how they work <a href="http://www.nservicebus.com/Profiles.aspx">here</a>.</p>
<p>How are you handling the flow of moving software through to production? Leave your comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/06/05/server-naming-and-configuration-conflicts/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>CQRS isn&#8217;t the answer &#8211; it&#8217;s just one of the questions</title>
		<link>http://www.udidahan.com/2010/05/07/cqrs-isnt-the-answer-its-just-one-of-the-questions/</link>
		<comments>http://www.udidahan.com/2010/05/07/cqrs-isnt-the-answer-its-just-one-of-the-questions/#comments</comments>
		<pubDate>Fri, 07 May 2010 09:25:12 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[CQRS]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Management]]></category>
		<category><![CDATA[Simplicity]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1278</guid>
		<description><![CDATA[With the growing interest in Command/Query Responsibility Segregation (CQRS), more people are starting to ask questions about how to apply it to their applications. CQRS is actually in danger of reaching &#8220;best practice&#8221; status at which point in time people will apply it indiscriminately with truly terrible results. 
One of the things that I&#8217;ve been [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/dont_pan.gif" alt="dont panic" title="dont panic" width="260" height="180" style="float:right;" />With the growing interest in Command/Query Responsibility Segregation (<a href="http://www.udidahan.com/2009/12/09/clarified-cqrs/">CQRS</a>), more people are starting to ask questions about how to apply it to their applications. CQRS is actually in danger of reaching &#8220;best practice&#8221; status at which point in time people will apply it indiscriminately with truly terrible results. </p>
<p>One of the things that I&#8217;ve been trying to do with my presentations around the world on CQRS was to explain the <b>why</b> behind it, just as much as the what. The problem with the format of these presentations is that they&#8217;re designed to communicate a fairly closed message: here&#8217;s the problem, here&#8217;s how that problem manifests itself, here&#8217;s a solution.</p>
<p>In this post, I&#8217;m going to try to go deeper.</p>
<h3>The hitchhiker&#8217;s guide to the galaxy</h3>
<p>In this most excellent book, one of the things that struck me was the theme that made it&#8217;s way through the whole book &#8211; starting with the answer to life, the universe, and everything: 42. By the time you get to the end of the book, you find out that the real <b>question</b> to life, the universe, and everything is &#8220;what do you get when you multiply 6 by 9&#8243;. And that&#8217;s how the book leaves it.</p>
<p>To us engineers, we can&#8217;t just accept the fact that the book would say that 6*9 = 42 when we know it&#8217;s 54. After bashing our heads on the rigid rules of math, we realize that not all math problems are necessarily in base 10, and that if we switch to base 13, the number 42 is 4*13 + 2 = 54. So, the book was right &#8211; but that&#8217;s not the point.</p>
<h3>What&#8217;s the point?</h3>
<p>The hitchhiker&#8217;s guide is an example of a teaching technique which presents an apparent paradox, leaving the student to dig up unspoken and unthought assumptions in order to resolve it. Key to this technique are rigid rules which do not allow any compromise or shortcuts on the student&#8217;s part. </p>
<p>The purpose of this technique is not for the student to learn the answer, but to gain deeper understanding, which in turn changes the way they go about thinking about problems in the future. </p>
<p>So, when given the problem 4*5, we do not just immediately answer 20, instead we clarify in which numeric base the question is being phrased, and only then go to solve the problem. In base 13, the answer would be 17. In hex, the answer would be 14.</p>
<p>The externally visible change is that we know which questions to ask in order to arrive at the right answer &#8211; not that we know the answer ahead of time.</p>
<h3>Making an &#8220;ass&#8221; out of &#8220;u&#8221; and &#8220;me&#8221;</h3>
<p>Let&#8217;s start at the end &#8211; one of the unspoken assumptions that has been causing problems:</p>
<blockquote><p>
All businesses can be treated the same from the perspective of software.
</p></blockquote>
<p>In our previous example, we assumed that all math problems use base 10. It turns out that different bases are useful for different domains (like base 2 for computers). We can say similar things about degrees and radians in geometry. The more we look at the real world, the more we see this repeating itself. There&#8217;s no reason that software should be any different.</p>
<p>Base 10 is not a ubiquitous best practice. We shouldn&#8217;t be surprised that there really aren&#8217;t best practices for software either.</p>
<p>Here&#8217;s another problematic assumption:</p>
<blockquote><p>
&#8220;The business&#8221; can (and do) tell us what they need in a way we can understand.
</p></blockquote>
<p>So many software fads have been built on the quicksand of this assumption. OOAD &#8211; on verbs and nouns. 4GL and other visual tools that &#8220;the business&#8221; will use directly. SOA &#8211; on IT business alignment. I expect we haven&#8217;t seen the end of this.</p>
<p>Some of you may be wondering why this is false, others are sagely nodding their heads in agreement.</p>
<h3>The myth of &#8220;the business&#8221;</h3>
<p>Unless you have a single user, who is also the CEO paying for the development, there is no &#8220;the&#8221;. It&#8217;s an amalgam of people with different backgrounds, skills, and goals &#8211; there is no homogeneity. Even if no software was involved, many business organizations are dysfunctional with conflicting goals, policies, and politics.</p>
<p>To some extent, we technical people have hidden ourselves away in IT to avoid the scary world of business whose rules we don&#8217;t understand. With the rise in importance of information to the world, we&#8217;ve been pulled back &#8211; being forced to talk to people, and not just computers. Luckily, we&#8217;ve been able to create a buffer to insulate ourselves &#8211; we&#8217;ve taken the less successful technical people from our heard and nominated them &#8220;business analysts&#8221;. No, not all companies do it this way, but we do need to take a minute to reflect on how information flows between the business Mars into and out of the IT Venus.</p>
<h3>On human communication</h3>
<p>Even if we made this insulation layer more permeable, allowing and encouraging more technical people and business people to cross its boundary, we still need to deal with the problem of two humans communicating with each other. There are enough books that have been written on this topic, so I won&#8217;t go into that beyond recommending (strongly) to technical people to read (some of) them.</p>
<p>Rather, I&#8217;d like to focus on the environment in which these discussions take place. IT has been around long enough, and users have used computers long enough, that a certain amount of tainting has taken place. If the world was a trial, the evidence would have been thrown out as untrustworthy.</p>
<p>When users tell you what they want, they&#8217;re usually framing that with respect to the current system that they&#8217;re using. &#8220;Like the old system &#8211; but faster, and with better search, and more information on that screen, and&#8230;&#8221; </p>
<p>At this point, business analysts write down and formalize these &#8220;requirements&#8221; into some IT-sanctioned structure (use cases, user stories, whatever), at which point developers are told to build it. Users only know what they <b>didn&#8217;t</b> want when developers deliver exactly what was asked.</p>
<p>How can that be?</p>
<h3>These are not the &#8220;requirements&#8221; you are looking for</h3>
<p>Users ultimately dictate <b>solutions</b> to us, as a delta from the previous set of solutions we&#8217;ve delivered them. That&#8217;s just human psychology &#8211; writer&#8217;s block when looking at a blank page, as compared to the ease with which we provide &#8220;constructive criticism&#8221; on somebody else&#8217;s work.</p>
<p>We need to get the real requirements. We need to probe beyond the veneer:</p>
<ul>
<li>Why do you need this additional screen? </li>
<li>What real-world trigger will cause you to open it? </li>
<li>Is there more than one trigger? </li>
<li>How are they different?</li>
<li>etc, etc, etc&#8230;</li>
</ul>
<p>This is <b>real</b> work &#8211; different work than programming. It requires different skills. And that&#8217;s not even getting into the political navigation between competing organizational forces.</p>
<p>But let&#8217;s say that you don&#8217;t have (enough) people with these skills in your organization. What then?</p>
<h3>Enter CQRS</h3>
<p>CQRS gives us a set of questions to ask, and some rigid rules that our answers must conform to. If our answers don&#8217;t fit, we need to go back to the drawing board and move things around and/or go back to &#8220;the business&#8221; and seek deeper understanding there.</p>
<p>For each screen/task/piece of data:</p>
<blockquote><p>
Will multiple users be collaborating on data related to this task?<br />
Look at every shred of raw data, not just at the entity level.<br />
Are there business consistency requirements around groups of raw data?
</p></blockquote>
<p>If &#8220;the business&#8221; answers no &#8211; ask them if they see that answer changing, and if so, in what time frame, and why. What changing conditions in the business environment would cause that to change &#8211; what other parts of the system would need to be re-examined under those conditions.</p>
<p>After understanding all that and you find a true single-user-only-thing, then you can use standard &#8220;CRUD&#8221; techniques and technologies. There are no inherent time-propagation problems in a single-user environment &#8211; so eventual consistency is beyond pointless, it actually makes matters worse.</p>
<p>On the other hand, if the business-data-space is collaborative, the inherent time-propagation of information between actors means they will be making decisions on data that isn&#8217;t up-to-the-millisecond-accurate anyway. This is physics, gravity &#8211; you can&#8217;t fight it (and win).</p>
<h3>The rule for collaboration</h3>
<blockquote><p>
Actors must be able submit one-way commands that will fail only under exceptional <b>business</b> circumstances.
</p></blockquote>
<p>The challenge we have is how to achieve the real business objectives uncovered in our previous &#8220;requirements excavation&#8221; activities and follow this rule at the same time. This will likely involve a different user-system interaction than those implemented in the past. UI design is part of the solution domain &#8211; it shouldn&#8217;t be dictated by the business (otherwise it&#8217;s like someone asking you to run a marathon, but also dictating how you do so, like by tying your shoelaces together).</p>
<p>Many of the technical patterns I described in my <a href="http://www.udidahan.com/2009/12/09/clarified-cqrs/">previous blog post</a> describe the tools involved. BTW, hackers can be considered &#8220;exceptional actors&#8221; &#8211; the business actually wants their commands to fail.</p>
<h3>In Summary</h3>
<p>The hard and fast rule of CQRS about one-way commands is relevant for collaborative domains only. This domain has inherent eventual consistency &#8211; in the real world. Taking that and baking it into our solution domain is how we align with the business.</p>
<p>The process we go through, until ultimately arriving at one-way-almost-always-successful-commands <b>is business analysis</b>. Rejecting pre-formulated solutions, truly understanding the business drivers, and then representing those as directly as possible in our solution domain &#8211; that&#8217;s our job.</p>
<p>After doing this enough times and/or in more than one business domain, we may gain the insight that there is no cookie-cutter, one-size-fits-all, best-practice solution architecture for everything. Each problem domain is distinct and different &#8211; and we need to understand the details, because they <b>should</b> shape the resulting software structure.</p>
<p>The next time the business tell us to implement 42, we&#8217;ll use CQRS along with other questioning techniques until we can get &#8220;6 x 9&#8243; out of them, learning from the exercise what are the significant and stable parts of the business &#8211; ultimately helping us to &#8220;build the right system, and to build the system right&#8221;.</p>
<p>Don&#8217;t Panic <img src='http://www.udidahan.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/05/07/cqrs-isnt-the-answer-its-just-one-of-the-questions/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>On Design for Testability</title>
		<link>http://www.udidahan.com/2010/04/18/on-design-for-testability/</link>
		<comments>http://www.udidahan.com/2010/04/18/on-design-for-testability/#comments</comments>
		<pubDate>Sun, 18 Apr 2010 15:43:57 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Management]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Testing]]></category>
		<category><![CDATA[The Team]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1221</guid>
		<description><![CDATA[Almost at every conference, event, training, or consulting engagement someone asks for my opinion on the whole design for testability thing. I&#8217;m not quite sure why I haven&#8217;t blogged on this topic, especially at the time that a lot of the other bloggers were weighing in, but better late than never.
Before getting into that, I [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/elephant_balance.png" style="float:right; margin-left:10px; margin-bottom:10px; " alt="keeping balance" title="keeping balance" width="204" height="203" />Almost at every conference, event, training, or consulting engagement someone asks for my opinion on the whole design for testability thing. I&#8217;m not quite sure why I haven&#8217;t blogged on this topic, especially at the time that a lot of the other bloggers were weighing in, but better late than never.</p>
<p>Before getting into that, I want to start with a slightly broader scope of discussion.</p>
<p>You see, I get asked about &#8220;best practices&#8221; on all sorts of things. And I try not to be the kind of consultant that responds with &#8220;it depends&#8221;, but the context of the question often makes the answer irrelevant. And the unspoken context of a best-practice question is:</p>
<h2>Given infinite time and budget</h2>
<p>The biggest problem that I see with well-intentioned, best-practices-following developers and architects is that they <b>don&#8217;t</b> ask the question &#8220;is this the right thing for us to be focusing on right now?&#8221; Understandably, that is a difficult question to answer &#8211; but it needs to be asked, since you don&#8217;t have infinite time or budget to do everything according to best practices (assuming those even exist).</p>
<h2>About testing</h2>
<p>The biggest issue I have with the &#8220;design for testability&#8221; topic is the extremely narrow view it takes of the word &#8220;testability&#8221;, usually in the form of more code written by a developer which invokes the production code of the system, also known as &#8220;unit tests&#8221;.</p>
<p>There are many different kinds of testing &#8211; unit, integration, functional, load, performance, exploratory, etc&#8230; where some may be automated and others not. Should we not discuss what &#8220;design for testability&#8221; means for not-just-unit-testing?</p>
<h2>And what&#8217;s the point of testing anyway?</h2>
<p>It&#8217;s not to find bugs.</p>
<p>Research has shown that testing (of all kinds) is not the most effective way of finding bugs. I don&#8217;t have the reference handy but I&#8217;m pretty sure that it&#8217;s from <a href="http://alistair.cockburn.us/">Alistair Cockburn&#8217;s work</a>. Code reviews are (on average) about 60% more effective.</p>
<p>Don&#8217;t get me wrong &#8211; testing can provide indications that the software <b>has</b> bugs in it, but not necessarily where in the code those bugs are.</p>
<p>The purpose of testing is to provide quantitative and qualitative information about the system that can help various stakeholders in their decision-making processes. The relevance of that information indicates the quality of the testing. Here are some examples:</p>
<ul>
<li>The system supports 100 concurrent users, with the expected user-type distribution (X% role A, Y% role B, etc), performing expected use-case distributions, and collaboration scenarios.</li>
<li>Time to proficiency for new users in role A is expected to be 3 days</li>
<li>Alternate #2 of use case #12 fails on step #3</li>
</ul>
<p>As you can see, the relevance of the above information is dependent on what decisions the various stakeholders need to make. The bullet on load can help us decide if more machines are needed or if developers need to tune the performance of the systems. The bullet on time to proficiency can help us decide if larger investment in usability is required. Information like the last bullet can be used in conjunction with the first two to decide on the timing and type of a release.</p>
<p>The timeliness of this relevant information is critical to the success of a project.</p>
<p>Choosing which and how much of the various testing activities to perform when is something that needs to be revisited several times throughout the lifetime of a project, taking into account the current risks (threats and probabilities) and time and resource investment to mitigate them.</p>
<p>Let me reiterate &#8211; we&#8217;re not going to have enough time to do everything.</p>
<h2>On iterations</h2>
<p>If the only part of your organization that is doing iterations are your developers, you&#8217;re not agile.</p>
<p>In order to capitalize on the information that testers are providing, you need them in your iterations.</p>
<p>The same goes for the other roles involved in the project &#8211; business analysts, DBAs, sysadmins, etc.</p>
<p>I know that 99% of organizations aren&#8217;t structured in a way to do this.</p>
<p>I never said doing this would be easy.</p>
<h2>On design</h2>
<p>Figuring out what kind of design and how much to do when is just as important, and just as hard. Design for testability is one part of that, but not the only one, or necessarily the most important one at any point of time.</p>
<p>Within that design for testability topic is the &#8220;design for unit-testing&#8221; sub-topic which seems to be the popular one. Before getting into the design aspects of it, let&#8217;s take a closer look at the unit-testing side of things.</p>
<h2>On unit-testing</h2>
<p>The assumption is that having more unit tests will lead to a code-base with less bugs, thus requiring shorter time to get the system into production, which will pay back the time it took to write those unit tests to begin with.</p>
<p>In practice, what tends to happen is that as development progresses, testing code breaks as the structure of the production code changes. Now one of two things happens &#8211; either the testing code is removed or rewritten. In either case, we didn&#8217;t get the return on investment we expected on the first bit of testing code. Unfortunately, rare is the case where the relevant people in the organization understand <b>why</b>, resulting in the same situation repeating itself over and over again.</p>
<p>Those projects would have been better off without unit testing, though the organization as a whole might have used those experiences to learn and improve. It&#8217;s been my experience that if the organization wasn&#8217;t conscious enough in the context of the project to notice the situation, it is unlikely to do so at higher levels.</p>
<h2>On fragile unit tests</h2>
<p>The reason that a unit test ends up being rewritten (or removed) is that its code was coupled to the production code in such a way that it broke when the production code changed. This tendency to break (fragility) is a critical property of a unit test. A fragile unit test will slow down a developer doing work on some existing code &#8211; it actually makes the system less maintainable.</p>
<p>For a unit test code to be stable (not fragile) it needs to be coupled to stable properties of the production code. The question of whether the production code is designed in such a way that it has stable properties &#8211; is a design question. Is it a <b>unit</b>? If not, you will <b>not</b> be able to write a unit-test against it.</p>
<p>And anyway, who said that every class is a unit, or should be a unit? Domain models (when done right) are good examples of a unit, yet the classes that make them up may not be units. Unit-testing should only be attempted with things which are units. </p>
<p>I think too much weight is put on whether a dependency of a class is a concrete or interface type, and not nearly enough on the nature of the dependency. I wouldn&#8217;t blame the hammer for pounding my thumb, and by the same token I think that blame should not be directed towards tools like those from TypeMock.</p>
<h2>On tools</h2>
<p>There is so much more depth to both design and testability that needs to be more broadly understood. No tool has yet been created to handle either design or testing in such a way that humans can give up responsibility for the outcome. </p>
<p>Over the years I&#8217;ve noticed that tools are most significant when used by skilled practitioners, which makes sense in retrospect. Giving a novice carpenter a laser-guided saw probably won&#8217;t significantly change the outcome of their work. Ultimately, the skilled practitioners are the ones that create tools &#8211; not the novices. And no tool, no matter how advanced, will make a novice perform at levels like the skilled practitioner.</p>
<p>In the case of a project too big for a single skilled practitioner to complete in the time required (or at all), the balance of importance shifts away from tools to the project management topics described above.</p>
<h2>In summary</h2>
<p>I hope that this post has shed some light on the context in which decisions with respect to testing need to be made. Design is one activity that can support certain kinds of testing, but not the only one, or even the most important one for the given type of testing necessary at that time in the project.</p>
<p>Design is hard. Project management is hard. Testing is hard.</p>
<p>Getting the right mix of people that together have enough experience and skills in these activities isn&#8217;t easy.</p>
<p>Don&#8217;t expect that sprinkling some interfaces in your code base will be enough.<br />
That doesn&#8217;t count much in the way of design, just as writing code in a testing namespace doesn&#8217;t count much in the way of testability.</p>
<p>Looking forward to hearing your comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/04/18/on-design-for-testability/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>On Small Applications</title>
		<link>http://www.udidahan.com/2010/03/07/on-small-applications/</link>
		<comments>http://www.udidahan.com/2010/03/07/on-small-applications/#comments</comments>
		<pubDate>Sun, 07 Mar 2010 11:32:34 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Management]]></category>
		<category><![CDATA[The Team]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1199</guid>
		<description><![CDATA[I hear this too often: &#8220;X sounds like a great pattern, but it&#8217;s overkill for small applications&#8221;. Many patterns have been subjected to this including (but not limited to): SOA, DDD, CQRS, ORM, etc. Often the statement is made by a person without experience in the given pattern (though possibly experienced in other patterns). Let&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/small_baby.png" style="float:right; margin-left:10px; margin-bottom:10px;" alt="small" title="small" />I hear this too often: &#8220;X sounds like a great pattern, but it&#8217;s overkill for small applications&#8221;. Many patterns have been subjected to this including (but not limited to): SOA, DDD, CQRS, ORM, etc. Often the statement is made by a person without experience in the given pattern (though possibly experienced in other patterns). Let&#8217;s take a look at the second part &#8211; the &#8220;small application&#8221;, and ask:</p>
<h3>What makes an app small?</h3>
<p>Or inversely, what makes an app warrant the &#8220;enterprise&#8221; moniker?</p>
<p>If there&#8217;s one thing that the history of our industry has shown repeatedly, it&#8217;s that developers aren&#8217;t particularly accurate with their estimates. Like, orders-of-magnitude inaccurate. Knowing this, it&#8217;s surprising that the &#8220;small app&#8221; argument seems to win so many arguments. The same goes for justifications in the form of &#8220;we&#8217;ve got to have an X, this is a BIG project&#8221;.</p>
<p>So, what makes an app small? </p>
<p>Is it a small number of lines of code? Well, what if those lines of code are keeping planes in the air?</p>
<p>Is it a small number of developers? Same as above. Actually, history has shown that some of the most valuable bits of code written were done by small numbers of developers. </p>
<p>Is it that it will only be installed on a single machine? </p>
<p>Is it&#8230;</p>
<p>What could it be?</p>
<h3>The real issue</h3>
<p>The small app argument is a diversionary tactic. </p>
<p>Loosely translated, it means &#8220;I&#8217;m comfortable where I am and I don&#8217;t want to change&#8221;.</p>
<p>Moving on&#8230;</p>
<h3>The real story of size</h3>
<p>Once we actually look at the specific context of an app, we tend to see that someone cares a great deal about it, enough to finance its custom development &#8211; rather than buying an off-the-shelf alternative. The expected lifetime of business use is easily 3-5 years, if not 7-10, during which many enhancements will likely be requested. Thus, some non-functional properties of the code matter &#8211; at the very least maintainability.</p>
<p>In which case, if the given pattern or approach does significantly improve the desired non-functional properties of the app, it only makes sense to use it.</p>
<p>There is one class of software that might possibly be treated as &#8220;small&#8221; &#8211; the one-off script that&#8217;s written to automate some IT task. And even then, so many of these scripts end up living longer than the apps themselves that they should be engineered at the same level of quality.</p>
<h3>In closing</h3>
<p>Don&#8217;t counter a &#8220;small app&#8221; argument with psychology.<br />
It will only make matters worse.</p>
<p>Instead, rephrase the issue around the lifetime of business use. </p>
<p>I&#8217;ve found that there are precious few cases where the harsh light of reality doesn&#8217;t help the appropriate decisions be made. If indeed this is a small-lifetime-app, just drag-and-drop until you&#8217;re done. Otherwise, the time it takes to understand and evaluate the applicability of the given patterns will definitely pay itself back many times over the life of the app.</p>
<p>And managers, keep your ears open for it. The technical risks behind that statement are icebergs waiting to sink your project.</p>
<p>* with thanks to <a href="http://devlicious.com/blogs/mike_nichols/archive/2010/03/06/the-biggest-driver-for-domain-modeling-decisions.aspx">Mike Nichols</a> for pushing my buttons.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/03/07/on-small-applications/feed/</wfw:commentRss>
		<slash:comments>20</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>Non-functional Architectural Woes</title>
		<link>http://www.udidahan.com/2010/01/12/non-functional-architectural-woes/</link>
		<comments>http://www.udidahan.com/2010/01/12/non-functional-architectural-woes/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 04:29:31 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1165</guid>
		<description><![CDATA[
As I sit here in the lounge at Bogota airport waiting for my delayed flight, I remembered something interesting that came up in my 2-week training/consulting in Cali. It&#8217;s not a question that came up, or anything like that. It was that I suddenly noticed a pattern in many of my consulting and training clients [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/non-functional-architecture.png" style="float:right; margin-left:10px; margin-bottom:10px;" alt="Non-functional architecture" title="Non-functional architecture" /><br />
As I sit here in the lounge at Bogota airport waiting for my delayed flight, I remembered something interesting that came up in my 2-week training/consulting in Cali. It&#8217;s not a question that came up, or anything like that. It was that I suddenly noticed a pattern in many of my consulting and training clients over the past years. And as I thought about it, I realized that it was prevalent in our industry as a whole &#8211; in the literature, on the web, everywhere.</p>
<p>It&#8217;s how people think about functional and non-functional requirements.</p>
<h3>The problem with categorization</h3>
<p>There&#8217;s nothing wrong with categorizing requirements as either functional or non-functional.</p>
<p>The problem is that people project that categorization from the problem domain to the solution domain as well.</p>
<p>There is an impression that architecture and technology choices are, to a large extent, based on non-functional requirements, and only on non-functional requirements.</p>
<p>Here are some examples:</p>
<p><b>Extensibility: </b> Workflow/BPM engine , DSL, Plug-in framework, etc<br />
<b>Scalability: </b> Message/Service Bus, Database (NoSQL camp, I&#8217;m looking at you too), etc<br />
<b>High Availability:</b> See scalability</p>
<p>Too many times have I noticed architects so focused on these issues that they all but ignore the functional requirements and the business objectives of the stakeholders.</p>
<p>Not to place an unfair portion of the blame there, the vendors have been perpetuating the above fallacies to sell more and/or new products. Given the enormous influence of the big vendors (conferences, training, etc) it isn&#8217;t any wonder that architects in the field use the vendors&#8217; &#8220;best practices&#8221;.</p>
<p>The problem that arises from this kind of thinking is a shoe-horning of functional requirements into the architecture decided upon entirely in the context of non-functional requirements.</p>
<h3>Functional Earthquakes</h3>
<p>Stable architecture cannot be created based entirely on non-functional requirements. There are functional requirements that can shake the foundation of a technically-oriented architecture.</p>
<p>Let&#8217;s take the canonical layered architecture with its normalized database. Now we get a new requirement in the form of:</p>
<p>&#8220;As a supplier, when I log-in, I want to see on my home page my most recent purchase orders, grouped by highest value retailer (total historical purchase order value), sorted according to requested time of delivery.&#8221;</p>
<p>In order for a developer to implement this according to the architectural guidelines of normalization and layering here&#8217;s what they do: join retailers who have an agreement with the supplier (join between supplier, retailer, and agreement tables), join the purchase orders and their lines, (ignoring tax for now), sum the line value and group by retailer, and use as an input to the purchase order table joining again, filter in last 24 hours, sort by time of delivery.</p>
<p>So, in our normalized database, we have many millions of purchase orders, hundreds of millions of lines, that we&#8217;re joining against each other as well as several other tables.</p>
<p>After this new feature has been implemented, any time a supplier sees their home page, the system stops accepting purchase orders until the home page has been rendered several minutes later.</p>
<p>Can we really say that our architecture is stable if a single functional requirement can undo all of it&#8217;s non-functional properties?</p>
<p>Obviously not &#8211; but the question the architect (and his boss) are asking is, how did this happen? And if it happened once, can it happen again?</p>
<h3>Lessons Learned &#8211; sorta</h3>
<p>So a reporting database is introduced so that all complex queries like those performed above won&#8217;t prevent the system from accepting new purchase orders. A nightly batch moves data from the normalized DB to the reporting DB. Sounds good &#8211; non-functionally.</p>
<p>And then a new requirement comes in for handling rush-orders. </p>
<p>So we do an hourly batch. But now these batches start to cause hiccups to our transactional system, which gets backed up, and when released, allocates many more threads to deal with the pending load, and the increased concurrency in the databases sharply increases the number of deadlocks, further backing up the system, until the system becomes effectively unavailable.</p>
<p>Can this be happening again? A functional requirement undoing all of our technically elegant non-functional architectural decisions?</p>
<h3>Now what?</h3>
<p>The technology is blamed. We should have never counted on SQL Server to handle our kind of *enterprise* requirements. Let&#8217;s move to Oracle &#8211; it&#8217;s *unbreakable*. (Several months and functionalities later) We should have never counted on a database to handle our kind of enterprise requirements. Let&#8217;s introduce an *Enterprise Service Bus*. (Several months and functionalities later) We should have never counted on our internal IT to host this. Let&#8217;s move to *The Cloud*. (Several months and functionalities later, looking at the bill from our cloud provider) We should have never used .NET for our application because it requires the more expensive Windows cloud. Let&#8217;s rewrite on Linux to reduce our cloud costs.</p>
<p>By this time, all the people who originally worked on the project aren&#8217;t there anymore. And the cycle continues with limited memory of where we started and how we got here.</p>
<p>Soon, soon, we&#8217;ll find that non-functional silver bullet that will make all of our problems go away.</p>
<h3>These are not the droids you are looking for</h3>
<p>They really aren&#8217;t.</p>
<p>If we want our architecture to be stable, we need to base it on stable abstractions. The only thing is that there aren&#8217;t any inherently stable abstractions in the solution domain (as we&#8217;ve had the chance to witness). That really only leaves one other place to look for them &#8211; in the problem domain, also known as the functional requirements.</p>
<p>But functional requirements change all the time! Wasn&#8217;t that what got us into this mess to begin with?</p>
<p>Indeed, but in between the functional requirements and behind them is something that is quite stable: the stakeholders business objectives.</p>
<p>The supply chain will continue to strive to optimize itself. To shorten the time for an order to be fulfilled. To decrease the amount of inventory that a retailer holds. To choose the best set of suppliers for our product catalog. To recognize which retailers give me the most business and serve them better. To identify high potential retailers &#8211; big retailers (like Walmart) who aren&#8217;t buying as much from me as other retailers.</p>
<p>This is how the business has been done for decades and will continue to be done for decades more.</p>
<p>If we could find a way to capture those stable elements and represent them as core elements in our architectural structure, and then balance the non-functional requirements within those functional contexts, maybe, just maybe, our architecture will stand the test of time.</p>
<p>More to come&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/01/12/non-functional-architectural-woes/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Scalability Podcast on Herding Code</title>
		<link>http://www.udidahan.com/2010/01/11/scalability-podcast-on-herding-code/</link>
		<comments>http://www.udidahan.com/2010/01/11/scalability-podcast-on-herding-code/#comments</comments>
		<pubDate>Mon, 11 Jan 2010 19:44:49 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Community]]></category>
		<category><![CDATA[Podcast]]></category>
		<category><![CDATA[Scalability]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1168</guid>
		<description><![CDATA[The great folks over at Herding Code were nice enough to interview me back in November as I was over in Paris giving my 5-day SOA course. We talked about quite a lot of topics related to scalability.
Click here for the full list of topics and to download the podcast.
Let me know what you think [...]]]></description>
			<content:encoded><![CDATA[<p>The great folks over at <a href="http://www.herdingcode.com">Herding Code</a> were nice enough to interview me back in November as I was over in Paris giving my <a href="http://www.UdiDahan.com/training">5-day SOA course</a>. We talked about quite a lot of topics related to scalability.</p>
<p><a href="http://herdingcode.com/?p=229">Click here</a> for the full list of topics and to download the podcast.</p>
<p>Let me know what you think or any questions you may have in the comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/01/11/scalability-podcast-on-herding-code/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>94</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>MySpace Architecture Considered Expensive</title>
		<link>http://www.udidahan.com/2009/10/09/myspace-architecture-considered-expensive/</link>
		<comments>http://www.udidahan.com/2009/10/09/myspace-architecture-considered-expensive/#comments</comments>
		<pubDate>Fri, 09 Oct 2009 21:24:09 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Caching]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Scalability]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1126</guid>
		<description><![CDATA[I just finished listening to the Microsoft presentation on how they use the Concurrency &#038; Coordination Runtime (CCR) in MySpace (the stated largest web site running .NET).
Some interesting numbers were stated in the talk.

Tens of thousands to hundreds of thousands of requests per second
Over 3 thousand web servers
Over a thousand mid-tier servers

No wonder most big [...]]]></description>
			<content:encoded><![CDATA[<p>I just finished listening to the Microsoft <a href="http://channel9.msdn.com/shows/Communicating/CCR-at-MySpace/">presentation</a> on how they use the <a href="http://msdn.microsoft.com/en-us/library/bb905470.aspx">Concurrency &#038; Coordination Runtime (CCR)</a> in MySpace (the stated largest web site running .NET).</p>
<p>Some interesting numbers were stated in the talk.</p>
<ul>
<li>Tens of thousands to hundreds of thousands of requests per second</li>
<li>Over 3 thousand web servers</li>
<li>Over a thousand mid-tier servers</li>
</ul>
<p>No wonder most big web sites don&#8217;t run .NET. The Windows licenses would put them out of business.</p>
<p>Well, that is if you follow those same architectural practices.</p>
<p>I&#8217;ve written in the past of alternative architectural approaches that can scale to those levels at easily an order of magnitude less hardware (I think it&#8217;s closer to two OOMs) &#8211; here&#8217;s one of them on the topic of weather:</p>
<p><a href="http://www.udidahan.com/2008/12/29/building-super-scalable-web-systems-with-rest/">Building Super-Scalable Web Systems with REST</a>.</p>
<p>By the way, the client quoted in that post is now well above 60 million users with only small incremental increases in hardware. Oh, and their running everything on Windows and .NET. The question is not &#8220;can it scale&#8221;, but rather &#8220;how much will it cost to scale&#8221;.</p>
<p>Architecture pays itself back faster than ever in the Web 2.0 world.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/10/09/myspace-architecture-considered-expensive/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>[Article] EDA: SOA through the looking glass</title>
		<link>http://www.udidahan.com/2009/09/29/article-eda-soa-through-the-looking-glass/</link>
		<comments>http://www.udidahan.com/2009/09/29/article-eda-soa-through-the-looking-glass/#comments</comments>
		<pubDate>Tue, 29 Sep 2009 11:05:33 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Articles]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[SOA]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1117</guid>
		<description><![CDATA[

My latest article has been published in issue 21 of the Microsoft Architecture Journal:
EDA: SOA Through The Looking Glass

While event-driven architecture (EDA) is a broadly known topic, both giving up ACID integrity guarantees and introducing eventual consistency make many architects uncomfortable. Yet it is exactly these properties that can direct architectural efforts toward identifying coarsely [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/architecture/aa699424.aspx"><br />
<img src="http://www.udidahan.com/wp-content/uploads/arcjournal21.png" style="float:right; margin-left:20px; margin-bottom:10px; border:1px solid black" alt="Microsoft Architecture Journal" title="Microsoft Architecture Journal" /></a></p>
<p>My latest article has been published in issue 21 of the Microsoft Architecture Journal:</p>
<p><u>EDA: SOA Through The Looking Glass</u></p>
<div style="font-size:12px">
While event-driven architecture (EDA) is a broadly known topic, both giving up ACID integrity guarantees and introducing eventual consistency make many architects uncomfortable. Yet it is exactly these properties that can direct architectural efforts toward identifying coarsely grained business-service boundaries—services that will result in true IT-business alignment.</p>
<p>Business events create natural temporal boundaries across which there is no business expectation of immediate consistency or confirmation. When they are mapped to technical solutions, the loosely coupled business domains on either side of business events simply result in autonomous, loosely coupled services whose contracts explicitly reflect the inherent publish/subscribe nature of the business.</p>
<p>This article will describe how all of these concepts fit together, as well as how they solve thorny issues such as high availability and fault tolerance.</p>
<p><a href="http://msdn.microsoft.com/en-us/architecture/aa699424.aspx">Continue reading&#8230;</a>
</div>
<p>Please leave questions and comments here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/09/29/article-eda-soa-through-the-looking-glass/feed/</wfw:commentRss>
		<slash:comments>9</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>Don&#8217;t Delete &#8211; Just Don&#8217;t</title>
		<link>http://www.udidahan.com/2009/09/01/dont-delete-just-dont/</link>
		<comments>http://www.udidahan.com/2009/09/01/dont-delete-just-dont/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 12:04:48 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Business Rules]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1097</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/no_delete.png" style="float:right; margin-left:10px; margin-bottom:10px; alt="no deletes" title="no deletes" /><br />
After reading Ayende&#8217;s <a href="http://ayende.com/Blog/archive/2009/08/30/avoid-soft-deletes.aspx">post</a> advocating against &#8220;soft deletes&#8221; I felt that I should add a bit more to the topic as there were some important business semantics missing. As developers discuss the pertinence of using an IsDeleted column in the database to mark deletion, and the way this relates to reporting and auditing concerns is weighed, the core domain concepts rarely get a mention. Let&#8217;s first understand the business scenarios we&#8217;re modeling, the why behind them, before delving into the how of implementation.</p>
<h3>The real world doesn&#8217;t cascade</h3>
<p>Let&#8217;s say our marketing department decides to delete an item from the catalog. Should all previous orders containing that item just disappear? And cascading farther, should all invoices for those orders be deleted as well? Going on, would we have to redo the company&#8217;s profit and loss statements?</p>
<p>Heaven forbid.</p>
<p>So, is Ayende wrong? Do we really need soft deletes after all?</p>
<p>On the one hand, we don&#8217;t want to leave our database in an inconsistent state with invoices pointing to non-existent orders, but on the other hand, our users did ask us to delete an entity.</p>
<p>Or did they?</p>
<h3>When all you have is a hammer&#8230;</h3>
<p>We&#8217;ve been exposing users to entity-based interfaces with &#8220;create, read, update, delete&#8221; semantics in them for so long that they have started presenting us requirements using that same language, even though it&#8217;s an extremely poor fit.</p>
<p>Instead of accepting &#8220;delete&#8221; as a normal user action, let&#8217;s go into why users &#8220;delete&#8221; stuff, and what they actually intend to do.</p>
<p>The guys in marketing can&#8217;t actually make all physical instances of a product disappear &#8211; nor would they want to. In talking with these users, we might discover that their intent is quite different:</p>
<blockquote><p>“What I mean by &#8216;delete&#8217; is that the product should be discontinued. We don&#8217;t want to sell this line of product anymore. We want to get rid of the inventory we have, but not order any more from our supplier. The product shouldn&#8217;t appear any more when customers do a product search or category listing, but the guys in the warehouse will still need to manage these items in the interim. It&#8217;s much shorter to just say &#8216;delete&#8217; though.”</p></blockquote>
<p>There seem to be quite a few interesting business rules and processes there, but nothing that looks like it could be solved by a single database column.</p>
<h3>Model the task, not the data</h3>
<p>Looking back at the story our friend from marketing told us, his intent is to discontinue the product &#8211; not to delete it in any technical sense of the word. As such, we probably should provide a more explicit representation of this task in the user interface than just selecting a row in some grid and clicking the &#8216;delete&#8217; button (and &#8220;Are you sure?&#8221; isn&#8217;t it).</p>
<p>As we broaden our perspective to more parts of the system, we see this same pattern repeating:</p>
<blockquote><p>
Orders aren&#8217;t deleted &#8211; they&#8217;re cancelled. There may also be fees incurred if the order is canceled too late.</p>
<p>Employees aren&#8217;t deleted &#8211; they&#8217;re fired (or possibly retired). A compensation package often needs to be handled.</p>
<p>Jobs aren&#8217;t deleted &#8211; they&#8217;re filled (or their requisition is revoked).
</p></blockquote>
<p>In all cases, the thing we should focus on is the task the user wishes to perform, rather than on the technical action to be performed on one entity or another. In almost all cases, more than one entity needs to be considered.</p>
<h3>Statuses</h3>
<p>In all the examples above, what we see is a replacement of the technical action &#8216;delete&#8217; with a relevant business action. At the entity level, instead of having a (hidden) technical WasDeleted status, we see an explicit business status that users need to be aware of.</p>
<p>The manager of the warehouse needs to know that a product is discontinued so that they don&#8217;t order any more stock from the supplier. In today&#8217;s world of retail with Vendor Managed Inventory, this often happens together with a modification to an agreement with the vendor, or possibly a cancellation of that agreement. </p>
<p>This isn&#8217;t just a case of transactional or reporting boundaries &#8211; users in different contexts need to see different things at different times as the status changes to reflect the entity&#8217;s place in the business lifecycle. Customers shouldn&#8217;t see discontinued products at all. Warehouse workers should, that is, until the corresponding Stock Keeping Unit (SKU) has been revoked (another status) after we&#8217;ve sold all the inventory we wanted (and maybe returned the rest back to the supplier).</p>
<h3>Rules and Validation</h3>
<p>When looking at the world through over-simplified-delete-glasses, we may consider the logic dictating when we can delete to be quite simple: do some role-based-security checks, check that the entity exists, delete. Piece of cake.</p>
<p>The real world is a bigger, more complicated cake.</p>
<p>Let&#8217;s consider deleting an order, or rather, canceling it. On top of the regular security checks, we&#8217;ve got some rules to consider:</p>
<blockquote><p>
If the order has already been delivered, check if the customer isn&#8217;t happy with what they got, and go about <b>returning</b> the order. </p>
<p>If the order contained products &#8220;made to order&#8221;, charge the customer for a portion (or all) of the order (based on other rules).</p>
<p>And more&#8230;
</p></blockquote>
<p>Deciding what the next status should be may very well depend on the current business status of the entity. Deciding if that change of state is allowed is context and time specific &#8211; at one point in time the task may have been allowed, but later not. The logic here is not necessarily entirely related to the entity being &#8220;deleted&#8221; &#8211; there may be other entities which need to be checked, and whose status may also need  to be changed as well.</p>
<h3>Summary</h3>
<p>I know that some of you are thinking, &#8220;my system isn&#8217;t that complex &#8211; we can just delete and be done with it&#8221;.</p>
<p>My question to you would be, have you asked your users <b>why</b> they&#8217;re deleting things? Have you asked them about additional statuses and rules dictating how entities move as groups between them? You don&#8217;t want the success of your project to be undermined by that kind of unfounded assumption, do you?</p>
<p>The reason we&#8217;re given budgets to build business applications is because of the richness in business rules and statuses that ultimately provide value to users and a competitive advantage to the business. If that value wasn&#8217;t there, wouldn&#8217;t we be serving our users better by just giving them Microsoft Access?</p>
<p>In closing, given that you&#8217;re not giving your users MS Access, don&#8217;t think about deleting entities. Look for the reason why. Understand the different statuses that entities move between. Ask which users need to care about which status. I know it doesn&#8217;t show up as nicely on your resume as &#8220;3 years WXF&#8221;, but &#8220;saved the company $4 million in wasted inventory&#8221; does speak volumes.</p>
<p>One last sentence: Don&#8217;t delete. Just don&#8217;t.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/09/01/dont-delete-just-dont/feed/</wfw:commentRss>
		<slash:comments>56</slash:comments>
		</item>
		<item>
		<title>Convention over Configuration &#8211; The Next Generation?</title>
		<link>http://www.udidahan.com/2009/08/15/convention-over-configuration-the-next-generation/</link>
		<comments>http://www.udidahan.com/2009/08/15/convention-over-configuration-the-next-generation/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 18:13:24 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Agile]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1081</guid>
		<description><![CDATA[
Convention over configuration describes a style of development made popular by Ruby on Rails which has gained a great deal of traction in the .net ecosystem. After using frameworks designed in this way, I can say that the popularity is justified &#8211; it is much more pleasurable developing this way. 
The thing is, when looking [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/PicardKirk.jpg" alt="PicardKirk" title="PicardKirk" width="160" height="103" style="float:right; margin-left:10px; margin-bottom:10px; " /><br />
Convention over configuration describes a style of development made popular by Ruby on Rails which has gained a great deal of traction in the .net ecosystem. After using frameworks designed in this way, I can say that the popularity is justified &#8211; it is much more pleasurable developing this way. </p>
<p>The thing is, when looking at this in light of the full software development lifecycle, there are signs that the waters run deeper than we might have originally thought.</p>
<p>Let&#8217;s take things one step at a time though&#8230;</p>
<h3>What is it?</h3>
<p><a href="http://en.wikipedia.org/wiki/Convention_over_configuration">Wikipedia tells us</a>:</p>
<blockquote><p>&#8220;Convention over Configuration (aka Coding by convention) is a software design paradigm which seeks to decrease the number of decisions that developers need to make, gaining simplicity, but not necessarily losing flexibility. The phrase essentially means a developer only needs to specify unconventional aspects of the application.&#8221;</p></blockquote>
<p>What this means is that frameworks built in this way have default implementations that can be swapped out if needed. So far so good.</p>
<h3>For example&#8230;</h3>
<p>In <a href="http://www.NServiceBus.com">NServiceBus</a>, there is an abstraction for how subscription data is stored and multiple implementations &#8211; one in-memory, another using a durable MSMQ queue, and a third which uses a database. The convention for that part of the system is that the MSMQ implementation will be used, unless something else is specified. </p>
<p>Developers wishing to specify a different implementation can specify the desired implementation in the container &#8211; either one that comes out of the box, or their own implementation of ISubscriptionStorage.</p>
<p>Things get more interesting when we consider the full lifecycle.</p>
<h3>Lifecycle effects</h3>
<p>When developers are in the early phases of writing a new service, they want to focus primarily on what the service does &#8211; its logic. They don&#8217;t want to muck around with MSMQ queues for storing subscriptions and would much rather use the in-memory storage. </p>
<p>As the service takes shape and the developers want to run the full service on their machine, possibly testing basic fault-tolerance behaviors &#8211; kill one service, see that the others get a timeout, bring the service back up, wanting it to maintain all the previous subscriptions.</p>
<p>Moving on from there, our developers want to take the same system they just tested on their machine and move it into a staging environment. There, they don&#8217;t want to use the MSMQ implementation for subscription storage, but rather the database implementation &#8211; as will be used in the production environment. </p>
<p>While it may not sound like a big deal &#8211; changing the code which specifies which implementation to use when moving from one environment to another, consider that on top of just subscription storage, there is logging (output to console, file, db?), saga persistence (in-memory, file-based DB, relational DB), and more.</p>
<p>It&#8217;s actually quite likely that something will get missed as we move the system between environments. Can there be a better way?</p>
<h3>What if&#8230;</h3>
<p>What if there was some way for the developer to express their intent to the system, and the system could change its conventions, without the developer having to change any code or configuration files?</p>
<p>You might compare this (in concept) to debug builds and release builds. Same code, same config, but the runtime behaves different between the two.</p>
<p>As I mulled over how we could capture that intent without any code or config changes, the solution that I kept coming to seemed too trivial at first, so I dismissed it. Yet, it was the simplest one that would work for console and WinForms applications, as well as windows services &#8211; command line arguments. The only thing is that I don&#8217;t think those are available for web applications.</p>
<p>But since we&#8217;re still in &#8220;what if&#8221; land, and I&#8217;m more thinking out loud here than providing workable solutions for tomorrow morning, let&#8217;s &#8220;what if&#8221; command line arguments worked for web apps too.</p>
<h3>Command-Line Intent</h3>
<p>Going back to our original scenario, when developers are working on the logic of the service, they run it using the generic NServiceBus host process, passing it the command line parameter /lite (or whatever). The host then automatically configures all the in-memory implementations. </p>
<p>As the system progresses, when the developer wants to run everything on their machine, they run the processes with /integration. The host then configures the appropriate implementations (MSMQ for subscription storage, SQLite for saga persistence, etc. </p>
<p>When the developers want to run the system in production, they could specify /production (or maybe that could be the default?), and the database backed implementations would be configured.</p>
<h3>Imagine&#8230;</h3>
<p>Imagine being able to move that fluidly from one environment to another. Not needing to pore over configuration files or startup script code which configures a zillion implementation details. Not needing to worry that as you moved the system to staging something would break.</p>
<p>Imagine short, frictionless iterations even for large scale systems.</p>
<p>Imagine &#8211; lifecycle-aware frameworks making all this imagination a reality.</p>
<h3>In Closing</h3>
<p>We&#8217;re not there yet &#8211; but we&#8217;re not that far either. The generic host we&#8217;re providing with NServiceBus 2.0 is now being extended to support exactly these scenarios. </p>
<p>It&#8217;s my hope that as more of us think about this challenge, we&#8217;ll come up with better solutions and more intelligent frameworks. Just as convention came to our rescue before, breaking us out of the pain of endless XML configuration, I hope this new family of lifecycle-aware frameworks will make the friction of moving a system through dev, test, staging, and production a thing of the past.</p>
<p>A worthy problem for us all to solve, don&#8217;t you think?</p>
<p>Any ideas on how to make it a reality?<br />
Send them in &#8211; leave a comment below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/08/15/convention-over-configuration-the-next-generation/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>MSDN Magazine Domain Model Article</title>
		<link>http://www.udidahan.com/2009/08/02/msdn-magazine-domain-model-article/</link>
		<comments>http://www.udidahan.com/2009/08/02/msdn-magazine-domain-model-article/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 14:11:45 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Articles]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Databases]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1063</guid>
		<description><![CDATA[
My article on “employing the domain model pattern” has been published in the August edition of MSDN Magazine.
Here’s a short excerpt:
“In this article, we’ll go through the reasons to (and not to) employ the domain model pattern, the benefits it brings, as well as provide some practical tips on keeping the overall solution as simple [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/magazine/ee236415.aspx"><img title="MSDN magazine" style="border-right: 0px; border-top: 0px; display: inline; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="346" alt="MSDN magazine" src="http://www.udidahan.com/wp-content/uploads/msdn_magazine_domain_model.gif" width="263" align="right" border="0" /></a></p>
<p>My article on “employing the domain model pattern” has been published in the August edition of MSDN Magazine.</p>
<p>Here’s a short excerpt:</p>
<blockquote><p>“In this article, we’ll go through the reasons to (and not to) employ the domain model pattern, the benefits it brings, as well as provide some practical tips on keeping the overall solution as simple as possible.”</p></blockquote>
<p><a href="http://msdn.microsoft.com/en-us/magazine/ee236415.aspx">Continue reading… </a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/08/02/msdn-magazine-domain-model-article/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Domain Events &#8211; Salvation</title>
		<link>http://www.udidahan.com/2009/06/14/domain-events-salvation/</link>
		<comments>http://www.udidahan.com/2009/06/14/domain-events-salvation/#comments</comments>
		<pubDate>Sun, 14 Jun 2009 06:25:31 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[Data Access]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Testing]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1029</guid>
		<description><![CDATA[
I&#8217;ve been hearing from people that have had a great deal of success using the Domain Event pattern and the infrastructure I previously provided for it in Domain Events &#8211; Take 2. I&#8217;m happy to say that I&#8217;ve got an improvement that I think you&#8217;ll like. The main change is that now we&#8217;ll be taking [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.udidahan.com/wp-content/uploads/sphere1.jpg" alt="sphere" title="sphere" width="198" height="201"  style="border-right: 0px; border-top: 0px; margin: 0px 10px; border-left: 0px; border-bottom: 0px" align="right"/><br />
I&#8217;ve been hearing from people that have had a great deal of success using the Domain Event pattern and the infrastructure I previously provided for it in <a href="http://www.udidahan.com/2008/08/25/domain-events-take-2/">Domain Events &#8211; Take 2</a>. I&#8217;m happy to say that I&#8217;ve got an improvement that I think you&#8217;ll like. The main change is that now we&#8217;ll be taking an approach that is reminiscent to how events are published in <a href="http://www.NServiceBus.com">NServiceBus</a>.</p>
<h3>Background</h3>
<p>Before diving right into the code, I wanted to take a minute to recall how we got here.</p>
<p>It started by looking for <a href="http://www.udidahan.com/2008/02/29/how-to-create-fully-encapsulated-domain-models/">how to create fully encapsulated domain models</a>.</p>
<p>The main assertion being that you do *not* need to inject anything into your domain entities.</p>
<p>Not services. Not repositories. Nothing.</p>
<p>Just pure domain model goodness.</p>
<h3>Make Roles Explicit</h3>
<p>I&#8217;m going to take the advice I so often give. A domain event is a role, and thus should be represented explicitly:</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">interface</span> IDomainEvent {}</pre>
</div>
<p>If this reminds you of the IMessage marker interface in nServiceBus, you&#8217;re beginning to see where this is going&#8230;</p>
<h3>How to define domain events</h3>
<p>A domain event is just a simple POCO that represents an interesting occurence in the domain. For example:</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> CustomerBecamePreferred : IDomainEvent </pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>    <span class="kwrd">public</span> Customer Customer { get; set; }</pre>
<pre><span class="lnum">   4:  </span>}</pre>
</div>
<p>For those of you concerned about the number of events you may have, and therefore are thinking about bunching up these events by namespaces or things like that, slow down. The number of domain events and their cohesion is directly related to that of the domain model. </p>
<p>If you feel the need to split your domain events up, there&#8217;s a good chance that you should be looking at splitting your domain model too. This is the bottom-up way of identifying bounded contexts.</p>
<h3>How to raise domain events</h3>
<p>In your domain entities, when a significant state change happens you&#8217;ll want to raise your domain events 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> Customer</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>    <span class="kwrd">public</span> <span class="kwrd">void</span> DoSomething()</pre>
<pre><span class="lnum">   4:  </span>    {</pre>
<pre class="alt"><span class="lnum">   5:  </span>        DomainEvents.Raise(<span class="kwrd">new</span> CustomerBecamePreferred() { Customer = <span class="kwrd">this</span> });</pre>
<pre><span class="lnum">   6:  </span>    }</pre>
<pre class="alt"><span class="lnum">   7:  </span>}</pre>
</div>
<p>We&#8217;ll look at the DomainEvents class in just a second, but I&#8217;m guessing that some of you are wondering &#8220;how did that entity get a reference to that?&#8221; The answer is that DomainEvents is a static class. &#8220;OMG, static?! But doesn&#8217;t that hurt testability?!&#8221; No, it doesn&#8217;t. Here, look:</p>
<h3>Unit testing with domain events</h3>
<p>One of the things we&#8217;d like to check when unit testing our domain entities is that the appropriate events are raised along with the corresponding state changes. Here&#8217;s an example:</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">void</span> DoSomethingShouldMakeCustomerPreferred()</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>    var c = <span class="kwrd">new</span> Customer();</pre>
<pre><span class="lnum">   4:  </span>    Customer preferred = <span class="kwrd">null</span>;</pre>
<pre class="alt"><span class="lnum">   5:  </span>&nbsp;</pre>
<pre><span class="lnum">   6:  </span>    DomainEvents.Register&lt;CustomerBecamePreferred&gt;(</pre>
<pre class="alt"><span class="lnum">   7:  </span>        p =&gt; preferred = p.Customer</pre>
<pre><span class="lnum">   8:  </span>            );</pre>
<pre class="alt"><span class="lnum">   9:  </span>&nbsp;</pre>
<pre><span class="lnum">  10:  </span>    c.DoSomething();</pre>
<pre class="alt"><span class="lnum">  11:  </span>    Assert(preferred == c &amp;&amp; c.IsPreferred);</pre>
<pre><span class="lnum">  12:  </span>}</pre>
</div>
<p>As you can see, the static DomainEvents class is used in unit tests as well. Also notice that you don&#8217;t need to mock anything &#8211; pure testable bliss.</p>
<h3>Who handles domain events</h3>
<p>First of all, consider that when some service layer object calls the DoSomething method of the Customer class, it doesn&#8217;t necessarily know which, if any, domain events will be raised. All it wants to do is its regular schtick:</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">void</span> Handle(DoSomethingMessage msg)</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>    <span class="kwrd">using</span> (ISession session = SessionFactory.OpenSession())</pre>
<pre><span class="lnum">   4:  </span>    <span class="kwrd">using</span> (ITransaction tx = session.BeginTransaction())</pre>
<pre class="alt"><span class="lnum">   5:  </span>    {</pre>
<pre><span class="lnum">   6:  </span>        var c = session.Get&lt;Customer&gt;(msg.CustomerId);</pre>
<pre class="alt"><span class="lnum">   7:  </span>        c.DoSomething();</pre>
<pre><span class="lnum">   8:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   9:  </span>        tx.Commit();</pre>
<pre><span class="lnum">  10:  </span>    }</pre>
<pre class="alt"><span class="lnum">  11:  </span>}</pre>
</div>
<p>The above code complies with the Single Responsibility Principle, so the business requirement which states that when a customer becomes preferred, they should be sent an email belongs somewhere else. </p>
<p>Notice that the key word in the requirement &#8211; &#8220;when&#8221;.</p>
<p>Any time you see that word in relation to your domain, consider modeling it as a domain event.</p>
<p>So, here&#8217;s the handling 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> CustomerBecamePreferredHandler : Handles&lt;CustomerBecamePreferred&gt;</pre>
<pre><span class="lnum">   2:  </span>{ </pre>
<pre class="alt"><span class="lnum">   3:  </span>   <span class="kwrd">public</span> <span class="kwrd">void</span> Handle(CustomerBecamePreferred args)</pre>
<pre><span class="lnum">   4:  </span>   {</pre>
<pre class="alt"><span class="lnum">   5:  </span>      <span class="rem">// send email to args.Customer</span></pre>
<pre><span class="lnum">   6:  </span>   }</pre>
<pre class="alt"><span class="lnum">   7:  </span>} </pre>
</div>
<p>This code will run no matter which service layer object we came in through.</p>
<p>Here&#8217;s the interface it implements:</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">interface</span> Handles&lt;T&gt; <span class="kwrd">where</span> T : IDomainEvent</pre>
<pre><span class="lnum">   2:  </span>{</pre>
<pre class="alt"><span class="lnum">   3:  </span>    <span class="kwrd">void</span> Handle(T args); </pre>
<pre><span class="lnum">   4:  </span>} </pre>
</div>
<p>Fairly simple.</p>
<p>Please be aware that the above code will be run on the same thread within the same transaction as the regular domain work so you should avoid performing any blocking activities, like using SMTP or web services. Instead, prefer using one-way messaging to communicate to something else which does those blocking activities.</p>
<p>Also, you can have multiple classes handling the same domain event. If you need to send email *and* call the CRM system *and* do something else, etc, you don&#8217;t need to change any code &#8211; just write a new handler. This keeps your system quite a bit more stable than if you had to mess with the original handler or, heaven forbid, service layer code.</p>
<h3>Where domain event handlers go</h3>
<p>These handler classes do not belong in the domain model.</p>
<p>Nor do they belong in the service layer.</p>
<p>Well, that&#8217;s not entirely accurate &#8211; you see, there&#8217;s no *the* service layer. There is the part that accepts messages from clients and calls methods on the domain model. And there is another, independent part that handles events from the domain. Both of these will probably make use of a message bus, but that implementation detail shouldn&#8217;t deter you from keeping each in their own package.</p>
<h3>The infrastructure</h3>
<p>I know you&#8217;ve been patient, reading through all my architectural blah-blah, so here 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">static</span> <span class="kwrd">class</span> DomainEvents</pre>
<pre><span class="lnum">   2:  </span>{ </pre>
<pre class="alt"><span class="lnum">   3:  </span>    [ThreadStatic] <span class="rem">//so that each thread has its own callbacks</span></pre>
<pre><span class="lnum">   4:  </span>    <span class="kwrd">private</span> <span class="kwrd">static</span> List&lt;Delegate&gt; actions;</pre>
<pre class="alt"><span class="lnum">   5:  </span>&nbsp;</pre>
<pre><span class="lnum">   6:  </span>    <span class="kwrd">public</span> <span class="kwrd">static</span> IContainer Container { get; set; } <span class="rem">//as before</span></pre>
<pre class="alt"><span class="lnum">   7:  </span>&nbsp;</pre>
<pre><span class="lnum">   8:  </span>    <span class="rem">//Registers a callback for the given domain event</span></pre>
<pre class="alt"><span class="lnum">   9:  </span>    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Register&lt;T&gt;(Action&lt;T&gt; callback) <span class="kwrd">where</span> T : IDomainEvent</pre>
<pre><span class="lnum">  10:  </span>    {</pre>
<pre class="alt"><span class="lnum">  11:  </span>       <span class="kwrd">if</span> (actions == <span class="kwrd">null</span>)</pre>
<pre><span class="lnum">  12:  </span>          actions = <span class="kwrd">new</span> List&lt;Delegate&gt;();</pre>
<pre class="alt"><span class="lnum">  13:  </span>&nbsp;</pre>
<pre><span class="lnum">  14:  </span>       actions.Add(callback);</pre>
<pre class="alt"><span class="lnum">  15:  </span>   }</pre>
<pre><span class="lnum">  16:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  17:  </span>   <span class="rem">//Clears callbacks passed to Register on the current thread</span></pre>
<pre><span class="lnum">  18:  </span>   <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> ClearCallbacks ()</pre>
<pre class="alt"><span class="lnum">  19:  </span>   {</pre>
<pre><span class="lnum">  20:  </span>       actions = <span class="kwrd">null</span>;</pre>
<pre class="alt"><span class="lnum">  21:  </span>   }</pre>
<pre><span class="lnum">  22:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">  23:  </span>   <span class="rem">//Raises the given domain event</span></pre>
<pre><span class="lnum">  24:  </span>   <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> Raise&lt;T&gt;(T args) <span class="kwrd">where</span> T : IDomainEvent</pre>
<pre class="alt"><span class="lnum">  25:  </span>   {</pre>
<pre><span class="lnum">  26:  </span>      <span class="kwrd">if</span> (Container != <span class="kwrd">null</span>)</pre>
<pre class="alt"><span class="lnum">  27:  </span>         <span class="kwrd">foreach</span>(var handler <span class="kwrd">in</span> Container.ResolveAll&lt;Handles&lt;T&gt;&gt;())</pre>
<pre><span class="lnum">  28:  </span>            handler.Handle(args);</pre>
<pre class="alt"><span class="lnum">  29:  </span>&nbsp;</pre>
<pre><span class="lnum">  30:  </span>      <span class="kwrd">if</span> (actions != <span class="kwrd">null</span>)</pre>
<pre class="alt"><span class="lnum">  31:  </span>          <span class="kwrd">foreach</span> (var action <span class="kwrd">in</span> actions)</pre>
<pre><span class="lnum">  32:  </span>              <span class="kwrd">if</span> (action <span class="kwrd">is</span> Action&lt;T&gt;)</pre>
<pre class="alt"><span class="lnum">  33:  </span>                  ((Action&lt;T&gt;)action)(args);</pre>
<pre><span class="lnum">  34:  </span>   }</pre>
<pre class="alt"><span class="lnum">  35:  </span>} </pre>
</div>
<p>Notice that while this class *can* use a container, the container isn&#8217;t needed for unit tests which use the Register method.</p>
<p>When used server side, please make sure that you add a call to ClearCallbacks in your infrastructure&#8217;s end of message processing section. In nServiceBus this is done with a message module like the one below:</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> DomainEventsCleaner : IMessageModule</pre>
<pre><span class="lnum">   2:  </span>{ </pre>
<pre class="alt"><span class="lnum">   3:  </span>    <span class="kwrd">public</span> <span class="kwrd">void</span> HandleBeginMessage() { }</pre>
<pre><span class="lnum">   4:  </span>&nbsp;</pre>
<pre class="alt"><span class="lnum">   5:  </span>    <span class="kwrd">public</span> <span class="kwrd">void</span> HandleEndMessage()</pre>
<pre><span class="lnum">   6:  </span>    {</pre>
<pre class="alt"><span class="lnum">   7:  </span>        DomainEvents.ClearCallbacks();</pre>
<pre><span class="lnum">   8:  </span>    }</pre>
<pre class="alt"><span class="lnum">   9:  </span>}</pre>
</div>
<p>The main reason for this cleanup is that someone just might want to use the Register API in their original service layer code rather than writing a separate domain event handler.</p>
<h3>Summary</h3>
<p>Like all good things in life, 3rd time&#8217;s the charm.</p>
<p>It took a couple of iterations, and the API did change quite a bit, but the overarching theme has remained the same &#8211; keep the domain model focused on domain concerns. While some might say that there&#8217;s only a slight technical difference between calling a service (IEmailService) and using an event to dispatch it elsewhere, I beg to differ.</p>
<p>These domain events are a part of the ubiquitous language and should be represented explicitly.</p>
<p>CustomerBecamePreferred is nothing at all like IEmailService.</p>
<p>In working with your domain experts or just going through a requirements document, pay less attention to the nouns and verbs that Object-Oriented Analysis &#038; Design call attention to, and keep an eye out for the word &#8220;when&#8221;. It&#8217;s a critically important word that enables us to model important occurrences and state changes.</p>
<p>What do you think? Are you already using this approach? Have you already tried it and found it broken in some way? Do you have any suggestions on how to improve it?</p>
<p>Let me know &#8211; leave a comment below.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/06/14/domain-events-salvation/feed/</wfw:commentRss>
		<slash:comments>133</slash:comments>
		</item>
		<item>
		<title>The Fallacy Of ReUse</title>
		<link>http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/</link>
		<comments>http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/#comments</comments>
		<pubDate>Sun, 07 Jun 2009 08:40:16 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Autonomous Services]]></category>
		<category><![CDATA[EDA]]></category>
		<category><![CDATA[OO]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1026</guid>
		<description><![CDATA[This industry is pre-occupied with reuse.
There&#8217;s this belief that if we just reused more code, everything would be better.
Some even go so far as saying that the whole point of object-orientation was reuse &#8211; it wasn&#8217;t, encapsulation was the big thing. After that component-orientation was the thing that was supposed to make reuse happen. Apparently [...]]]></description>
			<content:encoded><![CDATA[<p>This industry is pre-occupied with reuse.</p>
<p>There&#8217;s this belief that if we just reused more code, everything would be better.</p>
<p>Some even go so far as saying that the whole point of object-orientation was reuse &#8211; it wasn&#8217;t, encapsulation was the big thing. After that component-orientation was the thing that was supposed to make reuse happen. Apparently that didn&#8217;t pan out so well either because here we are now pinning our reuseful hopes on service-orientation.</p>
<p>Entire books of patterns have been written on how to achieve reuse with the orientation of the day.<br />
Services have been classified every which way in trying to achieve this, from entity services and activity services, through process services and orchestration services. Composing services has been touted as the key to reusing, and creating reusable services.</p>
<p>I might as well let you in on the dirty-little secret:</p>
<h3>Reuse is a fallacy</h3>
<p>Before running too far ahead, let&#8217;s go back to what the actual goal of reuse was: getting done faster.</p>
<p>That&#8217;s it.</p>
<p>It&#8217;s a fine goal to have.</p>
<p>And here&#8217;s how reuse fits in to the picture:</p>
<blockquote><p>
If we were to write all the code of a system, we&#8217;d write a certain amount of code.<br />
If we could reuse some code from somewhere else that was written before, we could write less code.<br />
The more code we can reuse, the less code we write.<br />
The less code we write, the sooner we&#8217;ll be done!
</p></blockquote>
<p>However, the above logical progression is based on another couple of fallacies:</p>
<h3>Fallacy: All code takes the same amount of time to write</h3>
<h3>Fallacy: Writing code is the primary activity in getting a system done</h3>
<p>Anyone who&#8217;s actually written some code that&#8217;s gone into production knows this.</p>
<p>There&#8217;s the time it takes us to understand what the system should do.<br />
Multiply that by the time it takes the users to understand what the system should do <img src='http://www.udidahan.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
Then there&#8217;s the integrating that code with all the other code, databases, configuration, web services, etc.<br />
Debugging. Deploying. Debugging. Rebugging. Meetings. Etc.</p>
<p>Writing code is actually the least of our worries.<br />
We actually spend less time writing code than&#8230;</p>
<h3>Rebugging code</h3>
<p>Also known as bug regressions.</p>
<p>This is where we fix one piece of code, and in the process break another piece of code.<br />
It&#8217;s not like we do it on purpose. It&#8217;s all those dependencies between the various bits of code.<br />
The more dependencies there are, the more likely something&#8217;s gonna break.<br />
Especially when we have all sorts of hidden dependencies,<br />
like when other code uses stuff we put in the database without asking us what it means,<br />
or, heaven forbid, changing it without telling us.</p>
<p>These debugging/rebugging cycles can make stabilizing a system take a long time.</p>
<p>So, how does reuse help/hinder with that?</p>
<p>Here&#8217;s how:</p>
<h3>Dependencies multiply by reuse</h3>
<p>It&#8217;s to be expected. If you wrote the code all in one place, there are no dependencies. By reusing code, you&#8217;ve created a dependency. The more you reuse, the more dependencies you have. The more dependencies, the more rebugging.</p>
<p>Of course, we need to keep in mind the difference between&#8230;</p>
<h3>Reuse &#038; Use</h3>
<p>Your code <b>uses</b> the runtime API (JDK, .NET BCL, etc).<br />
Likewise other frameworks like (N)Hibernate, Spring, WCF, etc.</p>
<p>Reuse happens when you extend and override existing behaviors within other code.<br />
This is most often done by inheritance in OO languages.</p>
<p>Interestingly enough, by the above generally accepted definition, most web services &#8220;reuse&#8221; is actually really use.</p>
<p>Let&#8217;s take a look at the characteristics of the code we&#8217;re using and reusing to see where we get the greatest value:</p>
<h3>The value of (re)use</h3>
<p>If we were to (re)use a piece of code in only one part of our system, it would be safe to say that we would get less value than if we could (re)use it in more places. For example, we could say that for many web applications, the web framework we use provides more value than a given encryption algorithm that we may use in only a few places.</p>
<p>So, what characterizes the code we use in many places?</p>
<p>Well, it&#8217;s very <b>generic</b>.</p>
<p>Actually, the more generic a piece of code, the less likely it is that we&#8217;ll be changing something in it when fixing a bug in the system.</p>
<p><b>That&#8217;s important</b>.</p>
<p>However, when looking at the kind of code we reuse, and the reasons around it, we tend to see very <b>non-generic</b> code &#8211; something that deals with the domain-specific behaviors of the system. Thus, the likelihood of a bug fix needing to touch that code is higher than in the generic/use-not-reuse case, often much higher.</p>
<h3>How it all fits together</h3>
<blockquote><p>
Goal:&#09;Getting done faster<br />
Via:&#09;Spending less time debugging/rebugging/stabilizing<br />
Via:&#09;Having less dependencies reasonably requiring a bug fix to touch the dependent side<br />
Via:&#09;Not reusing non-generic code
</p></blockquote>
<p>This doesn&#8217;t mean you shouldn&#8217;t use generic code / frameworks where applicable &#8211; absolutely, you should.<br />
Just watch the number of kind of dependencies you introduce.</p>
<h3>Back to services</h3>
<p>So, if we follow the above advice with services, we wouldn&#8217;t want domain specific services reusing each other.<br />
If we could get away with it, we probably wouldn&#8217;t even want them using each other either.</p>
<p>As use and reuse go down, we can see that service autonomy goes up. And vice-versa.<br />
Luckily, we have service interaction mechanisms from Event-Driven Architecture that enable use without breaking autonomy.<br />
Autonomy is actually very similar to the principle of encapsulation that drove object-orientation in the first place.<br />
Interesting, isn&#8217;t it?</p>
<h3>In summary</h3>
<p>We all want to get done faster.</p>
<p>Way back when, someone told us reuse was the way to do that.</p>
<p>They were wrong.</p>
<p>Reuse may make sense in the most tightly coupled pieces of code you have, but not very much anywhere else.</p>
<p>When designing services in your SOA, stay away from reuse, and minimize use (with EDA patterns).</p>
<p>The next time someone pulls the &#8220;reuse excuse&#8221;, you&#8217;ll be ready.</p>
<hr size="1" />
<h3>Further Reading</h3>
<ul>
<li><a href="http://www.udidahan.com/2008/10/22/additional-logic-required-for-service-autonomy/">Additional logic required for service autonomy</a></li>
<li><a href="http://www.udidahan.com/2008/12/13/self-contained-events-and-soa/">Self-contained events &#038; SOA</a></li>
<li><a href="http://msdn2.microsoft.com/en-us/arcjournal/bb245672">Autonomous Services and Enterprise Entity Aggregation</a> [MS Architecture Journal]</li>
<li><a href="http://udidahan.weblogs.us/2006/05/26/podcast-does-soa-mean-the-end-of-oo/">Does SOA mean the end of OO?</a> [Podcast]</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/06/07/the-fallacy-of-reuse/feed/</wfw:commentRss>
		<slash:comments>49</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>Backwards-Compatibility: Why Most Versioning Problems Aren&#8217;t</title>
		<link>http://www.udidahan.com/2009/04/10/backwards-compatibility-why-most-versioning-problems-arenrsquot/</link>
		<comments>http://www.udidahan.com/2009/04/10/backwards-compatibility-why-most-versioning-problems-arenrsquot/#comments</comments>
		<pubDate>Fri, 10 Apr 2009 13:17:17 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Autonomous Services]]></category>
		<category><![CDATA[SOA]]></category>
		<category><![CDATA[Simplicity]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/04/10/backwards-compatibility-why-most-versioning-problems-arenrsquot/</guid>
		<description><![CDATA[
I’ve been to too many clients where I’ve been brought in to help them with their problems around service versioning when the solution I propose is simply to have version N+1 of the system be backwards-compatible with version N. If two adjacent versions of a given system aren’t compatible with each other, it is practically [...]]]></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="244" alt="image" src="http://www.udidahan.com/wp-content/ServicesVersioningPubSubandMultipleInher_11E4C/image.png" width="244" align="right" border="0" />
<p>I’ve been to too many clients where I’ve been brought in to help them with their problems around service versioning when the solution I propose is simply to have version N+1 of the system be backwards-compatible with version N. If two adjacent versions of a given system aren’t compatible with each other, it is practically impossible to solve versioning issues.</p>
<p>Here’s what happens when versions aren’t compatible:</p>
<blockquote><p>Admins stop the system from accepting any new requests, and wait until all current requests are done processing. They take a backup/snapshot of all relevant parts of the system (like data in the DB). Then, bring down the system – all of it. Install the new version on all machines. Bring everything back up. Let the users back in.</p></blockquote>
<p>If, heaven-forbid, problems were uncovered with the new version (since some problems only appear in production), the admins have to roll back to the previous version – once again bringing everything down.</p>
<p>This scenario is fairly catastrophic for any company that requires not-even high availability, but pretty continuous availability – like public facing web apps.</p>
<p>If adjacent versions were compatible with each other, we could upgrade the system piece-meal – machine by machine, where both the old and new versions will be running side by side, communicating with each other. While the system’s performance may be sub-optimal, it will continue to be available throughout upgrades as well as downgrades.</p>
<p>This isn’t trivial to do.</p>
<p>It impacts how you decide what is (and more importantly, what isn’t) nullable.</p>
<p>It may force you to spread certain changes to features across more versions (aka releases).</p>
<p>As such, you can expect this to affect how you do release and feature planning.</p>
<p>However, if you do not take these factors into account, it’s almost a certainty that your versioning problems will persist and no technology (new or old) will be able to solve them.</p>
<p>Coming next… Units of versioning – inside and outside a service.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/04/10/backwards-compatibility-why-most-versioning-problems-arenrsquot/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>MSDN Magazine Smart Client Article</title>
		<link>http://www.udidahan.com/2009/03/28/msdn-magazine-smart-client-article/</link>
		<comments>http://www.udidahan.com/2009/03/28/msdn-magazine-smart-client-article/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 19:16:39 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[ESB]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Pub/Sub]]></category>
		<category><![CDATA[Scalability]]></category>
		<category><![CDATA[Smart Client]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Web Services]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/03/28/msdn-magazine-smart-client-article/</guid>
		<description><![CDATA[
My article on “optimizing a large-scale Software+Services application” has been published in the April edition of MSDN Magazine.
Here’s a short excerpt:
“We had to juggle occasional connectivity, data synchronization, and publish/subscribe all at the same time. We learned that we couldn’t solve all problems either client-side or server-side, but rather that an integrated approach was needed [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/magazine/dd569749.aspx"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; margin: 0px 0px 10px 10px; border-left: 0px; border-bottom: 0px" height="244" alt="image" src="http://www.udidahan.com/wp-content/uploads/MSDNMagazineSmartClientArticle_13E17/image.png" width="189" align="right" border="0" /></a></p>
<p>My article on “optimizing a large-scale Software+Services application” has been published in the April edition of MSDN Magazine.</p>
<p>Here’s a short excerpt:</p>
<blockquote><p>“We had to juggle occasional connectivity, data synchronization, and publish/subscribe all at the same time. We learned that we couldn’t solve all problems either client-side or server-side, but rather that an integrated approach was needed since any changes on one side needed corresponding changes on the other side.”</p></blockquote>
<p><a href="http://msdn.microsoft.com/en-us/magazine/dd569749.aspx">Continue reading… </a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/03/28/msdn-magazine-smart-client-article/feed/</wfw:commentRss>
		<slash:comments>12</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>97 Things</title>
		<link>http://www.udidahan.com/2009/02/15/97-things/</link>
		<comments>http://www.udidahan.com/2009/02/15/97-things/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 16:04:37 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Community]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/2009/02/15/97-things/</guid>
		<description><![CDATA[It looks like one of the community projects that I&#8217;ve been involved with has reached maturity:

97 Things Every Software Architect Should Know

Collective Wisdom from the Experts


Definitely worth checking out.
]]></description>
			<content:encoded><![CDATA[<p>It looks like one of the community projects that I&#8217;ve been involved with has reached maturity:</p>
<p>
<div style="font-size:20px; font-weight:bold;">97 Things Every Software Architect Should Know</div>
<p><br/>
<div style="font-size:16px; font-weight:bold;">Collective Wisdom from the Experts</div>
</p>
<p><a href="http://www.amazon.com/dp/059652269X"><img src="http://www.udidahan.com/wp-content/uploads/97_things_architect.jpg" width="153" height="227" style="border:none;" /></a></p>
<p>Definitely worth checking out.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2009/02/15/97-things/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
