Event Sourcing - Modern Application Development on AWS

Event Sourcing

With the event sourcing pattern, instead of updating data stores directly, any events with significance to business logic–such as orders being placed, credit inquiries being made, or orders being processed or shipped–are added to a durable event log. Because each event record is stored individually, all updates are atomic (indivisible and irreducible).

A key characteristic of this pattern is that the application state at any point in time can be rebuilt by simply reprocessing the stored events. Because data is stored as a series of events rather than through direct updates to data stores, various services can replay events from the event store to compute the appropriate state of their respective data stores. This works well with the CQRS pattern discussed previously, especially because you can reproduce data for an event regardless of whether the command and query data stores have different schemas.

Figure 12 – Example of the event sourcing pattern

Because the event sourcing pattern involves storing and later replaying event messages, it requires some mechanism for storing and retrieving messages. If you plan to use this pattern in the AWS Cloud, depending on your use case, you can use Amazon Kinesis, Amazon Simple Queue Service (SQS), Amazon MQ, or Amazon MSK (Amazon MSK). In the event sourcing pattern, each event that changes the system is stored first to a message queue, and then updates to the application state are made based on that event. For example, an event can be written as a record in an Amazon Kinesis stream, and then a service built on AWS Lambda can retrieve the record and perform updates in its own data store).

Figure 13 – Example of an event sourcing pattern using Amazon Kinesis and AWS Lambda

Sometimes it is useful to expand from one source of events to multiple targets. You can do this directly with Amazon Kinesis Data Streams, which allows multiple consumers to retrieve data from a stream. You can also use Amazon Simple Notification Service (Amazon SNS) to expand to multiple Lambda functions, which all listen to the same topic, and can propagate event data from Kinesis to other stateful components. With this configuration, you can also add an Amazon Simple Queue Service (Amazon SQS) queue between Amazon SNS and a given Lambda function that enables you to specify what causes the Lambda functions to execute.