<?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; CQRS</title>
	<atom:link href="http://www.udidahan.com/category/cqrs/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>12</slash:comments>
		</item>
		<item>
		<title>CQRS, DDD, and NServiceBus video</title>
		<link>http://www.udidahan.com/2010/06/18/cqrs-ddd-and-nservicebus-video/</link>
		<comments>http://www.udidahan.com/2010/06/18/cqrs-ddd-and-nservicebus-video/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 11:11:19 +0000</pubDate>
		<dc:creator>udidahan</dc:creator>
				<category><![CDATA[CQRS]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Presentations]]></category>

		<guid isPermaLink="false">http://www.udidahan.com/?p=1306</guid>
		<description><![CDATA[Following the theme of my last few blog posts, this post will also be pointing you to videos of me talking.
After I had finished speaking at QCon London last March, I sat down for a short interview with the guys from InfoQ chatting about topics from CQRS, to DDD, to NServiceBus. I&#8217;m happy to say [...]]]></description>
			<content:encoded><![CDATA[<p>Following the theme of my last few blog posts, this post will also be pointing you to videos of me talking.</p>
<p>After I had finished speaking at QCon London last March, I sat down for a short interview with the guys from InfoQ chatting about topics from CQRS, to DDD, to NServiceBus. I&#8217;m happy to say that the interview is now online with a full (and mostly accurate) transcript as well as with an MP3 download link.</p>
<p>Get it here: <a href="http://www.infoq.com/interviews/dahan-cqrs-ddd-nservicebus">Udi Dahan on CQRS, DDD and NServiceBus</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.udidahan.com/2010/06/18/cqrs-ddd-and-nservicebus-video/feed/</wfw:commentRss>
		<slash:comments>0</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>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>
	</channel>
</rss>
