An Abridged Guide to Event Sourcing

Tomer Gabel
Tomer GabelConsulting Engineer at Substrate Software Services
An Abridged Guide
to Event Sourcing
Tomer Gabel
Reversim Summit
Tel-Aviv 2017
Image: Jack Zalium, “Abriged” via Flickr (CC-BY-ND 2.0)
Background
• ADI is a site builder
• It’s pretty nifty
– Huge web application
– … and it even works!
• A cool app isn’t enough
• Sites have to be stored
somewhere!
Requirements
• Features:
– Store “site” blobs
– Store version history
– Soft-delete only
• Not required:
– Concurrent editing
Image: ImgFlip
1. WHY CRUD SUCKS
crud Source: Oxford Dictionary
/krəd/
noun informal
1. A substance which is considered unpleasant or disgusting,
typically because of its dirtiness.
2. Nonsense.
Mutation Anxiety
• So what’s wrong with CRUD?
• Create
• Read
• Update
• Delete
• It’s all about mutable state
Mutation Anxiety
Mutable state is bad.
• Old data is lost
• Hard to debug
• Can’t fix retroactively
• No built-in auditing
Image: David Bleasdale, “Fahrenheit 451” via Flickr (CC-BY 2.0)
Mutation Anxiety
“Who told you
you’re allowed to
destroy data?”
-- Greg Young
Image: Code on the Beach speaker profile (source)
Not To Scale
• CRUD implies:
– One source of truth
– Reads, writes against
the same store
– Full consistency (ACID)
• Difficult to scale!
Image: Jason Baker, “Bank Vaults under Hotels in Toronto, Ontari” via Flickr (CC-BY 2.0)
What’s Holding Us Back?
• Strong consistency
– Assumed with RDBMS
– Often not required!
• Consistency is a product concern
– “Does this have to be 100% fresh?”
– “No? So how stale can this get?”
Images: “Homemade Bread Freshly Baked” via MaxPixel (CC0, above); Tasha, “Homemade Croutons” via Flickr (CC-BY 2.0, below)
What’s Holding Us Back?
• Storage cost
– “How long must I hold on to data?”
– “What do you mean forever?!”
• Cost is an operational concern
– “So how much data is there?”
– “How much will it cost to retain?”
Image: Calvin Fraites via Flickr (CC-BY-NC-ND 2.0)
2. A BETTER WAY
“A database is just a view over its
transaction log.”
-- Ancient Vulcan proverb
Event Sourcing
• A very simple pattern
• Each entity has its own
event stream
– Events are facts
– Events are immutable
– Events are forever
Opened Account
Deposited $100
Withdrawn $25
Deposited $12
Balance:
$87
CQRS
• Writes simply append events
• But reads are projections:
– Full snapshots
– Partial/filter queries
– Views and joins
• Two separate concerns!
– Different schemas
– Different instances
– Even different data stores!
Time
Site 1
Created
Site 1
Updated
Site 1
Updated
Site 2
Created
Site 1
Deleted
Site ID Active
1 false
2 true
“active sites”
Better how?
• Tunable consistency
– Full or eventual
– Per use-case!
• Decoupled reads/writes
– Better scale
– Much more flexible!
• Built-in auditing
– Easier to debug
– Replayable!
3. WALKTHROUGH
Image: Pöllö via Wikimedia Commons (CC-BY 3.0)
An Event Model
• Our business domain:
a website
• Our use cases:
– “Let’s try this thing out”
– “Adding more stuff”
– “Oops! Revert”
– “OK, I’m outta here”
Created
Updated
Deleted
Restored
Storing Events
• What’s in an event?
– The entity (i.e. site) ID
– Some notion of time*
– Event data (e.g. type)
– Some metadata
• We can model this!
Column Type PK? Null?
site_id binary(16) ✔ ✖
version int ✔ ✖
created_at timestamp ✖ ✖
payload blob ✖ ✖
* No, you can’t use timestamps for versioning. More on this later
Sketching the API
• Commands are wishes
• Wishes may be rejected:
– Conflicting updates
– Stale version
– Invalid arguments
• Or granted:
– Result: new events!
Command SLA
Create Site Reasonable
Get Latest Very fast
Get Version Reasonable
Update Very fast
Delete Reasonable
Restore Reasonable
Architecture 101
Front-end
Event
Store
Site Service
Site
Materializer
Command
Appended
Events
Stored Events
Snapsho
t
Hold Your Horses
• Still some concerns:
–Conflicting updates
–Performance
–Operations
• Let’s sort ‘em out
Image: Woodennature, “Slow Down” via Wikimedia Commons (CC-BY 3.0)
Conflicting Updates
• Concurrent operations
can conflict
– Rapid site activity
– Multiple open tabs
– Normal network issues
• You have to deal with it
Created
Updated
Updated
Update Update
V0
V1
V2
Time
?
Conflicting Updates
• Strategies
– Last-write wins
– Optimistic locking
– Smart resolution/merge
• In our case
– No concurrent editing
– Simple optimistic locking
Created
Updated
Updated
Update Update
V0
V1
V2
Time
?
Performance
• Updates are easy
• What about reads?
– Load all events for
site
– Feed into materializer
– Spit snapshot out
• This can hurt.
Image: Dominik Kowanda, “Autobahn” via Flickr (CC-BY-ND 2.0)
Performance
• How many events?
– Domain-specific
(for ADI, easily 1000s)
– But events never die
• How big are they?
– Again, domain-specific
– Let’s assume 10-100KB
• Naïve reads will fail.
Image: madaise, “Jenga” via Flickr (CC-BY-ND 2.0)
Performance
• Remember CQRS?
– Reads are distinct from writes
– We can use another store!
• We’ll use snapshots
– Immutable
– Ephemeral
– Tunable (space/performance)
Time
V0
V1
V2
V3
V4
V5
V6
V7
…
S3
S6
Architecture Redux
Front-end
Event
Store
Site Service
Site
Materializer Stored Events
Snapsho
t
Snapshot
Store Base
Snapshot
Snapshot
StrategyPersisted
Snapshot
4. LESSONS LEARNED
No Silver Bullet
• Event sourcing is awesome
• But it’s a trade-off
– Learning curve
– Increased storage
– More knobs to turn
• Make it wisely!
Image: Ed Schipul, “silver bullet” via Flickr (CC-BY-SA 2.0)
Eventual Consistency
• Consistency is a
tradeoff
– Performance
– Complexity
– Cost (tech, support)
• Make it wisely!
Image: Michael Coghlan, “Scales of Justice - Frankfurt Version” via Flickr (CC-BY-SA 2.0)
Forethought
• Define target SLAs
– Latency
– Consistency
• Place sanity limits
– Stream size
– Write throttling
• Invest in tooling
– Debug/replay
– Schema evolution
Image: U.S. Army Corps of Engineers, “USACE Base Camp Development Planning Course” via Flickr (CC-BY 2.0)
Further Reading
Scaling Event Sourcing for
Netflix Downloads
Phillipa Avery & Robert Reta
How shit works: Time
Tomer Gabel
QUESTIONS?
Thank you for listening
tomer@tomergabel.com
@tomerg
http://engineering.wix.com
Sample implementation:
http://tinyurl.com/event-sourcing-sample
This work is licensed under a Creative
Commons Attribution-ShareAlike 4.0
International License.
1 of 32

