How shit works: Time

Tomer Gabel
Tomer GabelConsulting Engineer at Substrate Software Services
THE STORY OF ADI
Meet
Adi Awanta.
He has a dream.
Adi is fashionable
• Event sourcing is
the future, they say
• Event sourcing is
the shit, he agrees
Adi is fashionable
• Event sourcing is
the future, they say
• Event sourcing is
the shit, he agrees
• … and designs an
event model
Created
Modified
Published
Archived
Restored
Column Type Key Null
site_id binary(16)
event_time timestamp
event_type enum(…)
payload mediumblob
Adi is fashionable
HEY, KIDS
Can you guess what happens next?
Adi is confused
• Bad shit happens
• Event streams exhibit:
– Out of order events
– Conflicting events
– Impossible states
How shit works:
Time
Tomer Gabel
Wix Engineering Conference
May 2017
Image: Vera Kratochvil (public domain)
Time (noun)
“… the system of those sequential
relations that any event has to any other,
as past, present, or future; indefinite and
continuous duration regarded as that in
which events succeed one another.”
-- dictionary.com
Time (noun)
“… the system of those sequential
relations that any event has to any other,
as past, present, or future; indefinite and
continuous duration regarded as that in
which events succeed one another.”
-- dictionary.com
Modeling time
• Encoding
– Resolution
– Epoch
“Real” time
Epoch
Resolution
T1 T2 T3 … Tn
Modeling time
• Encoding
– Resolution
– Epoch
T3
Instant
(or “event”)
Modeling time
• Encoding
– Resolution
– Epoch
T3
A
B
Conflicting
events
Modeling time
• Encoding
– Resolution
– Epoch
• Bootstrapping
– Manual
– Battery-backed
– NTP
Epoch
???
Modeling time
• Encoding
– Resolution
– Epoch
• Bootstrapping
– Manual
– Battery-backed
– NTP
• Updating
T1 T2
Modeling time
• Encoding
– Resolution
– Epoch
• Bootstrapping
– Manual
– Battery-backed
– NTP
• Updating T = T+1
T1 T2
W
hen?
System Clock (RTC)
Modeling time
• Encoding
– Resolution
– Epoch
• Bootstrapping
– Manual
– Battery-backed
– NTP
• Updating
Image: Jeremy Saglimbeni on Vimeo (CC BY-SA 3.0)
RTC isn’t perfect
• Alas, clocks drift
• Subtle causes
– Temperature
– Power supply
– General relativity
– Cosmic radiation
– Alien gamma rays
Image showing ~220µs clock drift by Luke Bigum
Distributed time
Host A
Host C
Host B
Host D
RTC RTC
RTC RTC
Event Stream Event
Store
RTC
event_time = ?
Time source: Application
Host A
Host C
Host B
Host D
RTC RTC
RTC RTC
Event Stream Event
Store
RTC
1
2
3
4
Time source: Application
Host A
Host C
Host B
Host D
RTC RTC
RTC RTC
Event Stream Event
Store
RTC
1
2
3
4
Clocks must be
synchronized!
What about NTP?
Host A
Host C
Host B
Host D
RTC RTC
RTC RTC
Event
Store
RTC
NTP
What about NTP?
Host A
Host C
Host B
Host D
RTC RTC
RTC RTC
Event
Store
RTC
NTP
Accurate within
~10ms
Fig. 1, “Characterizing Quality of Time and Topology in a Time Synchronization Network”, Murta et al
DEALING WITH TIME IS HARD
TL;DR
Reframing the problem
• When reading events
– Do we need wall time?
– We don’t care about it
– It’s just metadata
• We only care about
ordering events
Created
(2017-05-03 10:15)
Updated
(2017-05-03 10:27)
Archived
(2017-05-03 10:55)
Reprise: Time (noun)
“… the system of those sequential
relations that any event has to any other,
as past, present, or future; indefinite and
continuous duration regarded as that in
which events succeed one another.”
-- dictionary.com
Reprise: Time (noun)
“… the system of those sequential
relations that any event has to any other,
as past, present, or future; indefinite and
continuous duration regarded as that in
which events succeed one another.”
-- dictionary.com
Causality
• Take any two events
• What relationship can
they have?
Created
Updated Updated
Archived
Causality
• Take any two events
• What relationship can
they have?
– Happens before: A→B
A. Created
B. Updated Updated
Archived
Causality
• Take any two events
• What relationship can
they have?
– Happens before: A→B
– Concurrent: A↛B and
B↛A
Created
A. Updated B. Updated
Archived
Lamport timestamps
• Provides partial
ordering of events
• Advantages:
– Respects causality
– Low overhead
– Simple to implement
Event
Store
Host
A
Host
C
Host
B
“Time, Clocks, and the Ordering of Events in a Distributed System”, Leslie Lamport, 1978
Event
Store
Host
A
Host
C
Host
B
Lamport timestamps
• Each host maintains
local logical clock
• Start with T=0
T=0
T=0T=0
Event
Store
Host
A
Host
C
Host
B
Lamport timestamps
• Each host maintains
local logical clock
• Start with T=0
• On send (i.e. write):
– Increment local clock
– Attach to message
T=0
T=0T=0
T=1
Lamport timestamps
• On receive (i.e. read):
– Process event if T < Tin
Event
Store
Host
A
Host
C
Host
B
T=0
Tin=1
Lamport timestamps
• On receive (i.e. read):
– Process event if T < Tin
– Update clock past the
latest event:
T = max(T, Tin) + 1
• All done!
Event
Store
Host
A
Host
C
Host
B
T=0
Tin=1
T=2
… well, almost
• This is a partial order
• Causality is dealt with
– A→B ⇒ T(A) < T(B)
• What about concurrency?
– A↛B and B↛A ⇒ ?
Final touches
• We want total ordering
– Must be stable
– Need not correspond to
real time
Created
Updated Updated
Archived
T=1
T=5
T=2T=2
Final touches
• We want total ordering
– Must be stable
– Need not correspond to
real time
• Breaking the tie:
– Use any consistent key
– Host name, MAC, IP…
Created
Updated Updated
Archived
T=1
T=5
T=2T=2
What did we gain?
• We can assign versions that…
– Respect causality
– Are totally ordered
– Don’t rely on the RTC
• We’ve enabled serializability!
MAKE ADI
GREAT AGAIN!
Enough theory. Let’s
Versioning
• Our versioning
mechanism must:
– Respect causality
– Detect conflicting
writes
– Not rely on wall time
• We’re almost there!
Relax
• It’s fairly simple
in practice
• First, the schema
Column Type Key Null
site_id binary(16)
event_time timestamp
event_type enum(…)
payload mediumblob
Relax
• It’s fairly simple
in practice
• First, the schema
– Don’t key on
timestamp
Column Type Key Null
site_id binary(16)
event_time timestamp
event_type enum(…)
payload mediumblob
Relax
• It’s fairly simple
in practice
• First, the schema
– Don’t key on
timestamp
– Add explicit
version
Column Type Key Null
site_id binary(16)
version mediumint
event_time timestamp
event_type enum(…)
payload mediumblob
Finishing touches
• On writes:
MySQL
Server
Client
C1-Cn
Finishing touches
• On writes:
– Read latest version V0
MySQL
Server
Client
C1-Cn
V0
Finishing touches
• On writes:
– Read latest version V0
– Generate events V1...Vn
– Write atomically
MySQL
Server
Client
C1-Cn
V1-Vn
Finishing touches
• On writes:
– Read latest version V0
– Generate events V1...Vn
– Write atomically
• Success!
– Return new version
MySQL
Server
Client
C1-Cn
V1-Vn
Vn
Finishing touches
• On writes:
– Read latest version V0
– Generate events V1...Vn
– Write atomically
• Duplicate primary key?
– Optimistic lock failure!
– Retry, propagate, resolve
MySQL
Server
Client
C1-Cn
V1-Vn
Error
PK violation
GREAT
SUCCESS!
KFIR ‘ADI’ BLOCH
A round of applause
for our special guest:
... and for the terrific
camera work I butchered:
VAIDAS PILKAUSKAS
QUESTIONS?
Thank you for listening
tomer@tomergabel.com
@tomerg
http://engineering.wix.com
On GitHub:
https://github.com/holograph
This work is licensed under a Creative
Commons Attribution-ShareAlike 4.0
International License.
1 of 53

Recommended

09-time+synch.ppt by
09-time+synch.ppt09-time+synch.ppt
09-time+synch.pptssuser30e869
1 view28 slides
A Brief History of Stream Processing by
A Brief History of Stream ProcessingA Brief History of Stream Processing
A Brief History of Stream ProcessingAleksandr Kuboskin, CFA
234 views66 slides
Distributed System by
Distributed SystemDistributed System
Distributed SystemPraveen Penumathsa
3.3K views124 slides
Physical and Logical Clocks by
Physical and Logical ClocksPhysical and Logical Clocks
Physical and Logical ClocksDilum Bandara
9.6K views35 slides
Time in distributed systmes by
Time in distributed systmesTime in distributed systmes
Time in distributed systmesmohammad amid abbasi
124 views41 slides
Clock.pdf by
Clock.pdfClock.pdf
Clock.pdfMohdAbdulHaque
9 views34 slides

More Related Content

Similar to How shit works: Time

Chapter 6-Synchronozation2.ppt by
Chapter 6-Synchronozation2.pptChapter 6-Synchronozation2.ppt
Chapter 6-Synchronozation2.pptMeymunaMohammed1
4 views36 slides
Time and ordering in streaming distributed systems by
Time and ordering in streaming distributed systemsTime and ordering in streaming distributed systems
Time and ordering in streaming distributed systemsZhenzhong Xu
799 views64 slides
Puniani, Arjan Singh | Candidate Time-Delayed Decryption Protocols for Deploy... by
Puniani, Arjan Singh | Candidate Time-Delayed Decryption Protocols for Deploy...Puniani, Arjan Singh | Candidate Time-Delayed Decryption Protocols for Deploy...
Puniani, Arjan Singh | Candidate Time-Delayed Decryption Protocols for Deploy...Arjan
1K views20 slides
Lifting the hood on spark streaming - StampedeCon 2015 by
Lifting the hood on spark streaming - StampedeCon 2015Lifting the hood on spark streaming - StampedeCon 2015
Lifting the hood on spark streaming - StampedeCon 2015StampedeCon
1.4K views53 slides
Chapter 10 by
Chapter 10Chapter 10
Chapter 10AbDul ThaYyal
4.1K views20 slides
The maths behind microscaling by
The maths behind microscalingThe maths behind microscaling
The maths behind microscalingLiz Rice
538 views29 slides

