Skip to content

Commit efc6eb8

Browse files
authored
Merge pull request iluwatar#610 from radresian/event-sourcing
Event sourcing
2 parents 9d2f0c6 + c6354c4 commit efc6eb8

File tree

16 files changed

+1441
-1
lines changed

16 files changed

+1441
-1
lines changed

event-sourcing/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
layout: pattern
3+
title: Event Sourcing
4+
folder: event-sourcing
5+
permalink: /patterns/event-sourcing/
6+
categories: Architectural
7+
tags:
8+
- Java
9+
- Difficulty Intermediate
10+
- Performance
11+
---
12+
13+
## Intent
14+
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.
15+
16+
![alt text](./etc/event-sourcing.png "Event Sourcing")
17+
18+
## Applicability
19+
Use the Event Sourcing pattern when
20+
21+
* You need very high performance on persisting your application state even your application state have a complex relational data structure
22+
* You need log of changes of your application state and ability to restore a state of any moment in time.
23+
* You need to debug production problems by replaying the past events.
24+
25+
## Real world examples
26+
27+
* [The Lmax Architecture] (https://martinfowler.com/articles/lmax.html)
28+
29+
## Credits
30+
31+
* [Martin Fowler - Event Sourcing] (https://martinfowler.com/eaaDev/EventSourcing.html)
32+
* [Event Sourcing | Microsoft Docs] (https://docs.microsoft.com/en-us/azure/architecture/patterns/event-sourcing)
33+
* [Reference 3: Introducing Event Sourcing] (https://msdn.microsoft.com/en-us/library/jj591559.aspx)

event-sourcing/etc/event-sourcing.png

58.9 KB
Loading
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.2.0" icons="true" automaticImage="PNG" always-add-relationships="true" generalizations="true"
3+
realizations="true" associations="true" dependencies="false" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="com.iluwatar.event.sourcing.event.DomainEvent" project="event-sourcing"
5+
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/event/DomainEvent.java" binary="false"
6+
corner="BOTTOM_RIGHT">
7+
<position height="-1" width="-1" x="809" y="161"/>
8+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
9+
sort-features="false" accessors="true" visibility="true">
10+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
11+
<operations public="true" package="true" protected="true" private="true" static="true"/>
12+
</display>
13+
</class>
14+
<class id="2" language="java" name="com.iluwatar.event.sourcing.event.AccountCreateEvent" project="event-sourcing"
15+
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/event/AccountCreateEvent.java" binary="false"
16+
corner="BOTTOM_RIGHT">
17+
<position height="-1" width="-1" x="145" y="455"/>
18+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
19+
sort-features="false" accessors="true" visibility="true">
20+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
21+
<operations public="true" package="true" protected="true" private="true" static="true"/>
22+
</display>
23+
</class>
24+
<class id="3" language="java" name="com.iluwatar.event.sourcing.event.MoneyDepositEvent" project="event-sourcing"
25+
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/event/MoneyDepositEvent.java" binary="false"
26+
corner="BOTTOM_RIGHT">
27+
<position height="-1" width="-1" x="480" y="451"/>
28+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
29+
sort-features="false" accessors="true" visibility="true">
30+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
31+
<operations public="true" package="true" protected="true" private="true" static="true"/>
32+
</display>
33+
</class>
34+
<class id="4" language="java" name="com.iluwatar.event.sourcing.event.MoneyTransferEvent" project="event-sourcing"
35+
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/event/MoneyTransferEvent.java" binary="false"
36+
corner="BOTTOM_RIGHT">
37+
<position height="-1" width="-1" x="809" y="472"/>
38+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
39+
sort-features="false" accessors="true" visibility="true">
40+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
41+
<operations public="true" package="true" protected="true" private="true" static="true"/>
42+
</display>
43+
</class>
44+
<class id="5" language="java" name="com.iluwatar.event.sourcing.state.AccountAggregate" project="event-sourcing"
45+
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/state/AccountAggregate.java" binary="false"
46+
corner="BOTTOM_RIGHT">
47+
<position height="-1" width="-1" x="104" y="826"/>
48+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
49+
sort-features="false" accessors="true" visibility="true">
50+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
51+
<operations public="true" package="true" protected="true" private="true" static="true"/>
52+
</display>
53+
</class>
54+
<class id="6" language="java" name="com.iluwatar.event.sourcing.processor.DomainEventProcessor"
55+
project="event-sourcing"
56+
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/processor/DomainEventProcessor.java" binary="false"
57+
corner="BOTTOM_RIGHT">
58+
<position height="-1" width="-1" x="458" y="121"/>
59+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
60+
sort-features="false" accessors="true" visibility="true">
61+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
62+
<operations public="true" package="true" protected="true" private="true" static="true"/>
63+
</display>
64+
</class>
65+
<class id="7" language="java" name="com.iluwatar.event.sourcing.processor.JsonFileJournal" project="event-sourcing"
66+
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/processor/JsonFileJournal.java" binary="false"
67+
corner="BOTTOM_RIGHT">
68+
<position height="189" width="171" x="69" y="26"/>
69+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
70+
sort-features="false" accessors="true" visibility="true">
71+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
72+
<operations public="true" package="true" protected="true" private="true" static="true"/>
73+
</display>
74+
</class>
75+
<class id="8" language="java" name="com.iluwatar.event.sourcing.domain.Account" project="event-sourcing"
76+
file="/event-sourcing/src/main/java/com/iluwatar/event/sourcing/domain/Account.java" binary="false"
77+
corner="BOTTOM_RIGHT">
78+
<position height="-1" width="-1" x="472" y="827"/>
79+
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
80+
sort-features="false" accessors="true" visibility="true">
81+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
82+
<operations public="true" package="true" protected="true" private="true" static="true"/>
83+
</display>
84+
</class>
85+
<generalization id="9">
86+
<end type="SOURCE" refId="2"/>
87+
<end type="TARGET" refId="1"/>
88+
</generalization>
89+
<generalization id="10">
90+
<end type="SOURCE" refId="4"/>
91+
<end type="TARGET" refId="1"/>
92+
</generalization>
93+
<association id="11">
94+
<end type="SOURCE" refId="6" navigable="false">
95+
<attribute id="12" name="processorJournal">
96+
<position height="0" width="0" x="0" y="0"/>
97+
</attribute>
98+
<multiplicity id="13" minimum="0" maximum="1">
99+
<position height="0" width="0" x="0" y="0"/>
100+
</multiplicity>
101+
</end>
102+
<end type="TARGET" refId="7" navigable="true"/>
103+
<display labels="true" multiplicity="true"/>
104+
</association>
105+
<generalization id="14">
106+
<end type="SOURCE" refId="3"/>
107+
<end type="TARGET" refId="1"/>
108+
</generalization>
109+
<association id="15">
110+
<end type="SOURCE" refId="5" navigable="false">
111+
<attribute id="16" name="accounts"/>
112+
<multiplicity id="17" minimum="0" maximum="2147483647"/>
113+
</end>
114+
<end type="TARGET" refId="8" navigable="true"/>
115+
<display labels="true" multiplicity="true"/>
116+
</association>
117+
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
118+
sort-features="false" accessors="true" visibility="true">
119+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
120+
<operations public="true" package="true" protected="true" private="true" static="true"/>
121+
</classifier-display>
122+
<association-display labels="true" multiplicity="true"/>
123+
</class-diagram>
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
@startuml
2+
package com.iluwatar.event.sourcing.journal {
3+
class JsonFileJournal {
4+
- aFile : File
5+
- events : List<String>
6+
- index : int
7+
+ JsonFileJournal()
8+
+ readNext() : DomainEvent
9+
+ reset()
10+
+ write(domainEvent : DomainEvent)
11+
}
12+
}
13+
package com.iluwatar.event.sourcing.processor {
14+
class DomainEventProcessor {
15+
- precessorJournal : ProcessorJournal
16+
+ DomainEventProcessor()
17+
+ process(domainEvent : DomainEvent)
18+
+ recover()
19+
+ setPrecessorJournal(precessorJournal : ProcessorJournal)
20+
}
21+
}
22+
package com.iluwatar.event.sourcing.service {
23+
class AccountService {
24+
- eventProcessor : EventProcessor
25+
+ AccountService(eventProcessor : EventProcessor)
26+
+ createAccount(accountNo : int, owner : String)
27+
}
28+
class MoneyTransactionService {
29+
- eventProcessor : EventProcessor
30+
+ MoneyTransactionService(eventProcessor : EventProcessor)
31+
+ depositMoney(accountNo : int, money : BigDecimal)
32+
+ transferMoney(accountNoFrom : int, accountNoTo : int, money : BigDecimal)
33+
+ withdrawalMoney(accountNo : int, money : BigDecimal)
34+
}
35+
class SequenceIdGenerator {
36+
- sequenceId : long {static}
37+
+ SequenceIdGenerator()
38+
+ nextSequenceId() : long {static}
39+
}
40+
}
41+
package com.iluwatar.event.sourcing.event {
42+
class AccountCreateEvent {
43+
- accountNo : int
44+
- owner : String
45+
+ AccountCreateEvent(sequenceId : long, createdTime : long, accountNo : int, owner : String)
46+
+ getAccountNo() : int
47+
+ getOwner() : String
48+
+ process()
49+
}
50+
class MoneyDepositEvent {
51+
- accountNo : int
52+
- money : BigDecimal
53+
+ MoneyDepositEvent(sequenceId : long, createdTime : long, accountNo : int, money : BigDecimal)
54+
+ getAccountNo() : int
55+
+ getMoney() : BigDecimal
56+
+ process()
57+
}
58+
class MoneyTransferEvent {
59+
- accountNoFrom : int
60+
- accountNoTo : int
61+
- money : BigDecimal
62+
+ MoneyTransferEvent(sequenceId : long, createdTime : long, money : BigDecimal, accountNoFrom : int, accountNoTo : int)
63+
+ getAccountNoFrom() : int
64+
+ getAccountNoTo() : int
65+
+ getMoney() : BigDecimal
66+
+ process()
67+
}
68+
class MoneyWithdrawalEvent {
69+
- accountNo : int
70+
- money : BigDecimal
71+
+ MoneyWithdrawalEvent(sequenceId : long, createdTime : long, accountNo : int, money : BigDecimal)
72+
+ getAccountNo() : int
73+
+ getMoney() : BigDecimal
74+
+ process()
75+
}
76+
}
77+
package com.iluwatar.event.sourcing.gateway {
78+
class AccountCreateContractSender {
79+
+ AccountCreateContractSender()
80+
+ sendContractInfo(account : Account)
81+
}
82+
class Gateways {
83+
- accountCreateContractSender : AccountCreateContractSender {static}
84+
- transactionLogger : TransactionLogger {static}
85+
+ Gateways()
86+
+ getAccountCreateContractSender() : AccountCreateContractSender {static}
87+
+ getTransactionLogger() : TransactionLogger {static}
88+
}
89+
class TransactionLogger {
90+
+ TransactionLogger()
91+
+ log(transaction : Transaction)
92+
}
93+
}
94+
package com.iluwatar.event.sourcing.app {
95+
class App {
96+
+ App()
97+
+ main(args : String[]) {static}
98+
}
99+
}
100+
package com.iluwatar.event.sourcing.state {
101+
class AccountAggregate {
102+
- accounts : Map<Integer, Account> {static}
103+
+ AccountAggregate()
104+
+ getAccount(accountNo : int) : Account {static}
105+
+ putAccount(account : Account) {static}
106+
+ resetState() {static}
107+
}
108+
}
109+
package com.iluwatar.event.sourcing.domain {
110+
class Account {
111+
- accountNo : int
112+
- money : BigDecimal
113+
- owner : String
114+
- transactions : List<Transaction>
115+
+ Account(accountNo : int, owner : String)
116+
+ copy() : Account
117+
- depositMoney(money : BigDecimal) : Transaction
118+
+ getAccountNo() : int
119+
+ getMoney() : BigDecimal
120+
+ getOwner() : String
121+
+ getTransactions() : List<Transaction>
122+
- handleDeposit(money : BigDecimal, realTime : boolean)
123+
+ handleEvent(accountCreateEvent : AccountCreateEvent)
124+
+ handleEvent(moneyDepositEvent : MoneyDepositEvent)
125+
+ handleEvent(moneyWithdrawalEvent : MoneyWithdrawalEvent)
126+
+ handleTransferFromEvent(moneyTransferEvent : MoneyTransferEvent)
127+
+ handleTransferToEvent(moneyTransferEvent : MoneyTransferEvent)
128+
- handleWithdrawal(money : BigDecimal, realTime : boolean)
129+
+ setMoney(money : BigDecimal)
130+
+ setTransactions(transactions : List<Transaction>)
131+
+ toString() : String
132+
- withdrawMoney(money : BigDecimal) : Transaction
133+
}
134+
class Transaction {
135+
- accountNo : int
136+
- lastBalance : BigDecimal
137+
- moneyIn : BigDecimal
138+
- moneyOut : BigDecimal
139+
+ Transaction(accountNo : int, moneyIn : BigDecimal, moneyOut : BigDecimal, lastBalance : BigDecimal)
140+
+ getAccountNo() : int
141+
+ getLastBalance() : BigDecimal
142+
+ getMoneyIn() : BigDecimal
143+
+ getMoneyOut() : BigDecimal
144+
+ toString() : String
145+
}
146+
}
147+
package com.iluwatar.event.sourcing.api {
148+
abstract class DomainEvent {
149+
- createdTime : long
150+
- eventClassName : String
151+
- realTime : boolean
152+
- sequenceId : long
153+
+ DomainEvent(sequenceId : long, createdTime : long, eventClassName : String)
154+
+ getCreatedTime() : long
155+
+ getEventClassName() : String
156+
+ getSequenceId() : long
157+
+ isRealTime() : boolean
158+
+ process() {abstract}
159+
+ setRealTime(realTime : boolean)
160+
}
161+
interface EventProcessor {
162+
+ process(DomainEvent) {abstract}
163+
+ recover() {abstract}
164+
+ setPrecessorJournal(ProcessorJournal) {abstract}
165+
}
166+
interface ProcessorJournal {
167+
+ readNext() : DomainEvent {abstract}
168+
+ reset() {abstract}
169+
+ write(DomainEvent) {abstract}
170+
}
171+
}
172+
Gateways --> "-accountCreateContractSender" AccountCreateContractSender
173+
DomainEventProcessor --> "-precessorJournal" ProcessorJournal
174+
Account --> "-transactions" Transaction
175+
Gateways --> "-transactionLogger" TransactionLogger
176+
AccountService --> "-eventProcessor" EventProcessor
177+
MoneyTransactionService --> "-eventProcessor" EventProcessor
178+
AccountCreateEvent --|> DomainEvent
179+
MoneyDepositEvent --|> DomainEvent
180+
MoneyTransferEvent --|> DomainEvent
181+
MoneyWithdrawalEvent --|> DomainEvent
182+
JsonFileJournal ..|> ProcessorJournal
183+
DomainEventProcessor ..|> EventProcessor
184+
@enduml

event-sourcing/pom.xml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright (c) 2014-2016 Ilkka Sepp�l�
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<modelVersion>4.0.0</modelVersion>
30+
<parent>
31+
<artifactId>java-design-patterns</artifactId>
32+
<groupId>com.iluwatar</groupId>
33+
<version>1.17.0-SNAPSHOT</version>
34+
</parent>
35+
<artifactId>event-sourcing</artifactId>
36+
<dependencies>
37+
<dependency>
38+
<groupId>junit</groupId>
39+
<artifactId>junit</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
43+
<dependency>
44+
<groupId>com.google.code.gson</groupId>
45+
<artifactId>gson</artifactId>
46+
<version>2.8.1</version>
47+
</dependency>
48+
49+
</dependencies>
50+
</project>

0 commit comments

Comments
 (0)