Recommended

Slaying Sacred Cows: Deconstructing Dependency Injection by
Slaying Sacred Cows: Deconstructing Dependency InjectionSlaying Sacred Cows: Deconstructing Dependency Injection
Slaying Sacred Cows: Deconstructing Dependency InjectionTomer Gabel
1.3K views34 slides
Play concurrency by
Play concurrencyPlay concurrency
Play concurrencyJustin Long
1.4K views26 slides
Jenkins, jclouds, CloudStack, and CentOS by David Nalley by
Jenkins, jclouds, CloudStack, and CentOS by David NalleyJenkins, jclouds, CloudStack, and CentOS by David Nalley
Jenkins, jclouds, CloudStack, and CentOS by David Nalleybuildacloud
892 views23 slides
Drools 6.0 (JudCon 2013) by
Drools 6.0 (JudCon 2013)Drools 6.0 (JudCon 2013)
Drools 6.0 (JudCon 2013)Mark Proctor
4.9K views106 slides
Bursting into the public Cloud - Sharing my experience doing it at large scal... by
Bursting into the public Cloud - Sharing my experience doing it at large scal...Bursting into the public Cloud - Sharing my experience doing it at large scal...
Bursting into the public Cloud - Sharing my experience doing it at large scal...Igor Sfiligoi
113 views25 slides
Openstack Study Nova 1 by
Openstack Study Nova 1Openstack Study Nova 1
Openstack Study Nova 1Jinho Shin
5.4K views23 slides

More Related Content

What's hot

OpenStack Framework Introduction by
OpenStack Framework IntroductionOpenStack Framework Introduction
OpenStack Framework IntroductionJason TC HOU (侯宗成)
24.6K views94 slides
Openstack presentation by
Openstack presentationOpenstack presentation
Openstack presentationSankalp Jain
1.4K views65 slides
Meetup on Apache Zookeeper by
Meetup on Apache ZookeeperMeetup on Apache Zookeeper
Meetup on Apache ZookeeperAnshul Patel
678 views30 slides
Introduction to Apache ZooKeeper by
Introduction to Apache ZooKeeperIntroduction to Apache ZooKeeper
Introduction to Apache ZooKeeperknowbigdata
1.7K views41 slides
DevCloud - Setup and Demo on Apache CloudStack by
DevCloud - Setup and Demo on Apache CloudStack DevCloud - Setup and Demo on Apache CloudStack
DevCloud - Setup and Demo on Apache CloudStack buildacloud
4.2K views9 slides
Ceph with CloudStack by
Ceph with CloudStackCeph with CloudStack
Ceph with CloudStackShapeBlue
730 views29 slides

What's hot(20)

Openstack presentation by Sankalp Jain
Openstack presentationOpenstack presentation
Openstack presentation
Sankalp Jain1.4K views
Meetup on Apache Zookeeper by Anshul Patel
Meetup on Apache ZookeeperMeetup on Apache Zookeeper
Meetup on Apache Zookeeper
Anshul Patel678 views
Introduction to Apache ZooKeeper by knowbigdata
Introduction to Apache ZooKeeperIntroduction to Apache ZooKeeper
Introduction to Apache ZooKeeper
knowbigdata1.7K views
DevCloud - Setup and Demo on Apache CloudStack by buildacloud
DevCloud - Setup and Demo on Apache CloudStack DevCloud - Setup and Demo on Apache CloudStack
DevCloud - Setup and Demo on Apache CloudStack
buildacloud4.2K views
Ceph with CloudStack by ShapeBlue
Ceph with CloudStackCeph with CloudStack
Ceph with CloudStack
ShapeBlue730 views
Novalug 07142012 by Mandi Walls
Novalug 07142012Novalug 07142012
Novalug 07142012
Mandi Walls1.1K views
Oracle Java Cloud Service JCS (and WebLogic 12c) - What you Should Know by Frank Munz
Oracle Java Cloud Service JCS (and WebLogic 12c) - What you Should KnowOracle Java Cloud Service JCS (and WebLogic 12c) - What you Should Know
Oracle Java Cloud Service JCS (and WebLogic 12c) - What you Should Know
Frank Munz2.5K views
CloudStack vs OpenStack vs Eucalyptus: IaaS Private Cloud Brief Comparison by bizalgo
CloudStack vs OpenStack vs Eucalyptus: IaaS Private Cloud Brief ComparisonCloudStack vs OpenStack vs Eucalyptus: IaaS Private Cloud Brief Comparison
CloudStack vs OpenStack vs Eucalyptus: IaaS Private Cloud Brief Comparison
bizalgo32.4K views
A year in the life of a Grails startup by tomaslin
A year in the life of a Grails startupA year in the life of a Grails startup
A year in the life of a Grails startup
tomaslin2.7K views
[KGC 2012] Online Game Server Architecture Case Study Performance and Security by Seungmin Shin
[KGC 2012] Online Game Server Architecture Case Study Performance and Security[KGC 2012] Online Game Server Architecture Case Study Performance and Security
[KGC 2012] Online Game Server Architecture Case Study Performance and Security
Seungmin Shin6.4K views
JavaOne 2014: Taming the Cloud Database with jclouds by zshoylev
JavaOne 2014: Taming the Cloud Database with jcloudsJavaOne 2014: Taming the Cloud Database with jclouds
JavaOne 2014: Taming the Cloud Database with jclouds
zshoylev457 views
OpenStack + VMware: Deploy, Upgrade, & Operate a Powerful Production OpenStac... by Mark Voelker
OpenStack + VMware: Deploy, Upgrade, & Operate a Powerful Production OpenStac...OpenStack + VMware: Deploy, Upgrade, & Operate a Powerful Production OpenStac...
OpenStack + VMware: Deploy, Upgrade, & Operate a Powerful Production OpenStac...
Mark Voelker380 views
Apache zookeeper seminar_trinh_viet_dung_03_2016 by Viet-Dung TRINH
Apache zookeeper seminar_trinh_viet_dung_03_2016Apache zookeeper seminar_trinh_viet_dung_03_2016
Apache zookeeper seminar_trinh_viet_dung_03_2016
Viet-Dung TRINH426 views
KVM High Availability Regardless of Storage - Gabriel Brascher, VP of Apache ... by ShapeBlue
KVM High Availability Regardless of Storage - Gabriel Brascher, VP of Apache ...KVM High Availability Regardless of Storage - Gabriel Brascher, VP of Apache ...
KVM High Availability Regardless of Storage - Gabriel Brascher, VP of Apache ...
ShapeBlue837 views
Introduction to Apache CloudStack by David Nalley by buildacloud
Introduction to Apache CloudStack by David NalleyIntroduction to Apache CloudStack by David Nalley
Introduction to Apache CloudStack by David Nalley
buildacloud1.7K views

