software, japanese, kyudo and more

Event sourcing: introduction by Greg Young

Some days ago I watched a video about event sourcing and cqrs where Greg Young was introducing the concepts in a very nice way.

I remember once in a project where we had to record every action that every user was performing in the system, so we had the usual database with the current state of the system that included a historical table with all the actions performed.

Event sourcing is about assuming that all the information that a system holds is based on the events that produced changes in that system. So they worth being stored. Instead of focusing on the current state of the data, it focus in the process that lead to the current state. For regulated enviroments it is a must to have all this events stored so it can naturally fall into the event sourcing pattern, but many other business can take a lot of benefit on using event sourcing.

This way of working is not new. Banks, lawyers, human resources, etc. have been working in this way since long time ago (before information was managed by something called software). The current state of your account is just the result of executing all the events/actions that affected it. No one would like to know their savings by having a table with the current state and not all the movements on the account. 

It has a lot of advantatges, like being able to go back in time and see how the system was in a given point of time, or calculate the evolution of a system, and also to be able to analyze the data in a deeper way than just having the current state. So for business intelligence reports and dashboards it is very useful.

It has also some ugly disadvantatges. Since you focus on storing the changes that are made to your system, you need to execute all the events in order to calculate the current state of the system. It is not very performant if you need to show this information to your users in a reasonable time. To minimize this you can create snapshots, so you only need to execute the events from the latest snapshot to the end, but still it would be too expensive to query data to show it on the screens. That is why event sourcing cannot work without CQRS.

I will talk about CQRS in a new post, but there is also a small introduction to it in the attached video.If you want to know more about event sourcing and to learn from someone more acknowledgeable, here you have the video:

Greg Young - CQRS and Event Sourcing - Code on the Beach 2014

Are you using event sourcing in your companies? Did you find any problems difficult to solve?

Self-Hosted OWIN - Hello World!

We are going to create a new self-hosted website using owin.

0) Requirements;

  • Visual Studio 201x (I am using the VS 2015) in "Run as Administrator" mode (required to open ports).
  • Internet connection (assumed that you have it since you are reading this post).

1) Create a new Console application:

2) Install the Owin packages:

Open the Package Manager Console by clicking the Tools menu and going to Tools/Nuget Package Manager/Package Manager Console

Execute the following command:
Install-Package Microsoft.Owin.SelfHost

It will install the Owin libraries to reference them in your project.

3) Configure the application to host the web:

Add the following content to the Program.cs class:
On top  of usings:
using Microsoft.Owin;
using Microsoft.Owin.Hosting;

Above the namespace declaration:
[assembly: OwinStartup(typeof(Owin.HelloWorld.Startup))]

Add the Startup class :
    public class Startup
        public void Configuration(IAppBuilder app)
            app.Run(context =>
                context.Response.ContentType = "text/plain";
                return context.Response.WriteAsync("Hello, world.");

Add the content to the body of the Main method:
        static void Main(string[] args)
            using (WebApp.Start("http://localhost:9000"))
                Console.WriteLine("Press [enter] to quit...");

4) Check it!

Press F5 to execute the Console Application in Debug mode.

5) Further information: