Yet another presentation about Event Sourcing? Yes and no. Event Sourcing is a really great concept. Some could say it’s a Holy Grail of the software architecture. I might agree with that, while remembering that everything comes with a price. This session is a summary of my experience with ES gathered while working on 3 different commercial products. Instead of theoretical aspects, I will focus on possible challenges with ES implementation. What could explode (very often with delayed ignition)? How and where to store events effectively? What are possible schema evolution solutions? How to achieve the highest level of scalability and live with eventual consistency? And many other interesting topics that you might face when experimenting with ES.
8. What is Event Sourcing?
DB
Order {
items=[itemA, itemB]
}
9. What is Event Sourcing?
DB
DB
Order {
items=[itemA, itemB]
}
ItemAdded(itemA)
ItemAdded(itemC)
ItemRemoved(itemC)
ItemAdded(itemB)
10. What is Event Sourcing?
DB
DB
Order {
items=[itemA, itemB]
}
ItemAdded(itemA)
ItemAdded(itemC)
ItemRemoved(itemC)
ItemAdded(itemB)
11. History
● 9000 BC, Mesopotamian Clay Tablets,
e.g. for market transactions
12. History
● 2005, Event Sourcing
“Enterprise applications that use Event Sourcing
are rarer, but I have seen a few applications (or
parts of applications) that use it.”
13. Why Event Sourcing?
● complete log of every state change
● debugging
● performance
● scalability
● microservices integration pattern
14. ES and CQRS
Client
Query Service
Data access
Queries
Read
model
Read
model
Read
models
Command Service
Domain
Events
Commands
15. ES and CQRS level 1
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Transaction
Command Service
Domain
Events
Commands
16. ES and CQRS level 1
● Entry-level, synchronous & transactional event sourcing
● Implementing event sourcing using a relational database
● slick-eventsourcing
17. ES and CQRS level 1
+ easy to implement
+ easy to reason about
+ 0 eventual consistency
- performance
- scalability
18. ES and CQRS level 2
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
Transaction
Command Service
Domain
Events
Commands
19. ES and CQRS level 2
+/- performance
+/- scalability
- eventual consistency
- increased events DB load ?
- lags
20. ES and CQRS level 3
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
Transaction
event
bus
Command Service
Domain
Events
Commands
21. ES and CQRS level 3.1
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
event
bus
Transaction
Command Service
Domain
Events
Commands
22. ES and CQRS level 3.1.1
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
event
bus
Transaction
Command Service
Domain
Events
Commands
23. ES and CQRS level 3.1.1
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
event
bus
Transaction
At-least-once delivery
Command Service
Domain
Events
Commands
24. ES and CQRS level 3.1.1
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
event
bus
Transaction
25. ES and CQRS level 3.1.1
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
event
bus
Transaction
26. ES and CQRS level 3.1.1
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
event
bus
Transaction
27. ES and CQRS level 3.1.1
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
event
bus
Transaction
28. ES and CQRS level 3.1.1
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
event
bus
Transaction
29. ES and CQRS level 3.1.1
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
event
bus
Transaction
?
30. ES and CQRS level 3.2
Events
Client
Query Service
Data access
Commands
Queries
Read
model
Read
model
Read
models
Projector
event
bus
Command Service
Domain
Command Service
Domain
Command Service
Domain
Transaction
`
Sharded
Cluster
31. ES and CQRS level 3.x
+ performance
+ scalability
- eventual consistency
- complex implementation
- locking vs Single Writer
32. ES and CQRS alternatives
● Change Capture Data (CDC) logging instead of event bus?
● event bus instead of DB?
33. Not covered but worth to check
● Command Sourcing
● Event Collaboration
47. Event storage for Akka Persistence
● file
● RDBMS
● Event Store
● MongoDB
● Kafka
● Cassandra
48. akka-persistence-jdbc trap
val theTag = s"%$tag%"
sql"""
SELECT "#$ordering", "#$deleted", "#$persistenceId", "#$sequenceNumber",
"#$message", "#$tags"
FROM (
SELECT * FROM #$theTableName
WHERE "#$tags" LIKE $theTag
AND "#$ordering" > $theOffset
AND "#$ordering" <= $maxOffset
ORDER BY "#$ordering"
)
WHERE rownum <= $max"""
50. Cassandra perfect for ES?
● partitioning by design
● replication by design
● leaderless (no single point of failure)
● optimised for writes (2 nodes = 100 000 tx/s)
● near-linear horizontal scaling
51. ScyllaDB ?
● Cassandra without JVM
○ same protocol, SSTable compatibility
● C++ and Seastar lib
● up to 1,000,000 IOPS/node
● not fully supported by Akka Persistence
109. Saga - choreography
Command Service
Domain
Events
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
event
bus
Transaction
110. Saga - orchestration
Command Service
Domain
Events
Client
Query Service
Data access
Commands Queries
Read
model
Read
model
Read
models
Projector
event
bus
Transaction
Command Service
Domain
Events
Commands
111. Saga
● should be persistable
● events order should be irrelevant
● time window limitation
● compensating action must be commutative
112. Saga
● Sagas with ES
● DDD, Saga & Event-sourcing
● Applying Saga Pattern
● Microservice Patterns
113. ES with RODO/GDPR
● “right to forget” with:
○ data shredding (and/or deleting)
■ events, state, views, read models
○ retention policy
■ message brokers, backups, logs
○ data before RODO migration
114. ES and CQRS level 3.2
Events
Client
Query Service
Data access
Commands
Queries
Read
model
Read
model
Read
models
Projector
event
bus
Command Service
Domain
Command Service
Domain
Command Service
Domain
Transaction
Sharding
Clustering
121. Cluster best practises
● remember about the split brain
● very good monitoring & alerting
● a lot of failover tests
● cluster also on dev/staging
● keep it as small as possible (code base, number of nodes, etc.)
122. Summary
● carefully choose ES lib/framework
● there is no perfect database for event sourcing
● understand event/command/state schema evolution
● eventual consistency is your friend
● scaling is complex
● database inside-out
● log-based processing mindset
123. Want to learn more?
● Reactive Event Sourcing with Akka (Java/Scala)
● Akka Typed Fundamentals (Java/Scala)
● Akka Cluster
● Performance tests with Gatling