<?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>t h e D a v i d A i k e n &#187; Best Practices</title>
	<atom:link href="http://www.davidaiken.com/tag/best-practices/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davidaiken.com</link>
	<description>Not Statistically Significant</description>
	<lastBuildDate>Wed, 16 Nov 2011 04:04:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Implementing Windows Azure Retry Logic</title>
		<link>http://www.davidaiken.com/2011/10/10/implementing-windows-azure-retry-logic/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.davidaiken.com/2011/10/10/implementing-windows-azure-retry-logic/#comments</comments>
		<pubDate>Mon, 10 Oct 2011 20:02:44 +0000</pubDate>
		<dc:creator>theDavidAiken</dc:creator>
				<category><![CDATA[SQL Azure]]></category>
		<category><![CDATA[Windows Azure]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Retry Logic]]></category>

		<guid isPermaLink="false">http://www.davidaiken.com/?p=220</guid>
		<description><![CDATA[Windows Azure will automatically repair itself. Can your service? In this post I’m going to show you a simple way to make your service a little more resilient by adding retry logic.
Transient Datacenter conditions
When you have to deal with external services of any type there are times when the  [...]]]></description>
			<content:encoded><![CDATA[<p>Windows Azure will automatically repair itself. Can your service? In this post I’m going to show you a simple way to make your service a little more resilient by adding retry logic.</p>
<h1>Transient Datacenter conditions</h1>
<p>When you have to deal with external services of any type there are times when the service might not respond. This could be due to any number of reasons from network connectivity &amp; service throttling, to hardware failure. Windows Azure is designed to withstand these failures, not by avoiding them, but by taking corrective action when they do occur. Windows Azure auto heals itself. These conditions are sometimes referred to as transient conditions because they are not typically long lasting.</p>
<p>As an example, SQL Azure can give you <a href="http://msdn.microsoft.com/en-us/library/4cff491e-9359-4454-bd7c-fb72c4c452ca#bkmk_throt_errors" target="_blank">connection errors</a>, and <a href="http://msdn.microsoft.com/en-us/library/4cff491e-9359-4454-bd7c-fb72c4c452ca#bkmk_throt_errors" target="_blank">throttling errors</a>, Windows Azure Storage can give you timeout and throttling errors and Service Bus has <a href="http://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.serverbusyexception.aspx" target="_blank">ServerBusy</a> and <a href="http://msdn.microsoft.com/en-us/library/microsoft.servicebus.messaging.messagingcommunicationexception.aspx" target="_blank">MessagingCommunication</a> Exceptions.</p>
<p>Any other external dependency will also likely have similar conditions. Without defensive coding for these transient conditions, your app will suffer unnecessary outages. Fortunately the problem can be easily resolved.</p>
<h1>Retry Logic</h1>
<p>Handling these conditions is usually as easy as repeating the operation after a short delay. </p>
<p>The Windows Azure Storage Client Library that ships with the SDK already has <a href="http://blogs.msdn.com/b/windowsazurestorage/archive/2011/02/03/overview-of-retry-policies-in-the-windows-azure-storage-client-library.aspx" target="_blank">retry behavior</a> that you need to switch on. You can set this on any storage client by setting the <strong>RetryPolicy</strong> Property.</p>
<p>SQL Azure doesn’t provide a default retry mechanism out of the box, since it uses the SQL Server client libraries. Service Bus also doesn’t provide a retry mechanism.</p>
<h2>Transient Fault Handling Framework</h2>
<p>To provide an easy way to handle this, the Windows Azure Customer Advisory Team have developed a Transient Fault Handling Framework &#8211; <a title="http://windowsazurecat.com/2011/02/transient-fault-handling-framework/" href="http://windowsazurecat.com/2011/02/transient-fault-handling-framework/">http://windowsazurecat.com/2011/02/transient-fault-handling-framework/</a>. The framework provides a number of ways to handle specific SQL Azure, Storage, Service Bus and Cache conditions. </p>
<p>The most interesting aspect to me however is the <strong>ExecuteAction</strong> and <strong>ExecuteAction&lt;T&gt;</strong> methods. These methods allow you to basically wrap any user code in a retry block. Example:</p>
<pre style="width: 105.39%; height: 53px" class="csharpcode">var policy = <span class="kwrd">new</span> RetryPolicy&lt;SqlAzureTransientErrorDetectionStrategy&gt;(MaxRetries,
                    TimeSpan.FromMilliseconds(DelayMs));
policy.ExecuteAction(() =&gt; <span class="kwrd">object</span>.DoSomething());</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
</p>
<h2>Retry Pattern</h2>
<p>What is great about these methods are they enable you to use the <a href="http://en.wikipedia.org/wiki/Decorator_pattern" target="_blank">decorator pattern</a> to add retry logic to your service. This of course assumes you built your service with extensibility in mind. </p>
<p>In my example I have a <strong>UriRepository </strong>which is defined by the <strong>IUriRepository</strong> interface. I have a <strong>SQLAzureUriRepository</strong> that implements the interface. This class however contains no retry logic. Instead I implemented a <strong>RetryUriRepository</strong> that also implements <strong>IUriRepository</strong>. <strong>RetryUriRepository</strong> allows you to specify via constructor injection, which <strong>UriRepositiory</strong> to retry.</p>
<p>Here is a snippet of the <strong>RetryUriRepository</strong>:</p>
<pre style="width: 106.32%; height: 345px" class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> RetryUriRepository : IUriRepository
{
    <span class="kwrd">private</span> <span class="kwrd">readonly</span> IUriRepository _uriRepository;
    <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">int</span> MaxRetries = 10;
    <span class="kwrd">private</span> <span class="kwrd">const</span> <span class="kwrd">double</span> DelayMs = 2000;

    <span class="kwrd">public</span> RetryUriRepository(IUriRepository uriRepository)
    {   _uriRepository = uriRepository;    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> InsertUri(<span class="kwrd">string</span> shortUri, <span class="kwrd">string</span> longUri, <span class="kwrd">string</span> ipAddress)
    {
        var policy = GetRetryPolicy();
        policy.ExecuteAction(() =&gt; _uriRepository.InsertUri(shortUri, longUri, ipAddress));
    }
    <span class="kwrd">private</span> <span class="kwrd">static</span> RetryPolicy GetRetryPolicy()
    {
        <span class="kwrd">return</span> <span class="kwrd">new</span> RetryPolicy&lt;SqlAzureTransientErrorDetectionStrategy&gt;(MaxRetries,&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; TimeSpan.FromMilliseconds(DelayMs));
    }
}</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Using the supplied framework might be overkill, but it should give you an idea on how to implement retry logic in your service.</p>
<h1>Too Much Retry</h1>
<p>One thing that becomes interesting is when the number of retries increases. This typically indicates either a longer error condition, or you are overloading the services you are consuming. The most likely, and only one we can do anything about, is the later. The more throttling that goes on, the more retires. The more retires the less throughput. The less throughput the slower the response time. Poor response time = disgruntled users (and executives).</p>
<p>Don’t be tempted to turn off the retry logic when this happens. This will just make the problem much worse. About the only solution when dealing with overloading a service is to either scale that service out, or attempt to delay the processing using a queue/worker pattern.</p>
<h1>Conclusion</h1>
<p>Implementing retry logic is critical if you want your service to keep running. Monitoring the frequency of these retries can be a good indicator you are starting to experience scale issues. Don’t turn your retry logic off to handle scale issues.</p>
<p>THIS POSTING IS PROVIDED “AS IS” WITH NO WARRANTIES, AND CONFERS NO RIGHTS, EXCEPT ON THE THIRD SUNDAY WITH A FULL MOON</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidaiken.com/2011/10/10/implementing-windows-azure-retry-logic/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Security Best Practices for Developing Windows Azure Applications</title>
		<link>http://www.davidaiken.com/2010/06/02/security-best-practices-for-developing-windows-azure-applications/#utm_source=feed&#038;utm_medium=feed&#038;utm_campaign=feed</link>
		<comments>http://www.davidaiken.com/2010/06/02/security-best-practices-for-developing-windows-azure-applications/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 17:49:49 +0000</pubDate>
		<dc:creator>theDavidAiken</dc:creator>
				<category><![CDATA[Windows Azure]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.davidaiken.com/2010/06/02/security-best-practices-for-developing-windows-azure-applications/</guid>
		<description><![CDATA[Update (June 4th): There are a few errors in the guide which are being fixed by the authors. I&#8217;ll update this post again once they are fixed.
This is worth a read &#60;link removed&#62;
This paper focuses on the security challenges and recommended approaches to design and develop more secure applications  [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Update (June 4th): There are a few errors in the guide which are being fixed by the authors. I&#8217;ll update this post again once they are fixed.</strong></p>
<p>This is worth a read &lt;link removed&gt;</p>
<blockquote><p>This paper focuses on the security challenges and recommended approaches to design and develop more secure applications for Microsoft’s Windows Azure platform. Microsoft Security Engineering Center (MSEC) and Microsoft’s Online Services Security &amp; Compliance (OSSC) team have partnered with the Windows Azure team to build on the same security principles and processes that Microsoft has developed through years of experience managing security risks in traditional development and operating environments.</p></blockquote>
<p>THIS POSTING IS PROVIDED “AS IS” WITH NO WARRANTIES, AND CONFERS NO RIGHTS</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidaiken.com/2010/06/02/security-best-practices-for-developing-windows-azure-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