Similar to An Abridged Guide to Event Sourcing

KubeCon 2019 Recap (Parts 1-3) by
KubeCon 2019 Recap (Parts 1-3)KubeCon 2019 Recap (Parts 1-3)
KubeCon 2019 Recap (Parts 1-3)Ford Prior
84 views83 slides
An In-depth look at application containers by
An In-depth look at application containersAn In-depth look at application containers
An In-depth look at application containersJohn Kinsella
412 views33 slides
Microservices, Kubernetes, and Application Modernization Done Right by
Microservices, Kubernetes, and Application Modernization Done RightMicroservices, Kubernetes, and Application Modernization Done Right
Microservices, Kubernetes, and Application Modernization Done RightLightbend
1.4K views67 slides
Multi-Cluster and Failover for Apache Kafka - Kafka Summit SF 17 by
Multi-Cluster and Failover for Apache Kafka - Kafka Summit SF 17Multi-Cluster and Failover for Apache Kafka - Kafka Summit SF 17
Multi-Cluster and Failover for Apache Kafka - Kafka Summit SF 17Gwen (Chen) Shapira
9.6K views33 slides
Digital Transformation with Kubernetes, Containers, and Microservices by
Digital Transformation with Kubernetes, Containers, and MicroservicesDigital Transformation with Kubernetes, Containers, and Microservices
Digital Transformation with Kubernetes, Containers, and MicroservicesLightbend
1.3K views70 slides
Rails scaling by
Rails scalingRails scaling
Rails scalingSebastian Roth
945 views30 slides

Similar to An Abridged Guide to Event Sourcing(20)

KubeCon 2019 Recap (Parts 1-3) by Ford Prior
KubeCon 2019 Recap (Parts 1-3)KubeCon 2019 Recap (Parts 1-3)
KubeCon 2019 Recap (Parts 1-3)
Ford Prior84 views
An In-depth look at application containers by John Kinsella
An In-depth look at application containersAn In-depth look at application containers
An In-depth look at application containers
John Kinsella412 views
Microservices, Kubernetes, and Application Modernization Done Right by Lightbend
Microservices, Kubernetes, and Application Modernization Done RightMicroservices, Kubernetes, and Application Modernization Done Right
Microservices, Kubernetes, and Application Modernization Done Right
Lightbend1.4K views
Multi-Cluster and Failover for Apache Kafka - Kafka Summit SF 17 by Gwen (Chen) Shapira
Multi-Cluster and Failover for Apache Kafka - Kafka Summit SF 17Multi-Cluster and Failover for Apache Kafka - Kafka Summit SF 17
Multi-Cluster and Failover for Apache Kafka - Kafka Summit SF 17
Gwen (Chen) Shapira9.6K views
Digital Transformation with Kubernetes, Containers, and Microservices by Lightbend
Digital Transformation with Kubernetes, Containers, and MicroservicesDigital Transformation with Kubernetes, Containers, and Microservices
Digital Transformation with Kubernetes, Containers, and Microservices
Lightbend1.3K views
Kafka Summit SF 2017 - One Data Center is Not Enough: Scaling Apache Kafka Ac... by confluent
Kafka Summit SF 2017 - One Data Center is Not Enough: Scaling Apache Kafka Ac...Kafka Summit SF 2017 - One Data Center is Not Enough: Scaling Apache Kafka Ac...
Kafka Summit SF 2017 - One Data Center is Not Enough: Scaling Apache Kafka Ac...
confluent4.5K views
Disaster Recovery Plans for Apache Kafka by confluent
Disaster Recovery Plans for Apache KafkaDisaster Recovery Plans for Apache Kafka
Disaster Recovery Plans for Apache Kafka
confluent11.3K views
Chirp 2010: Scaling Twitter by John Adams
Chirp 2010: Scaling TwitterChirp 2010: Scaling Twitter
Chirp 2010: Scaling Twitter
John Adams38K views
Lagom at hybris Reactive Software Munich Meetup, April 13, 2016 by Lutz Hühnken
Lagom at hybris Reactive Software Munich Meetup, April 13, 2016Lagom at hybris Reactive Software Munich Meetup, April 13, 2016
Lagom at hybris Reactive Software Munich Meetup, April 13, 2016
Lutz Hühnken957 views
Building a smarter application stack - service discovery and wiring for Docker by Tomas Doran
Building a smarter application stack - service discovery and wiring for DockerBuilding a smarter application stack - service discovery and wiring for Docker
Building a smarter application stack - service discovery and wiring for Docker
Tomas Doran6.2K views
Building a Smarter Application Stack by Docker, Inc.
Building a Smarter Application StackBuilding a Smarter Application Stack
Building a Smarter Application Stack
Docker, Inc.580 views
Building a smarter application Stack by Tomas Doran from Yelp by dotCloud
Building a smarter application Stack by Tomas Doran from YelpBuilding a smarter application Stack by Tomas Doran from Yelp
Building a smarter application Stack by Tomas Doran from Yelp
dotCloud166K views
PlayStation and Lucene - Indexing 1M documents per second: Presented by Alexa... by Lucidworks
PlayStation and Lucene - Indexing 1M documents per second: Presented by Alexa...PlayStation and Lucene - Indexing 1M documents per second: Presented by Alexa...
PlayStation and Lucene - Indexing 1M documents per second: Presented by Alexa...
Lucidworks617 views
Put Your Thinking CAP On by Tomer Gabel
Put Your Thinking CAP OnPut Your Thinking CAP On
Put Your Thinking CAP On
Tomer Gabel3.5K views
Scaling up to 30M users - The Wix Story by Aviran Mordo
Scaling up to 30M users - The Wix StoryScaling up to 30M users - The Wix Story
Scaling up to 30M users - The Wix Story
Aviran Mordo4.2K views
Rackspace: Email's Solution for Indexing 50K Documents per Second: Presented ... by Lucidworks
Rackspace: Email's Solution for Indexing 50K Documents per Second: Presented ...Rackspace: Email's Solution for Indexing 50K Documents per Second: Presented ...
Rackspace: Email's Solution for Indexing 50K Documents per Second: Presented ...
Lucidworks3.1K views

