CQRS e Event Sourcing
CHE L'EVENTO SIA CON TE
ECCENTRIC DEVELOPER
Oscar Fanelli
Engineering Manager
Symfony / ZF / ReactJS
Team Management
Astronomia
JIRA / PhpStorm
Faccio cose
Conosco gente
Stringo mani
Pigio sulla tastiera
Troubles
- How many customers bought an item after putting it in the basket?
- How many users changed their mind about going to an event?
- You are working on a banking application
- 2 weeks ago you released a bug in production
more explosive example
APPLICATION
Event Storage
a whole new way to go crazy while coding
APPLICATION
ID | NAME | € |
---|---|---|
4 | Paul Red | 50 |
ID | NAME | € |
---|---|---|
4 | Paul Red | 250 |
ID | NAME | € |
---|---|---|
4 | Paul Red | 200 |
ID | NAME | € |
---|---|---|
4 | Paul Red | 120 |
APPLICATION
ID | EVENT | € |
---|---|---|
4 | Deposit | 50 |
ID | EVENT | € |
---|---|---|
4 | Deposit | 200 |
ID | EVENT | € |
---|---|---|
4 | Withdrawal | 50 |
ID | EVENT | € |
---|---|---|
4 | Transfer | 80 |
Targets
- Reconstruct past states
- Adjust current state to cope with retroactive changes (replay)
- Complete rebuild
APPLICATION
MANAGER
EVENT STORAGE
event
EVENT
BUS
action
- Reconstruct past states
- Adjust current state to cope with retroactive changes (replay)
- Complete rebuild
- High-reliability
Targets
APPLICATION
EVENT STORAGE
PROVIDER
PROJECTOR 0
MANAGER
EVENT
BUS
PROJECTOR 1
PROJECTOR 2
PROJECTOR 3
EVENT STORAGE
Benefits
- Event logging
- Custom test environment (stop, rewind & replay)
- Debugging
- Seeds for new developers
Watch out
- Events should have enough info to reverse them
- External updates (third-party services) should be aware of a replay
- External queries (es: exchange rate)
- Lot of slow queries
- Not easy to adopt
Auditing Bundle
only for lazy developers
APPLICATION
AUDITING
BUNDLE
EVENT STORAGE
event
EVENT
BUS
action
Auditing Bundle
- Lighter version of the Event Sourcing
- Subscriber of:
- Custom events
- Entity events (CRUD)
- Document events (CRUD)
- Writes on the Event Storage whenever something happen
auditing_bundle:
network: 'Your Application Name'
whitelists:
# entity lifecycles events that you want to whitelist (if empty, no entity lifecycle will be stored)
# possible values: create|read|update|delete
entity_lifecycles: [ "create", "read", "update", "delete" ]
# document lifecycles events that you want to whitelist (if empty, no entity lifecycle will be stored)
# possible values: create|read|update|delete
document_lifecycles: [ "create", "read", "update", "delete" ]
# custom objects that you want to audit (if empty, every object will be audited)
objects: []
# custom events that you want to audit (if empty, every custom event will be audited)
events: []
blacklists:
# custom objects that you don't want to audit (if empty, every object will be audited)
objects: []
# custom events that you don't want to audit (if empty, every custom event will be audited)
events: []
APPLICATION
AUDITING
BUNDLE
EVENT STORAGE
event
???
action
Who said CQRS?
Command Query Responsibility Segregation
CQRS
- Perfect with Event Sourcing
- Who reads data != who writes data
- Provider
- Manager
- Helpful with business logic
Tips
- Well-named event name (avoid update/inserted)
- Events represent only what just happened
- Events are immutable
In the end...
- So much WOW!
- You don't have to implement only this pattern
- Don't use it if you don't need it
CQRS and Event Sourcing
SO LONG AND THANKS FOR ALL THE FISH.
oscar.fanelli@gmail.com
@nesis
OSCAR FANELLI
CQRS e Event Sourcing: che l'Evento sia con te
By Oscar Fanelli
CQRS e Event Sourcing: che l'Evento sia con te
- 2,202