Similar to How shit works: Time(20)

Time and ordering in streaming distributed systems by Zhenzhong Xu
Time and ordering in streaming distributed systemsTime and ordering in streaming distributed systems
Time and ordering in streaming distributed systems
Zhenzhong Xu799 views
Puniani, Arjan Singh | Candidate Time-Delayed Decryption Protocols for Deploy... by Arjan
Puniani, Arjan Singh | Candidate Time-Delayed Decryption Protocols for Deploy...Puniani, Arjan Singh | Candidate Time-Delayed Decryption Protocols for Deploy...
Puniani, Arjan Singh | Candidate Time-Delayed Decryption Protocols for Deploy...
Arjan 1K views
Lifting the hood on spark streaming - StampedeCon 2015 by StampedeCon
Lifting the hood on spark streaming - StampedeCon 2015Lifting the hood on spark streaming - StampedeCon 2015
Lifting the hood on spark streaming - StampedeCon 2015
StampedeCon1.4K views
The maths behind microscaling by Liz Rice
The maths behind microscalingThe maths behind microscaling
The maths behind microscaling
Liz Rice538 views
Corbett osdi12 slides (1) by Aksh54
Corbett osdi12 slides (1)Corbett osdi12 slides (1)
Corbett osdi12 slides (1)
Aksh5431 views
Storm: a distributed ,fault tolerant ,real time computation by Nitin Guleria
Storm: a distributed ,fault tolerant ,real time computationStorm: a distributed ,fault tolerant ,real time computation
Storm: a distributed ,fault tolerant ,real time computation
Nitin Guleria1.3K views
Actors for Behavioural Simulation by ClarkTony
Actors for Behavioural SimulationActors for Behavioural Simulation
Actors for Behavioural Simulation
ClarkTony223 views
Eventually, time will kill your data pipeline by Lars Albertsson
Eventually, time will kill your data pipelineEventually, time will kill your data pipeline
Eventually, time will kill your data pipeline
Lars Albertsson936 views
ICML 2016 DCCB - Distributed Clustering of Linear Bandits in Peer to Peer Net... by Shuai Li
ICML 2016 DCCB - Distributed Clustering of Linear Bandits in Peer to Peer Net...ICML 2016 DCCB - Distributed Clustering of Linear Bandits in Peer to Peer Net...
ICML 2016 DCCB - Distributed Clustering of Linear Bandits in Peer to Peer Net...
Shuai Li226 views
Principles in Data Stream Processing | Matthias J Sax, Confluent by HostedbyConfluent
Principles in Data Stream Processing | Matthias J Sax, ConfluentPrinciples in Data Stream Processing | Matthias J Sax, Confluent
Principles in Data Stream Processing | Matthias J Sax, Confluent
HostedbyConfluent628 views
Functional Programming with Immutable Data Structures by elliando dias
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structures
elliando dias4.8K views
Computer network (8) by NYversity
Computer network (8)Computer network (8)
Computer network (8)
NYversity422 views

