Automatic Retry and Circuit Breaker made easy

Polly library logoIf you do not know Polly, you are missing out! I did not know about it until a couple of days ago and you properly never heard about it either, as this wonderful little library only has 63 downloads on NuGet at the time of writing.

Polly is an easy to use retry and circuit breaker pattern implementation for .Net – let me show you.
Start by specifying the policy – what should happen when an exception thrown:

  var policy = Policy
    .Handle<SqlException(e => e.Number == 1205) // Handling deadlock victim
    .Or<OtherException>()
    .Retry(3, (exception, retyCount, context) =>
    {
      // Log...
    });

The above policy specifies a SqlExeption with number 1205 or OtherException should be retried three times – if it still fails log and bobble the original exception up the call stack.

  var result = policy.Execute(() => FetchData(p1, p2));

It is also possible to specify the time between retries – e.g. exponential back off:

  var policy = Policy
    .Handle<MyException>()
    .WaitAndRetry(5, retryAttempt =>
      TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)
    );

Or the circuit breaker safeguarding against the same error occurs again and again if an external system is temporarily unavailable:

  var policy = Policy
    .Handle<TimeoutException>()
    .CircuitBreaker(2, TimeSpan.FromMinutes(1));

Go get it – I’m already using it :)

Accessing HTTP Request from ASP.NET Web API

Do you need access to the bare HTTP request in ASP.NET Web API to access custom header etc.? Then add the HttpRequestMessage:

public class CalculatorController : ApiController
{
  public int Add(HttpRequestMessage requestMessage, int x, int y)
  {
    var accessToken = requestMessage.Headers.Authorization.Parameter;
    // use the HTTP header

    return x + y;
  }
}

The HttpRequestMessage is automatically bound to the controller action, so you can still execute the action like http://localhost/calculator/add?x=3&y=2

Simple and easy.

GOTO Aarhus 2012 – Tuesday

The morning keynote by Scott Hanselman was about the true power of JavaScript. He argued that JavaScript in the browser is a full operating system running as a virtual machine within the browser – so we should treat it so. Don’t use Java Applets, Flash, Flex or Silverlight as it just another (slow) abstraction upon an already powerfull engine – the browser. It was a great talk leading up to the pre-release of TypeScript.

I followed a couple of sessions the continuous delivery by Sam Newman, Michael T. Nygard (author of Release It) and Jez Humble (author of Continuous Delivery).
Continuous Integration is a prerequisite of Continuous Delivery, but many still don’t use apply Continuous Integration to their solution, with daily incremental check-ins, automated build and unit tests.

To simplify Continuous Delivery, everything must be automated. To ease the task of automation, things must be simplified. To simplify, start by decomposing the system into manageable pieces, so each can be deployed separately. How?
Decompose the system into disconnected services makes it easier to deploy a subset of the system. This limits the impact of a deployment. It even makes it possible to mitigate the risk further by making small incremental changes by only deploying one subsystem at the time.

These services have to be structured as application silos and share nothing, not even the database schema.

By automating and decomposing your system into disconnected application silo services you too can do Continuous Delivery.
After the conference the GOTO Aarhus guys had joint up with the local community and user groups to hos open sessions. I attended the ANUG (Aarhus .NET User Group) session with Anders Hejlsberg. He presented the brand new TypeScript – a superset of JavaScript that compiles into plain JavaScript and runs in any browser (similar concept as CoffeeScript). It has great tooling support in Visual Studio with intelliSense and static verification.

I’m looking forward to the last day of the conference tomorrow.

GOTO Aarhus 2012 – Monday

The day started with a keynote from @Falkvinge from the Pirate Party. I wasn’t expecting much from this keynote, but I was pleasantly surprised. First of all, I assumed that I knew quite a bit about the Pirate Party – I was wrong! Facts: the Pirate Party is present in 150 countries and has 2 European Union parliament members. These guys are serious and not just a protest party wanting to legalize sharing copyrighted material. They are fighting the problems with limiting access to knowledge and ideas. They are emphasizing that exclusive right like patents, copyright and subsidizing are counterproductive. That’s so true! @Falkvinge disrupted my brain – that’s great, because that is why I’m here!

Next up was great presentation of graph databases by Jim Webber – fast speaking provocative British architect from Neo4J. He (re)spiked my interest in ‘other’ databases and stressed that each type of database like relational, object, key-value stores, document,  graph etc. databases each fit their problem domain. So you shouldn’t just pick RavenDB because it is the new hot think in .Net sphere (or because Ayende aka Oren Eini says so). I will definitely take a look Net4J with the .Net client library Neo4jClient . Another great point from Jim Webber was; ACID does scale (though many claims otherwise), but he stressed it was distributed ACID with 2PC that doesn’t scale.

From then on I attended a couple of unfortunate sessions (not worth mentioning). Now it is time for the conference party where the beer is sponsored by Atlassian.

GOTO Aarhus 2012 Schedule

Soon I’ll be joining a bunch of great people from the Danish developer community and abroad at this year GOTO Conference in Aarhus next week.

I’ve been looking at the conference schedule trying to create my schedule… the line-up of international fame speakers are impressive, but I’ll go for the odd sessions to expand my horizon. During breaks I’ll discuss and share ideas with my fellow attendees – I might even skip sessions for interesting discussions in the hallways.

Here is my tentative schedule: