CRDT CONFLICTFREE REPLICATED DATA TYPES
Distributed Systems (HansArno Jacobsen) 1
Pixabay.com
CRDTs Units
Eventual consistency, informally
Statebased objects
Eventual consistency, more formally Conflictfree replicated data types
Distributed Systems (HansArno Jacobsen) 2
EVENTUAL CONSISTENCY, INFORMALLY
Distributed Systems (HansArno Jacobsen)
3
Pixabay.com
Eventual Consistency
Eventualconsistencyisdesirableforlargescale distributed systems where high availability is important
Tendstobecheaptoimplement(e.g.,viagossip)but may serve stale data
Constitutesachallengeforenvironmentswhere stronger consistency is important
Distributed Systems (HansArno Jacobsen) 4
Handling Concurrent Writes
Premise for eventual consistency were scenarios with few (no) concurrent writes to the same key (cf. clientcentric consistency)
However, we do need a mechanism to handle concurrent writes should they so happen
If there were a way to handle concurrent writes, we could support eventual consistency more broadly
Would only need to guarantee that after processing all writes for a key, all replicas converge, no matter what order the writes are processed (e.g., assuming gossip)
Distributed Systems (HansArno Jacobsen) 5
Max register L1: 0 W(4) (4) W(2) (4)
Examples
Growthonly counter (Gcounter)
L1: 0 W(+5) (5) W(+2) (7) W(+1) 8
L2: 0 W(+2) (2) W(+5) (7) W(+1) 8 Writes propagate to L2, L1, respectively
Different locations (replicas)
merge(5) 5 L2: 0 W(5) (5) W(3) (5) merge(4)
State propagate to L2, L1 via periodic merging
Distributed Systems (HansArno Jacobsen)
6
5
Selfstudy Questions
Think of a few basic data structures, like lists, sets, counters, binary trees, heaps, maps, etc., and visualize for yourself what happens if replicated instances of these structures are updated via gossip.
Does their state converge, no matter the update sequence?
What happens if update operations are lost or duplicated?
What mechanisms we know other than gossip could be used to keep these replicated structures updated without violating their convergence.
What are pros and cons of these mechanisms?
Distributed Systems (HansArno Jacobsen) 7
Distributed Systems (HansArno Jacobsen) 8
CRDT FROM STATEBASED OBJECTS TO REPLICATED STATEBASED OBJECTS
Distributed Systems (HansArno Jacobsen)
9
Pixabay.com
Statebased objects Mostly plain old objects
Offerupdateandqueryrequeststoclients
Maintaininternalstate
Processclientrequests
Perform merge requests amongst each other Periodicallymerge(supportinfrastructure)
Distributed Systems (HansArno Jacobsen) 10
Statebased Object
What we commonly know as object Comprised of
Internal state
One or more query methods One or more update methods A merge method
Distributed Systems (HansArno Jacobsen) 11
class Avg(object): def __init__(self):
def update(self, x): self.sum += x self.cnt += 1
self.sum = 0
self.cnt = 0
def query(self): if self.cnt != 0:
def merge(self, avg): self.sum += avg.sum self.cnt += avg.cnt
return
else: return 0
self.sum /
self.cnt
Class Average Running Example
Distributed Systems (HansArno Jacobsen) 12
Average
Statebased object representing a running average
Internalstate
self.sum and self.cnt
Query returns average
Update updates average with a new value x
Merge merges one Avg instance into another one
Distributed Systems (HansArno Jacobsen) 13
Replicated Statebased Object
Statebased object replicated across multiple nodes
E.g., replicate Avg across two nodes
Both nodes have a copy of statebased object
Clients send query and update to a single node
Nodes periodically send their copy of statebased object to other nodes for merging
Distributed Systems (HansArno Jacobsen) 14
Node a
a0 Timeline Update
Unique
Causal history based on operation identifiers
operation identifier
Each state represents a snapshot of object in time that results from updates applied
state
query
history
State
a1
a0 sum:0, cnt:0
0
a1 sum:1, cnt:1
1 0
a2 sum:4, cnt:2 Distributed Systems (HansArno Jacobsen)
2 0,1
State
15
Operation identifier is unique across replicas
Each state represents a snapshot of object in time that results from updates applied
state
query
history
Timeline
a0 sum:0, cnt:0
0
a1 sum:1, cnt:1
1 0
a2 sum:4, cnt:2
2 0,1
Distributed Systems (HansArno Jacobsen)
16
States and Causal Histories
If y = x.update() where the update has identifier i, then the causal history of y is the causal history of x union { i }.
a0 sum:0, a1 sum:1, b0 sum:0, b1 sum:2, b2 sum:6,
cnt:0 cnt:1 cnt:0 cnt:1 cnt:2
0 1 0 2 3
{} {0} {} {1} {1,2}
Distributed Systems (HansArno Jacobsen)
17
state
query ()
history
a0 sum:0,
cnt:0 cnt:1 cnt:0 cnt:1 cnt:2
0{} 2 {0} 0{} 4 {1}
a1 sum:2,
b0 sum:0,
b1 sum:4,
update 2 update 4
state
query () history
Merge
a2 sum:6,
Distributed Systems (HansArno Jacobsen)
3 {0,1}
18
Nodes Periodically Propagate Their State
Distributed Systems (HansArno Jacobsen) 19
Selfstudy Questions
Think of a few basic data structures, like lists, sets, counters, binary trees, heaps, maps, etc., and visualize for yourself what happens if replicated instances of these structures are updated via gossip.
For the above data structures, specify merge operations that merge the state of two instances of a given structure.
Assume merge happens periodically, does your replicated structures state converge?
Distributed Systems (HansArno Jacobsen) 20
Distributed Systems (HansArno Jacobsen) 21
CRDT
EVENTUAL CONSISTENCY, MORE FORMALLY
Distributed Systems (HansArno Jacobsen)
22
Pixabay.com
Eventual Consistency (EC)
A replicated statebased object is
eventually consistent if whenever two replicas of the statebased object have the same causal history, they eventually (not necessarily immediately) converge to the same internal state
Distributed Systems (HansArno Jacobsen) 23
Strong Eventual Consistency (SEC)
A replicated statebased object is
strongly eventually consistent if whenever two replicas of the statebased object have the same causal history, they (immediately) have the same internal state
Strong eventual consistency implies eventual consistency
Distributed Systems (HansArno Jacobsen) 24
NoMergeAverage BMergeAverage MaxAverage
EC or SEC
That is the question?
Variants of our Average object, defined next Average
Note that some of these objects do not represent realistic functionality (i.e., needed functionality)
These objects are meant to illustrate convergence concepts only
Distributed Systems (HansArno Jacobsen) 25
Average
a, b attain the same causal history but do not converge to the same internal state they do not converge at all!
state
query history
Neither eventually
b0 sum:0, cnt:0 b1 sum:4, cnt:1
0
consistent, nor
4