Using Azure Cosmos DB as your append-only datastore
One of the most compelling reasons for adopting Azure Cosmos DB as your datastore is probably a feature called “change feeds”.
This is even more true if your backend uses Event Sourcing.
Event Sourcing
Event Sourcing is a design pattern that proposes an alternative solution to CRUD.
When we use CRUD, we store and manipulate the state of an application using actions like Create, Read, Update or Delete. For example, if we had an entity called “Customer” we would store its name, surname, address and so on.
When we want the information to change, we use a combination of CREATE, UPDATE, READ and DELETE operations in order to ensure the entity is in the desired state.
Moving to Event Sourcing involves shifting from storing the state of an entity to storing the actions that are performed on that entity. For this reason, we would need an append-only datastore where actions might be inserted like: CHANGE CUSTOMER NAME, CHANGE CUSTOMER ADDRESS.
The entity itself can be reconstructed on reading. This can generate an overhead that is however manageable.
The design pattern offers different advantages, and if you are interested in going into more detail I would highly recommend reading the article at this link:
Configuring the Code
To operate correctly the change feed requires a container to be created called the Lease Container. Lease Containers allow different client instances to keep track of where they are when reading from a monitored collection.
The lease container acts as a state storage and coordinates processing the change feed across multiple workers. The lease container can be stored in the same account as the monitored container or in a separate account.
Another important concept is that of the host.
A host is an application instance that uses the change feed processor to listen for changes. Multiple instances with the same lease configuration can run in parallel, but each instance should have a different instance name.
That said, I tried to configure it and this is the code that worked for me:
Conclusions
Change feeds are a great candidate for implementing event sourcing. Their setup is very easy using the Azure Cosmos DB SDK.