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