More from Tomer Gabel

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
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
An Abridged Guide to Event Sourcing by
An Abridged Guide to Event SourcingAn Abridged Guide to Event Sourcing
An Abridged Guide to Event SourcingTomer Gabel
1K views32 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

More from Tomer Gabel(20)

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
Slaying Sacred Cows: Deconstructing Dependency Injection by Tomer Gabel
Slaying Sacred Cows: Deconstructing Dependency InjectionSlaying Sacred Cows: Deconstructing Dependency Injection
Slaying Sacred Cows: Deconstructing Dependency Injection
Tomer Gabel1.3K views
An Abridged Guide to Event Sourcing by Tomer Gabel
An Abridged Guide to Event SourcingAn Abridged Guide to Event Sourcing
An Abridged Guide to Event Sourcing
Tomer Gabel1K 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
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
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

Recently uploaded

Saikat Chakraborty Java Oracle Certificate.pdf by
Saikat Chakraborty Java Oracle Certificate.pdfSaikat Chakraborty Java Oracle Certificate.pdf
Saikat Chakraborty Java Oracle Certificate.pdfSaikatChakraborty787148
15 views1 slide
zincalume water storage tank design.pdf by
zincalume water storage tank design.pdfzincalume water storage tank design.pdf
zincalume water storage tank design.pdf3D LABS
5 views1 slide
Searching in Data Structure by
Searching in Data StructureSearching in Data Structure
Searching in Data Structureraghavbirla63
7 views8 slides
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L... by
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...Anowar Hossain
13 views34 slides
Pull down shoulder press final report docx (1).pdf by
Pull down shoulder press final report docx (1).pdfPull down shoulder press final report docx (1).pdf
Pull down shoulder press final report docx (1).pdfComsat Universal Islamabad Wah Campus
13 views25 slides
NEW SUPPLIERS SUPPLIES (copie).pdf by
NEW SUPPLIERS SUPPLIES (copie).pdfNEW SUPPLIERS SUPPLIES (copie).pdf
NEW SUPPLIERS SUPPLIES (copie).pdfgeorgesradjou
15 views30 slides

Recently uploaded(20)

zincalume water storage tank design.pdf by 3D LABS
zincalume water storage tank design.pdfzincalume water storage tank design.pdf
zincalume water storage tank design.pdf
3D LABS5 views
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L... by Anowar Hossain
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
DevOps to DevSecOps: Enhancing Software Security Throughout The Development L...
Anowar Hossain13 views
NEW SUPPLIERS SUPPLIES (copie).pdf by georgesradjou
NEW SUPPLIERS SUPPLIES (copie).pdfNEW SUPPLIERS SUPPLIES (copie).pdf
NEW SUPPLIERS SUPPLIES (copie).pdf
georgesradjou15 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...
AltinKaradagli6 views
SUMIT SQL PROJECT SUPERSTORE 1.pptx by Sumit Jadhav
SUMIT SQL PROJECT SUPERSTORE 1.pptxSUMIT SQL PROJECT SUPERSTORE 1.pptx
SUMIT SQL PROJECT SUPERSTORE 1.pptx
Sumit Jadhav 13 views
_MAKRIADI-FOTEINI_diploma thesis.pptx by fotinimakriadi
_MAKRIADI-FOTEINI_diploma thesis.pptx_MAKRIADI-FOTEINI_diploma thesis.pptx
_MAKRIADI-FOTEINI_diploma thesis.pptx
fotinimakriadi8 views
Instrumentation & Control Lab Manual.pdf by NTU Faisalabad
Instrumentation & Control Lab Manual.pdfInstrumentation & Control Lab Manual.pdf
Instrumentation & Control Lab Manual.pdf
NTU Faisalabad 5 views
Machine Element II Course outline.pdf by odatadese1
Machine Element II Course outline.pdfMachine Element II Course outline.pdf
Machine Element II Course outline.pdf
odatadese19 views
Update 42 models(Diode/General ) in SPICE PARK(DEC2023) by Tsuyoshi Horigome
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Update 42 models(Diode/General ) in SPICE PARK(DEC2023)
Generative AI Models & Their Applications by SN
Generative AI Models & Their ApplicationsGenerative AI Models & Their Applications
Generative AI Models & Their Applications
SN8 views
MSA Website Slideshow (16).pdf by msaucla
MSA Website Slideshow (16).pdfMSA Website Slideshow (16).pdf
MSA Website Slideshow (16).pdf
msaucla68 views
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

