Event Sourcing on Azure
Tags: azure, architecture, dotnet
Published: 2020-11-24 15:07:18

Hi All! With this post, we'll start a new Series about Event Sourcing on Azure. We're going to talk a bit about the pattern, general architecture, and the individual building blocks. Then in the next posts, we'll dig more and see each one in detail.

If you're a regular reader of this blog, you might know that I wrote already about Event Sourcing [in the past](https://www.davideguida.com/event-sourcing-in-net-core-part-1-a-gentle-introduction/?swcfpc=1). It's a complex pattern, probably one of the most complex to get right. I enjoy the challenges it gives and how it causes a whole plethora of other patterns to be evaluated in conjunction ([CQRS](https://www.davideguida.com/lets-do-some-ddd-with-entity-framework-core-3-part-3-better-value-objects/?swcfpc=1) anyone?).

#### And like any other pattern, there are no *silver bullets*. Architecture and implementation will change based on the Domain needs.

But we can "quickly" lay out the general idea, and then diverge from it based on our necessities (or should I say the **business** necessities).

So let's start with the architecture!

[![](https://i1.wp.com/www.davideguida.com/wp-content/uploads/2020/08/image-3.png?resize=788%2C392&ssl=1)](https://i1.wp.com/www.davideguida.com/wp-content/uploads/2020/08/image-3.png?ssl=1&swcfpc=1)

On the left we have the Commands (or *Write Side*), let's begin with that. The Commands our system exposes will be accessed via REST endpoints through a Web API. We could use Azure Functions with an HTTP trigger as well, but we'll talk more about this in another post of the Series.

Whatever way we pick to communicate with the outer world, the commands will first go through a validation phase against the business rules. This usually happens by re-hydrating the [*Aggregate* *Root*](https://docs.microsoft.com/en-us/archive/msdn-magazine/2011/november/the-cutting-edge-design-of-a-domain-model?WT.mc_id=DOP-MVP-5003878#customer-as-an-aggregate-root-class) from past events and performing a set of operations on it.

#### Then, if everything is fine, the commands be translated into *Domain Events* and persisted in our Event Store. We will be using CosmosDB for this.

Then we have to publish *Integration Events* to inform other parts of the system that "something" happened. This will be handled by [Azure Service Bus Topics](https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-messaging-overview?WT.mc_id=DOP-MVP-5003878#topics). We'll use Topics instead of simple Queues because we might have different consumer types interested in a particular event type. And of course, we want to deploy, operate, and scale those consumers independently.

One of these consumers will be an Azure Functions App with a very important role: [materializing our Query Models](https://docs.microsoft.com/en-us/azure/architecture/patterns/materialized-view?WT.mc_id=DOP-MVP-5003878). When querying data, we can't, of course, rehydrate the Aggregates each time. Yes, we would get consistent data each time, but it would be overkill, even if we were using *snapshots*.

So we subscribe to the Topics and each time we receive an event, we refresh a query-specific version of the data and we store it in another storage. We will still be using CosmosDB in our example.

Materialized views have the great benefit of being exactly what our calling application needs, including all the possible aggregated data. Moreover, in case our requirements change, we can always add new views or update the existing ones. As long as we have the original events stream, we can flush all the views and rebuild them from scratch with little cost.

That's all for today. [Next time](https://www.davideguida.com/event-sourcing-on-azure-part-2-events-persistence/?swcfpc=1) we'll see how we can handle events persistence. Ciao!