More from Tomer Gabel

How shit works: Time by
How shit works: TimeHow shit works: Time
How shit works: TimeTomer Gabel
342 views53 slides
Nondeterministic Software for the Rest of Us by
Nondeterministic Software for the Rest of UsNondeterministic Software for the Rest of Us
Nondeterministic Software for the Rest of UsTomer Gabel
329 views39 slides
How shit works: the CPU by
How shit works: the CPUHow shit works: the CPU
How shit works: the CPUTomer Gabel
1.8K views38 slides
How Shit Works: Storage by
How Shit Works: StorageHow Shit Works: Storage
How Shit Works: StorageTomer Gabel
914 views44 slides
Java 8 and Beyond, a Scala Story by
Java 8 and Beyond, a Scala StoryJava 8 and Beyond, a Scala Story
Java 8 and Beyond, a Scala StoryTomer Gabel
747 views24 slides
The Wix Microservice Stack by
The Wix Microservice StackThe Wix Microservice Stack
The Wix Microservice StackTomer Gabel
1.7K views42 slides

More from Tomer Gabel(20)

How shit works: Time by Tomer Gabel
How shit works: TimeHow shit works: Time
How shit works: Time
Tomer Gabel342 views
Nondeterministic Software for the Rest of Us by Tomer Gabel
Nondeterministic Software for the Rest of UsNondeterministic Software for the Rest of Us
Nondeterministic Software for the Rest of Us
Tomer Gabel329 views
How shit works: the CPU by Tomer Gabel
How shit works: the CPUHow shit works: the CPU
How shit works: the CPU
Tomer Gabel1.8K views
How Shit Works: Storage by Tomer Gabel
How Shit Works: StorageHow Shit Works: Storage
How Shit Works: Storage
Tomer Gabel914 views
Java 8 and Beyond, a Scala Story by Tomer Gabel
Java 8 and Beyond, a Scala StoryJava 8 and Beyond, a Scala Story
Java 8 and Beyond, a Scala Story
Tomer Gabel747 views
The Wix Microservice Stack by Tomer Gabel
The Wix Microservice StackThe Wix Microservice Stack
The Wix Microservice Stack
Tomer Gabel1.7K views
Scala Refactoring for Fun and Profit (Japanese subtitles) by Tomer Gabel
Scala Refactoring for Fun and Profit (Japanese subtitles)Scala Refactoring for Fun and Profit (Japanese subtitles)
Scala Refactoring for Fun and Profit (Japanese subtitles)
Tomer Gabel6.6K views
Scala Refactoring for Fun and Profit by Tomer Gabel
Scala Refactoring for Fun and ProfitScala Refactoring for Fun and Profit
Scala Refactoring for Fun and Profit
Tomer Gabel985 views
Onboarding at Scale by Tomer Gabel
Onboarding at ScaleOnboarding at Scale
Onboarding at Scale
Tomer Gabel1.5K views
Scala in the Wild by Tomer Gabel
Scala in the WildScala in the Wild
Scala in the Wild
Tomer Gabel2.8K views
Speaking Scala: Refactoring for Fun and Profit (Workshop) by Tomer Gabel
Speaking Scala: Refactoring for Fun and Profit (Workshop)Speaking Scala: Refactoring for Fun and Profit (Workshop)
Speaking Scala: Refactoring for Fun and Profit (Workshop)
Tomer Gabel765 views
Leveraging Scala Macros for Better Validation by Tomer Gabel
Leveraging Scala Macros for Better ValidationLeveraging Scala Macros for Better Validation
Leveraging Scala Macros for Better Validation
Tomer Gabel1.4K views
A Field Guide to DSL Design in Scala by Tomer Gabel
A Field Guide to DSL Design in ScalaA Field Guide to DSL Design in Scala
A Field Guide to DSL Design in Scala
Tomer Gabel6.5K views
Functional Leap of Faith (Keynote at JDay Lviv 2014) by Tomer Gabel
Functional Leap of Faith (Keynote at JDay Lviv 2014)Functional Leap of Faith (Keynote at JDay Lviv 2014)
Functional Leap of Faith (Keynote at JDay Lviv 2014)
Tomer Gabel1.5K views
Scala Back to Basics: Type Classes by Tomer Gabel
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
Tomer Gabel3.7K views
5 Bullets to Scala Adoption by Tomer Gabel
5 Bullets to Scala Adoption5 Bullets to Scala Adoption
5 Bullets to Scala Adoption
Tomer Gabel2.7K views
Nashorn: JavaScript that doesn’t suck (ILJUG) by Tomer Gabel
Nashorn: JavaScript that doesn’t suck (ILJUG)Nashorn: JavaScript that doesn’t suck (ILJUG)
Nashorn: JavaScript that doesn’t suck (ILJUG)
Tomer Gabel5.9K views
Ponies and Unicorns With Scala by Tomer Gabel
Ponies and Unicorns With ScalaPonies and Unicorns With Scala
Ponies and Unicorns With Scala
Tomer Gabel961 views
Lab: JVM Production Debugging 101 by Tomer Gabel
Lab: JVM Production Debugging 101Lab: JVM Production Debugging 101
Lab: JVM Production Debugging 101
Tomer Gabel2.1K views
DevCon³: Scala Best Practices by Tomer Gabel
DevCon³: Scala Best PracticesDevCon³: Scala Best Practices
DevCon³: Scala Best Practices
Tomer Gabel3.7K views

