Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions event-sourcing/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
layout: pattern
title: Event Sourcing
folder: event-sourcing
permalink: /patterns/event-sourcing/
categories: Architectural
tags:
- Java
- Difficulty Intermediate
- Performance
---

## Intent
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. This can simplify tasks in complex domains, by avoiding the need to synchronize the data model and the business domain, while improving performance, scalability, and responsiveness. It can also provide consistency for transactional data, and maintain full audit trails and history that can enable compensating actions.

![alt text](./etc/event-sourcing.png "Event Sourcing")

## Applicability
Use the Event Sourcing pattern when

* You need very high performance on persisting your application state even your application state have a complex relational data structure
* You need log of changes of your application state and ability to restore a state of any moment in time.
* You need to debug production problems by replaying the past events.

## Real world examples

* [The Lmax Architecture] (https://martinfowler.com/articles/lmax.html)

## Credits

* [Martin Fowler - Event Sourcing] (https://martinfowler.com/eaaDev/EventSourcing.html)
* [Event Sourcing | Microsoft Docs] (https://docs.microsoft.com/en-us/azure/architecture/patterns/event-sourcing)
* [Reference 3: Introducing Event Sourcing] (https://msdn.microsoft.com/en-us/library/jj591559.aspx)
Binary file added event-sourcing/etc/event-sourcing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
123 changes: 123 additions & 0 deletions event-sourcing/etc/event-sourcing.ucls
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.2.0" icons="true" automaticImage="PNG" always-add-relationships="true" generalizations="true"
realizations="true" associations="true" dependencies="false" nesting-relationships="true" router="FAN">
<class id="1" language="java" name="com.iluwatar.event.sourcing.event.DomainEvent" project="event-sourcing"
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/event/DomainEvent.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="809" y="161"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="2" language="java" name="com.iluwatar.event.sourcing.event.AccountCreateEvent" project="event-sourcing"
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/event/AccountCreateEvent.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="145" y="455"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="3" language="java" name="com.iluwatar.event.sourcing.event.MoneyDepositEvent" project="event-sourcing"
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/event/MoneyDepositEvent.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="480" y="451"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="4" language="java" name="com.iluwatar.event.sourcing.event.MoneyTransferEvent" project="event-sourcing"
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/event/MoneyTransferEvent.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="809" y="472"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="5" language="java" name="com.iluwatar.event.sourcing.state.AccountAggregate" project="event-sourcing"
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/state/AccountAggregate.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="104" y="826"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="6" language="java" name="com.iluwatar.event.sourcing.processor.DomainEventProcessor"
project="event-sourcing"
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/processor/DomainEventProcessor.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="458" y="121"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="7" language="java" name="com.iluwatar.event.sourcing.processor.JsonFileJournal" project="event-sourcing"
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/processor/JsonFileJournal.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="189" width="171" x="69" y="26"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="8" language="java" name="com.iluwatar.event.sourcing.domain.Account" project="event-sourcing"
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/domain/Account.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="472" y="827"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<generalization id="9">
<end type="SOURCE" refId="2"/>
<end type="TARGET" refId="1"/>
</generalization>
<generalization id="10">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="1"/>
</generalization>
<association id="11">
<end type="SOURCE" refId="6" navigable="false">
<attribute id="12" name="processorJournal">
<position height="0" width="0" x="0" y="0"/>
</attribute>
<multiplicity id="13" minimum="0" maximum="1">
<position height="0" width="0" x="0" y="0"/>
</multiplicity>
</end>
<end type="TARGET" refId="7" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<generalization id="14">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="1"/>
</generalization>
<association id="15">
<end type="SOURCE" refId="5" navigable="false">
<attribute id="16" name="accounts"/>
<multiplicity id="17" minimum="0" maximum="2147483647"/>
</end>
<end type="TARGET" refId="8" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>
184 changes: 184 additions & 0 deletions event-sourcing/etc/event-sourcing.urm.puml
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
@startuml
package com.iluwatar.event.sourcing.journal {
class JsonFileJournal {
- aFile : File
- events : List<String>
- index : int
+ JsonFileJournal()
+ readNext() : DomainEvent
+ reset()
+ write(domainEvent : DomainEvent)
}
}
package com.iluwatar.event.sourcing.processor {
class DomainEventProcessor {
- precessorJournal : ProcessorJournal
+ DomainEventProcessor()
+ process(domainEvent : DomainEvent)
+ recover()
+ setPrecessorJournal(precessorJournal : ProcessorJournal)
}
}
package com.iluwatar.event.sourcing.service {
class AccountService {
- eventProcessor : EventProcessor
+ AccountService(eventProcessor : EventProcessor)
+ createAccount(accountNo : int, owner : String)
}
class MoneyTransactionService {
- eventProcessor : EventProcessor
+ MoneyTransactionService(eventProcessor : EventProcessor)
+ depositMoney(accountNo : int, money : BigDecimal)
+ transferMoney(accountNoFrom : int, accountNoTo : int, money : BigDecimal)
+ withdrawalMoney(accountNo : int, money : BigDecimal)
}
class SequenceIdGenerator {
- sequenceId : long {static}
+ SequenceIdGenerator()
+ nextSequenceId() : long {static}
}
}
package com.iluwatar.event.sourcing.event {
class AccountCreateEvent {
- accountNo : int
- owner : String
+ AccountCreateEvent(sequenceId : long, createdTime : long, accountNo : int, owner : String)
+ getAccountNo() : int
+ getOwner() : String
+ process()
}
class MoneyDepositEvent {
- accountNo : int
- money : BigDecimal
+ MoneyDepositEvent(sequenceId : long, createdTime : long, accountNo : int, money : BigDecimal)
+ getAccountNo() : int
+ getMoney() : BigDecimal
+ process()
}
class MoneyTransferEvent {
- accountNoFrom : int
- accountNoTo : int
- money : BigDecimal
+ MoneyTransferEvent(sequenceId : long, createdTime : long, money : BigDecimal, accountNoFrom : int, accountNoTo : int)
+ getAccountNoFrom() : int
+ getAccountNoTo() : int
+ getMoney() : BigDecimal
+ process()
}
class MoneyWithdrawalEvent {
- accountNo : int
- money : BigDecimal
+ MoneyWithdrawalEvent(sequenceId : long, createdTime : long, accountNo : int, money : BigDecimal)
+ getAccountNo() : int
+ getMoney() : BigDecimal
+ process()
}
}
package com.iluwatar.event.sourcing.gateway {
class AccountCreateContractSender {
+ AccountCreateContractSender()
+ sendContractInfo(account : Account)
}
class Gateways {
- accountCreateContractSender : AccountCreateContractSender {static}
- transactionLogger : TransactionLogger {static}
+ Gateways()
+ getAccountCreateContractSender() : AccountCreateContractSender {static}
+ getTransactionLogger() : TransactionLogger {static}
}
class TransactionLogger {
+ TransactionLogger()
+ log(transaction : Transaction)
}
}
package com.iluwatar.event.sourcing.app {
class App {
+ App()
+ main(args : String[]) {static}
}
}
package com.iluwatar.event.sourcing.state {
class AccountAggregate {
- accounts : Map<Integer, Account> {static}
+ AccountAggregate()
+ getAccount(accountNo : int) : Account {static}
+ putAccount(account : Account) {static}
+ resetState() {static}
}
}
package com.iluwatar.event.sourcing.domain {
class Account {
- accountNo : int
- money : BigDecimal
- owner : String
- transactions : List<Transaction>
+ Account(accountNo : int, owner : String)
+ copy() : Account
- depositMoney(money : BigDecimal) : Transaction
+ getAccountNo() : int
+ getMoney() : BigDecimal
+ getOwner() : String
+ getTransactions() : List<Transaction>
- handleDeposit(money : BigDecimal, realTime : boolean)
+ handleEvent(accountCreateEvent : AccountCreateEvent)
+ handleEvent(moneyDepositEvent : MoneyDepositEvent)
+ handleEvent(moneyWithdrawalEvent : MoneyWithdrawalEvent)
+ handleTransferFromEvent(moneyTransferEvent : MoneyTransferEvent)
+ handleTransferToEvent(moneyTransferEvent : MoneyTransferEvent)
- handleWithdrawal(money : BigDecimal, realTime : boolean)
+ setMoney(money : BigDecimal)
+ setTransactions(transactions : List<Transaction>)
+ toString() : String
- withdrawMoney(money : BigDecimal) : Transaction
}
class Transaction {
- accountNo : int
- lastBalance : BigDecimal
- moneyIn : BigDecimal
- moneyOut : BigDecimal
+ Transaction(accountNo : int, moneyIn : BigDecimal, moneyOut : BigDecimal, lastBalance : BigDecimal)
+ getAccountNo() : int
+ getLastBalance() : BigDecimal
+ getMoneyIn() : BigDecimal
+ getMoneyOut() : BigDecimal
+ toString() : String
}
}
package com.iluwatar.event.sourcing.api {
abstract class DomainEvent {
- createdTime : long
- eventClassName : String
- realTime : boolean
- sequenceId : long
+ DomainEvent(sequenceId : long, createdTime : long, eventClassName : String)
+ getCreatedTime() : long
+ getEventClassName() : String
+ getSequenceId() : long
+ isRealTime() : boolean
+ process() {abstract}
+ setRealTime(realTime : boolean)
}
interface EventProcessor {
+ process(DomainEvent) {abstract}
+ recover() {abstract}
+ setPrecessorJournal(ProcessorJournal) {abstract}
}
interface ProcessorJournal {
+ readNext() : DomainEvent {abstract}
+ reset() {abstract}
+ write(DomainEvent) {abstract}
}
}
Gateways --> "-accountCreateContractSender" AccountCreateContractSender
DomainEventProcessor --> "-precessorJournal" ProcessorJournal
Account --> "-transactions" Transaction
Gateways --> "-transactionLogger" TransactionLogger
AccountService --> "-eventProcessor" EventProcessor
MoneyTransactionService --> "-eventProcessor" EventProcessor
AccountCreateEvent --|> DomainEvent
MoneyDepositEvent --|> DomainEvent
MoneyTransferEvent --|> DomainEvent
MoneyWithdrawalEvent --|> DomainEvent
JsonFileJournal ..|> ProcessorJournal
DomainEventProcessor ..|> EventProcessor
@enduml
50 changes: 50 additions & 0 deletions event-sourcing/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

The MIT License
Copyright (c) 2014-2016 Ilkka Sepp�l�

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>java-design-patterns</artifactId>
<groupId>com.iluwatar</groupId>
<version>1.17.0-SNAPSHOT</version>
</parent>
<artifactId>event-sourcing</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>

</dependencies>
</project>
Loading