Event Sourcing

WIP

“Instead of storing just the current state of the data in a domain, use an append-only store to record the full series of actions taken on that data. The store acts as the system of record and can be used to materialize the domain objects.”

Database

Most of us are familiar with a relational database, these can be used to store the sequence of events. The following example was adapted from https://eventflow.readthedocs.io/

1
2
3
4
5
6
7
8
9
10
/* MS SQL */
CREATE TABLE [dbo].[event_source](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[entity_id] [int] NOT NULL,
[data] [nvarchar](max) NOT NULL,
[meta_data] [nvarchar](max) NOT NULL
CONSTRAINT [pk_event_source] PRIMARY KEY CLUSTERED
(
[id] ASC
)
  • id; global id you can use to order the events
  • entity_id; links to a table of entities
  • data; serialized entity (JSON)
  • meta_data; tracking information such as time stamp, action performed by anything useful to track the event

References