Recently uploaded

LDPC_CODES.ppt by
LDPC_CODES.pptLDPC_CODES.ppt
LDPC_CODES.ppthsomashekar987
16 views44 slides
Design of machine elements-UNIT 3.pptx by
Design of machine elements-UNIT 3.pptxDesign of machine elements-UNIT 3.pptx
Design of machine elements-UNIT 3.pptxgopinathcreddy
32 views31 slides
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc... by
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...csegroupvn
5 views210 slides
fakenews_DBDA_Mar23.pptx by
fakenews_DBDA_Mar23.pptxfakenews_DBDA_Mar23.pptx
fakenews_DBDA_Mar23.pptxdeepmitra8
15 views34 slides
BCIC - Manufacturing Conclave - Technology-Driven Manufacturing for Growth by
BCIC - Manufacturing Conclave -  Technology-Driven Manufacturing for GrowthBCIC - Manufacturing Conclave -  Technology-Driven Manufacturing for Growth
BCIC - Manufacturing Conclave - Technology-Driven Manufacturing for GrowthInnomantra
6 views4 slides
Proposal Presentation.pptx by
Proposal Presentation.pptxProposal Presentation.pptx
Proposal Presentation.pptxkeytonallamon
42 views36 slides

Recently uploaded(20)

Design of machine elements-UNIT 3.pptx by gopinathcreddy
Design of machine elements-UNIT 3.pptxDesign of machine elements-UNIT 3.pptx
Design of machine elements-UNIT 3.pptx
gopinathcreddy32 views
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc... by csegroupvn
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
Design of Structures and Foundations for Vibrating Machines, Arya-ONeill-Pinc...
csegroupvn5 views
fakenews_DBDA_Mar23.pptx by deepmitra8
fakenews_DBDA_Mar23.pptxfakenews_DBDA_Mar23.pptx
fakenews_DBDA_Mar23.pptx
deepmitra815 views
BCIC - Manufacturing Conclave - Technology-Driven Manufacturing for Growth by Innomantra
BCIC - Manufacturing Conclave -  Technology-Driven Manufacturing for GrowthBCIC - Manufacturing Conclave -  Technology-Driven Manufacturing for Growth
BCIC - Manufacturing Conclave - Technology-Driven Manufacturing for Growth
Innomantra 6 views
Effect of deep chemical mixing columns on properties of surrounding soft clay... by AltinKaradagli
Effect of deep chemical mixing columns on properties of surrounding soft clay...Effect of deep chemical mixing columns on properties of surrounding soft clay...
Effect of deep chemical mixing columns on properties of surrounding soft clay...
AltinKaradagli9 views
REACTJS.pdf by ArthyR3
REACTJS.pdfREACTJS.pdf
REACTJS.pdf
ArthyR327 views
Generative AI Models & Their Applications by SN
Generative AI Models & Their ApplicationsGenerative AI Models & Their Applications
Generative AI Models & Their Applications
SN8 views
Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ... by AltinKaradagli
Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ...Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ...
Investigation of Physicochemical Changes of Soft Clay around Deep Geopolymer ...
AltinKaradagli12 views
DevOps-ITverse-2023-IIT-DU.pptx by Anowar Hossain
DevOps-ITverse-2023-IIT-DU.pptxDevOps-ITverse-2023-IIT-DU.pptx
DevOps-ITverse-2023-IIT-DU.pptx
Anowar Hossain12 views
Design_Discover_Develop_Campaign.pptx by ShivanshSeth6
Design_Discover_Develop_Campaign.pptxDesign_Discover_Develop_Campaign.pptx
Design_Discover_Develop_Campaign.pptx
ShivanshSeth632 views

