The Importance of Event Sourcing in Serverless Development
Event sourcing is a way of capturing and persisting the changes happening in a system as a sequence of events.
Figure 3-3 showed a customer account service that emits account created, account updated, and account deleted events. Traditionally, when you store and update data in a table, it records the latest state of each entity. Table 3-1 shows what this might look like for the customer account service. There’s one record (row) per customer, storing the latest information for that customer.
Table 3-1. Sample rows from the Customer Account table Customer ID | First name | Last name | Address | DOB | Status |
100-255-8730 | Joe | Bloke | 99, Edge Lane, London | 1966/04/12 | ACTIVE |
100-735-6729 | Biz | Raj | 12A, Top Street, Mumbai | 1995/06/15 | DELETED |
While Table 3-1 provides an up-to-date representation of each customer’s data, it does not reveal whether customers’ addresses have changed at any point. Event sourcing helps provide a different perspective on the data by capturing and persisting the domain events as they occur. If you look at the data in Table 3-2, you’ll see that it preserves the domain events related to a customer account. This data store acts as the source for the events if you ever want to reconstruct the activities of an account.
Table 3-2. Event source data store for the customer account service PK | SK | Event ID | First name | Last name | Address | DOB | Status |
100-255-8730 | 2023-04-05T08:47: 30.718Z | Hru343t5-jvcj | Joe | Bloke | 99, Edge Lane, London | 1966/04/12 | UPDATED |
100-735-6729 | 2023-01-15T02:37: 20.545Z | lgojk834sd3-r454 | Biz | Raj | 12A, Top Street, Mumbai | 1995/06/15 | DELETED |
100-255-8730 | 2022-10-04T09:27: 20.443Z | Jsd93ebhas-xdfgns | Joe | Bloke | 34, Fine Way, Leeds | 1966/04/12 | UPDATED |
100-255-8730 | 2022-06-15T18:57: 43.148Z | Zxjfie294hfd-kd9e7n | Joe | Bloke | 15, Nice Road, Cardiff | 1966/04/12 | CREATED |
100-735-6729 | 2009-11-29T20:49: 40.003Z | skdj834sd3-j3ns | Biz | Raj | 12A, Top Street, Mumbai | 1995/06/15 | CREATED |