How The Mill Adventure Implemented Event Sourcing at Scale Using DynamoDB
AWS Architecture BlogHow The Mill Adventure Implemented Event Sourcing at Scale Using DynamoDBby Uri Segev and João Dias | on13 SEP 2021| inAmazon DynamoDB,Architecture,AWS Lambda,Customer Solutions,Game Development,Serverless|Permalink|ShareThis post was co-written by Joao Dias, Chief Architect at The Mill Adventure and Uri Segev, Principal Serverless Solutions Architect at AWSThe Mill Adventureprovides a complete gaming platform, including licenses and operations, for rapid deployment and success in online gaming. It underpins every aspect of the process so that you can focus on telling your story to your audience while the team makes everything else work perfectly.In this blog post, we demonstrate how The Mill Adventure implemented event sourcing at scale usingAmazon DynamoDBandServerless on AWStechnologies. By partnering with AWS, The Mill Adventure reduced their costs, and they are able to maintain operations and scale their solution to suit their needs without their intervention.What is event sourcing?Event sourcing captures an entity’s state (such as a transaction or a user) as a sequence of state-changing events. Whenever the state changes, a new event is appended to the sequence of events using an atomic operation.The system persists these events in an event store, which is a database of events. The store supports adding and retrieving the state events. The system reconstructs the entity’s state by reading the events from the event store and replaying them. Because the store is immutable (meaning these events are saved in the event store forever) the entity’s state can be recreated up to a particular version or date and have accurate historical values.Why use event sourcing?Event sourcing provides many advantages, that include (but are not limited to) the following:Audit trail:Events are immutable and provide a history of what has taken place in the system. This means it’s not only providing the current state, but how it got there.Time travel:By persisting a sequence of events, it is relatively easy to determine the state of the system at any point in time by aggregating the events within that time period. This provides you the ability to answer historical questions about the state of the system.Performance:Events are simple and immutable and only require an append operation. The event store should be optimized to handle high-performance writes.Scalability:Storing events avoids the complications associated with saving complex domain aggregates to relational databases, which allows more flexibility for scaling.Event-driven architecturesEvent sourcing is also related to event-driven architectures. Every event that changes an entity’s state can also be used to notify other components about the change. In event-driven architectures, we use event routers to distribute the events to interested components.The event router has three main functions:Decouple the event producers from the event consumers:The producers don’t know who the consumers are, and they do not need to change when new consumers are added or removed.Fan out:Event routers are capable of distributing events to multiple subscribers.Filtering:Event routers send each subscriber only the events they are interested in. This saves on the number of events that consumers need to process; therefore, it reduces the cost of the consumers.How did The Mill Adventure implement event sourcing?The Mill Adventure uses DynamoDB tables as their object store. Each event is a new item in the table. The DynamoDB table model for an event sourced system is quite simple, as follows:FieldTypeDescriptionidPKThe object identifierversionSKThe event sequence numbereventdataThe event data itself, in other words, the change to the object’s stateAll events for the same object have the same id. Thus, you can retrieve them using a single read request.When a component modifies the state of an object, it first determines the sequence number for the new event by reading the current state from the table (in other words, the sequence of events for that object). It then attempts to write a new item to the table that represents the change to the object’s state. The item is written using DynamoDB’s conditional write. This ensures that there are no other changes to the same object happening at the same time. If the write failed due to a condition not met error, it will start over.An additional benefit of using DynamoDB as the event store isDynamoDB Streams, which is used to deliver events about changes in tables. These events can be used by event-driven applications so they will know about the different objects’ change of state.How does it work?Let’s use an example of a business entity, such as a user. When a user is created, the system creates aUserCreatedevent with the initial user data (like user name, address, etc.). The system then persists this event to the DynamoDB event store using a conditional write. This makes sure that the event is only written once and that the version numbers are sequential.Then the user address gets updated, so again, the system creates aUserUpdatedevent with the new address and persists it.When the system needs the user’s current state, for example, to show it in back-office application, the system loads all the events for the given user identifier from the store. For each one of them, it invokes a mutation function that recreates the latest state. Given the following items in the database:Event 1:UserCreated(name: The Mill, address: Malta)Event 2:UserUpdated(address: World)You can imagine how each mutator function for those events would look like, which then produce the latest state:{ 
"name": "The Mill", 
"address": "World" 
}A business state like a bank statement can have a large number of events. To optimize loading, the system periodically saves a snapshot of the current state. To reconstruct the current state, the application finds the most recent snapshot and the events that have occurred since that snapshot. As a result, there are fewer events to replay.ArchitectureThe Mill Adventure architecture for an event source system using AWS components is straightforward. The architecture is fully serverless, as such, it only usesAWS Lambdafunctions for compute. Lambda functions produce the state-changing events that are written to the database.Other Lambda functions, when they retrieve an object’s state, will read the events from the database and calculate the current state by replaying the events.Finally, interested functions will be notified about the changes by subscribing to the event bus. Then they perform their business logic, like updating state projections or publishing to WebSocket APIs. These functions use DynamoDB streams as the event bus to handle messages as shows in Figure 1.Figure 1. Event sourcing architectureFigure 1 is not completely accurate due to a limitation of DynamoDB Streams, which can only support up to two subscribers.Because The Mill Adventure has many microservices that are interested in these events, they have a single function that gets invoked from the stream and sends the events to other event routers. These fan out to a large number of subscribers such as Amazon EventBridge, Amazon Simple Notification Service (Amazon SNS), or maybe even Amazon Kinesis Data Streams for some use cases.Any service in the system could be listening to these events being created via the DynamoDB stream and distributed via the event router and act on them. For example, publishing a WebSocket API notification or prompting a contact update in a third-party service.ConclusionIn this blog post, we showed how The Mill Adventure uses serverless technologies like DynamoDB and Lambda functions to implement an event-driven event sourcing system.An event sourced system can be difficult to scale, but using DynamoDB as the event store resolved this issue. It can also be difficult to produce consistent snapshots and Command Query Responsibility Segregation (CQRS) views, but using DynamoDB streams for distributing the events made it relatively easy.By partnering with AWS, The Mill Adventure created a sports/casino platform to be proud of. It provides high quality data and performance without having servers, they only pay for what they use, and their workload can scale up and down as needed.TAGS:Customer SolutionsUri SegevUri Segev is a Serverless Solutions Architect with Amazon Web Services based in Israel. Uri has many years of experience as a developer and software architect. He has spoken on serverless topics at several events. As part of his role, Uri focuses on Serverless services as well as topics like Event Driven Architectures and Modern Application development.João DiasJoão Dias is the Chief Architect at The Mill Adventure, and passionate about simplifying the complicated. He has been writing software since before it was cool and has been building innovative iGaming solutions for the past few years.
This post was co-written by Joao Dias, Chief Architect at The Mill Adventure and Uri Segev, Principal Serverless Solutions Architect at AWS The Mill Adventure provides a complete gaming platform, including licenses and operations, for rapid deployment and success in online gaming. It underpins every aspect of the process so that you can focus on […]
Uri Segev
Amazon DynamoDB Architecture AWS Lambda Customer Solutions Game Development Serverless