An Abridged Guide to Event Sourcing

  • 1. An Abridged Guide to Event Sourcing Tomer Gabel Reversim Summit Tel-Aviv 2017 Image: Jack Zalium, “Abriged” via Flickr (CC-BY-ND 2.0)
  • 2. Background • ADI is a site builder • It’s pretty nifty – Huge web application – … and it even works! • A cool app isn’t enough • Sites have to be stored somewhere!
  • 3. Requirements • Features: – Store “site” blobs – Store version history – Soft-delete only • Not required: – Concurrent editing Image: ImgFlip
  • 4. 1. WHY CRUD SUCKS crud Source: Oxford Dictionary /krəd/ noun informal 1. A substance which is considered unpleasant or disgusting, typically because of its dirtiness. 2. Nonsense.
  • 5. Mutation Anxiety • So what’s wrong with CRUD? • Create • Read • Update • Delete • It’s all about mutable state
  • 6. Mutation Anxiety Mutable state is bad. • Old data is lost • Hard to debug • Can’t fix retroactively • No built-in auditing Image: David Bleasdale, “Fahrenheit 451” via Flickr (CC-BY 2.0)
  • 7. Mutation Anxiety “Who told you you’re allowed to destroy data?” -- Greg Young Image: Code on the Beach speaker profile (source)
  • 8. Not To Scale • CRUD implies: – One source of truth – Reads, writes against the same store – Full consistency (ACID) • Difficult to scale! Image: Jason Baker, “Bank Vaults under Hotels in Toronto, Ontari” via Flickr (CC-BY 2.0)
  • 9. What’s Holding Us Back? • Strong consistency – Assumed with RDBMS – Often not required! • Consistency is a product concern – “Does this have to be 100% fresh?” – “No? So how stale can this get?” Images: “Homemade Bread Freshly Baked” via MaxPixel (CC0, above); Tasha, “Homemade Croutons” via Flickr (CC-BY 2.0, below)
  • 10. What’s Holding Us Back? • Storage cost – “How long must I hold on to data?” – “What do you mean forever?!” • Cost is an operational concern – “So how much data is there?” – “How much will it cost to retain?” Image: Calvin Fraites via Flickr (CC-BY-NC-ND 2.0)
  • 11. 2. A BETTER WAY “A database is just a view over its transaction log.” -- Ancient Vulcan proverb
  • 12. Event Sourcing • A very simple pattern • Each entity has its own event stream – Events are facts – Events are immutable – Events are forever Opened Account Deposited $100 Withdrawn $25 Deposited $12 Balance: $87
  • 13. CQRS • Writes simply append events • But reads are projections: – Full snapshots – Partial/filter queries – Views and joins • Two separate concerns! – Different schemas – Different instances – Even different data stores! Time Site 1 Created Site 1 Updated Site 1 Updated Site 2 Created Site 1 Deleted Site ID Active 1 false 2 true “active sites”
  • 14. Better how? • Tunable consistency – Full or eventual – Per use-case! • Decoupled reads/writes – Better scale – Much more flexible! • Built-in auditing – Easier to debug – Replayable!
  • 15. 3. WALKTHROUGH Image: Pöllö via Wikimedia Commons (CC-BY 3.0)
  • 16. An Event Model • Our business domain: a website • Our use cases: – “Let’s try this thing out” – “Adding more stuff” – “Oops! Revert” – “OK, I’m outta here” Created Updated Deleted Restored
  • 17. Storing Events • What’s in an event? – The entity (i.e. site) ID – Some notion of time* – Event data (e.g. type) – Some metadata • We can model this! Column Type PK? Null? site_id binary(16) ✔ ✖ version int ✔ ✖ created_at timestamp ✖ ✖ payload blob ✖ ✖ * No, you can’t use timestamps for versioning. More on this later
  • 18. Sketching the API • Commands are wishes • Wishes may be rejected: – Conflicting updates – Stale version – Invalid arguments • Or granted: – Result: new events! Command SLA Create Site Reasonable Get Latest Very fast Get Version Reasonable Update Very fast Delete Reasonable Restore Reasonable
  • 20. Hold Your Horses • Still some concerns: –Conflicting updates –Performance –Operations • Let’s sort ‘em out Image: Woodennature, “Slow Down” via Wikimedia Commons (CC-BY 3.0)
  • 21. Conflicting Updates • Concurrent operations can conflict – Rapid site activity – Multiple open tabs – Normal network issues • You have to deal with it Created Updated Updated Update Update V0 V1 V2 Time ?
  • 22. Conflicting Updates • Strategies – Last-write wins – Optimistic locking – Smart resolution/merge • In our case – No concurrent editing – Simple optimistic locking Created Updated Updated Update Update V0 V1 V2 Time ?
  • 23. Performance • Updates are easy • What about reads? – Load all events for site – Feed into materializer – Spit snapshot out • This can hurt. Image: Dominik Kowanda, “Autobahn” via Flickr (CC-BY-ND 2.0)
  • 24. Performance • How many events? – Domain-specific (for ADI, easily 1000s) – But events never die • How big are they? – Again, domain-specific – Let’s assume 10-100KB • Naïve reads will fail. Image: madaise, “Jenga” via Flickr (CC-BY-ND 2.0)
  • 25. Performance • Remember CQRS? – Reads are distinct from writes – We can use another store! • We’ll use snapshots – Immutable – Ephemeral – Tunable (space/performance) Time V0 V1 V2 V3 V4 V5 V6 V7 … S3 S6
  • 26. Architecture Redux Front-end Event Store Site Service Site Materializer Stored Events Snapsho t Snapshot Store Base Snapshot Snapshot StrategyPersisted Snapshot
  • 28. No Silver Bullet • Event sourcing is awesome • But it’s a trade-off – Learning curve – Increased storage – More knobs to turn • Make it wisely! Image: Ed Schipul, “silver bullet” via Flickr (CC-BY-SA 2.0)
  • 29. Eventual Consistency • Consistency is a tradeoff – Performance – Complexity – Cost (tech, support) • Make it wisely! Image: Michael Coghlan, “Scales of Justice - Frankfurt Version” via Flickr (CC-BY-SA 2.0)
  • 30. Forethought • Define target SLAs – Latency – Consistency • Place sanity limits – Stream size – Write throttling • Invest in tooling – Debug/replay – Schema evolution Image: U.S. Army Corps of Engineers, “USACE Base Camp Development Planning Course” via Flickr (CC-BY 2.0)
  • 31. Further Reading Scaling Event Sourcing for Netflix Downloads Phillipa Avery & Robert Reta How shit works: Time Tomer Gabel
  • 32. QUESTIONS? Thank you for listening tomer@tomergabel.com @tomerg http://engineering.wix.com Sample implementation: http://tinyurl.com/event-sourcing-sample This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.