How shit works: Time

  • 3. He has a dream.
  • 4. Adi is fashionable • Event sourcing is the future, they say • Event sourcing is the shit, he agrees
  • 5. Adi is fashionable • Event sourcing is the future, they say • Event sourcing is the shit, he agrees • … and designs an event model Created Modified Published Archived Restored
  • 6. Column Type Key Null site_id binary(16) event_time timestamp event_type enum(…) payload mediumblob Adi is fashionable
  • 7. HEY, KIDS Can you guess what happens next?
  • 8. Adi is confused • Bad shit happens • Event streams exhibit: – Out of order events – Conflicting events – Impossible states
  • 9. How shit works: Time Tomer Gabel Wix Engineering Conference May 2017 Image: Vera Kratochvil (public domain)
  • 10. Time (noun) “… the system of those sequential relations that any event has to any other, as past, present, or future; indefinite and continuous duration regarded as that in which events succeed one another.” -- dictionary.com
  • 11. Time (noun) “… the system of those sequential relations that any event has to any other, as past, present, or future; indefinite and continuous duration regarded as that in which events succeed one another.” -- dictionary.com
  • 12. Modeling time • Encoding – Resolution – Epoch “Real” time Epoch Resolution T1 T2 T3 … Tn
  • 13. Modeling time • Encoding – Resolution – Epoch T3 Instant (or “event”)
  • 14. Modeling time • Encoding – Resolution – Epoch T3 A B Conflicting events
  • 15. Modeling time • Encoding – Resolution – Epoch • Bootstrapping – Manual – Battery-backed – NTP Epoch ???
  • 16. Modeling time • Encoding – Resolution – Epoch • Bootstrapping – Manual – Battery-backed – NTP • Updating T1 T2
  • 17. Modeling time • Encoding – Resolution – Epoch • Bootstrapping – Manual – Battery-backed – NTP • Updating T = T+1 T1 T2 W hen?
  • 18. System Clock (RTC) Modeling time • Encoding – Resolution – Epoch • Bootstrapping – Manual – Battery-backed – NTP • Updating Image: Jeremy Saglimbeni on Vimeo (CC BY-SA 3.0)
  • 19. RTC isn’t perfect • Alas, clocks drift • Subtle causes – Temperature – Power supply – General relativity – Cosmic radiation – Alien gamma rays Image showing ~220µs clock drift by Luke Bigum
  • 20. Distributed time Host A Host C Host B Host D RTC RTC RTC RTC Event Stream Event Store RTC event_time = ?
  • 21. Time source: Application Host A Host C Host B Host D RTC RTC RTC RTC Event Stream Event Store RTC 1 2 3 4
  • 22. Time source: Application Host A Host C Host B Host D RTC RTC RTC RTC Event Stream Event Store RTC 1 2 3 4 Clocks must be synchronized!
  • 23. What about NTP? Host A Host C Host B Host D RTC RTC RTC RTC Event Store RTC NTP
  • 24. What about NTP? Host A Host C Host B Host D RTC RTC RTC RTC Event Store RTC NTP Accurate within ~10ms Fig. 1, “Characterizing Quality of Time and Topology in a Time Synchronization Network”, Murta et al
  • 25. DEALING WITH TIME IS HARD TL;DR
  • 26. Reframing the problem • When reading events – Do we need wall time? – We don’t care about it – It’s just metadata • We only care about ordering events Created (2017-05-03 10:15) Updated (2017-05-03 10:27) Archived (2017-05-03 10:55)
  • 27. Reprise: Time (noun) “… the system of those sequential relations that any event has to any other, as past, present, or future; indefinite and continuous duration regarded as that in which events succeed one another.” -- dictionary.com
  • 28. Reprise: Time (noun) “… the system of those sequential relations that any event has to any other, as past, present, or future; indefinite and continuous duration regarded as that in which events succeed one another.” -- dictionary.com
  • 29. Causality • Take any two events • What relationship can they have? Created Updated Updated Archived
  • 30. Causality • Take any two events • What relationship can they have? – Happens before: A→B A. Created B. Updated Updated Archived
  • 31. Causality • Take any two events • What relationship can they have? – Happens before: A→B – Concurrent: A↛B and B↛A Created A. Updated B. Updated Archived
  • 32. Lamport timestamps • Provides partial ordering of events • Advantages: – Respects causality – Low overhead – Simple to implement Event Store Host A Host C Host B “Time, Clocks, and the Ordering of Events in a Distributed System”, Leslie Lamport, 1978
  • 33. Event Store Host A Host C Host B Lamport timestamps • Each host maintains local logical clock • Start with T=0 T=0 T=0T=0
  • 34. Event Store Host A Host C Host B Lamport timestamps • Each host maintains local logical clock • Start with T=0 • On send (i.e. write): – Increment local clock – Attach to message T=0 T=0T=0 T=1
  • 35. Lamport timestamps • On receive (i.e. read): – Process event if T < Tin Event Store Host A Host C Host B T=0 Tin=1
  • 36. Lamport timestamps • On receive (i.e. read): – Process event if T < Tin – Update clock past the latest event: T = max(T, Tin) + 1 • All done! Event Store Host A Host C Host B T=0 Tin=1 T=2
  • 37. … well, almost • This is a partial order • Causality is dealt with – A→B ⇒ T(A) < T(B) • What about concurrency? – A↛B and B↛A ⇒ ?
  • 38. Final touches • We want total ordering – Must be stable – Need not correspond to real time Created Updated Updated Archived T=1 T=5 T=2T=2
  • 39. Final touches • We want total ordering – Must be stable – Need not correspond to real time • Breaking the tie: – Use any consistent key – Host name, MAC, IP… Created Updated Updated Archived T=1 T=5 T=2T=2
  • 40. What did we gain? • We can assign versions that… – Respect causality – Are totally ordered – Don’t rely on the RTC • We’ve enabled serializability!
  • 41. MAKE ADI GREAT AGAIN! Enough theory. Let’s
  • 42. Versioning • Our versioning mechanism must: – Respect causality – Detect conflicting writes – Not rely on wall time • We’re almost there!
  • 43. Relax • It’s fairly simple in practice • First, the schema Column Type Key Null site_id binary(16) event_time timestamp event_type enum(…) payload mediumblob
  • 44. Relax • It’s fairly simple in practice • First, the schema – Don’t key on timestamp Column Type Key Null site_id binary(16) event_time timestamp event_type enum(…) payload mediumblob
  • 45. Relax • It’s fairly simple in practice • First, the schema – Don’t key on timestamp – Add explicit version Column Type Key Null site_id binary(16) version mediumint event_time timestamp event_type enum(…) payload mediumblob
  • 46. Finishing touches • On writes: MySQL Server Client C1-Cn
  • 47. Finishing touches • On writes: – Read latest version V0 MySQL Server Client C1-Cn V0
  • 48. Finishing touches • On writes: – Read latest version V0 – Generate events V1...Vn – Write atomically MySQL Server Client C1-Cn V1-Vn
  • 49. Finishing touches • On writes: – Read latest version V0 – Generate events V1...Vn – Write atomically • Success! – Return new version MySQL Server Client C1-Cn V1-Vn Vn
  • 50. Finishing touches • On writes: – Read latest version V0 – Generate events V1...Vn – Write atomically • Duplicate primary key? – Optimistic lock failure! – Retry, propagate, resolve MySQL Server Client C1-Cn V1-Vn Error PK violation
  • 52. KFIR ‘ADI’ BLOCH A round of applause for our special guest: ... and for the terrific camera work I butchered: VAIDAS PILKAUSKAS
  • 53. QUESTIONS? Thank you for listening tomer@tomergabel.com @tomerg http://engineering.wix.com On GitHub: https://github.com/holograph This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.