-
Notifications
You must be signed in to change notification settings - Fork 1
/
3_crdt.html
920 lines (787 loc) · 32.7 KB
/
3_crdt.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
<!--The design and styling of this site was taken from http://redbook.io-->
<!DOCTYPE html>
<html>
<head>
<title>Consistency in Distributed Systems</title>
<link href='css/style.css' rel='stylesheet'>
<link href='css/3_crdt.css' rel='stylesheet'>
<link href="css/default.css" rel="stylesheet">
<link href="css/svg.css" rel="stylesheet">
<meta name=viewport content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="favicon.ico">
</head>
<body>
<div id="header">
<div id="grouptitle">
<a href="index.html">Consistency in Distributed Systems</a>
</div>
</div>
<div id="container">
<article>
<h1 id="conflict_free_replicated_data_types" class="lecturetitle">
<a href="https://scholar.google.com/scholar?cluster=4496511741683930603">
Conflict-Free Replicated Data Types
</a>
</h1>
<p>
In this lecture, we'll define a particular class of replicated data
structures known as state-based objects and formalize what it means for
them to be eventually consistent and strongly eventually consistent.
We'll then survey conflict-free replicated data types (CRDTs): a specific
type of state-based object that is guaranteed to achieve strong eventual
consistency.
</p>
<h2 id="state_based_objects">State-Based Objects</h2>
<p>
In the first lecture, we looked at replicated <em>key-value stores</em>.
In the second lecture, we looked at replicated <em>registers</em>.
Key-value stores and registers are two specific examples of data
structures that we can replicate. In this section, we'll generalize
things and look at arbitrary replicated data structures known as
state-based objects. We'll then introduce a way to visualize the state of
a replicated state-based object over time.
</p>
<p>
A <strong>state-based object</strong> is an ordinary, plain Jane
object—like the objects you've likely seen in Python or
Java—with
(1) some <strong>internal state</strong>,
(2) a <strong>query</strong> method,
(3) some number of <strong>update</strong> methods, and
(4) a <strong>merge</strong> method.
Understanding state-based objects is easiest with an example. Here's one
written in Python:
</p>
<pre><code class="python">class Average(object):
def __init__(self):
self.sum = 0
self.cnt = 0
def query(self):
if self.cnt != 0:
return self.sum / self.cnt
else:
return 0
def update(self, x):
self.sum += x
self.cnt += 1
def merge(self, avg):
self.sum += avg.sum
self.cnt += avg.cnt</code></pre>
<p>
<code>Average</code> is a state-based object representing a running
average. Note the following:
</p>
<ul>
<li>
<code>Average</code>'s <em>internal state</em> are the two variables
<code>self.sum</code> and <code>self.cnt</code> representing the sum
and count of all the values being averaged.
</li>
<li>
<code>Average</code>'s <em>query</em> method returns the average. Note
that <code>Average</code>'s <code>query</code> method does not modify
any internal state. This is true for every state-based object.
</li>
<li>
<code>Average</code>'s single <em>update</em> method updates the
average with a new value <code>x</code>. In general, a state-based
object can have multiple update methods.
</li>
<li>
<code>Average</code>'s <code>merge</code> method merges one
<code>Average</code> instance into another.
</li>
</ul>
<p>
A <strong>replicated state-based object</strong> is simply a
state-based object that is replicated across multiple computers. For
example, if we replicate an <code>Average</code> state-based object
across two servers <code>a</code> and <code>b</code> (as we do below),
then both <code>a</code> and <code>b</code> have a copy of the
state-based object. Clients (e.g. <code>c</code> and <code>d</code>
below) send <code>query</code> and <code>update</code> requests to a
server's copy of the state-based object. Meanwhile, every server
periodically sends its copy of the state-based object to other servers to
be merged with the <code>merge</code> method. That looks something like
this:
</p>
<div class="svgbox">
<svg viewBox="0 0 400 350" id="chaotic_replication"></svg>
</div>
<p>
Animated diagrams like these help us visualize how clients and servers
communicate in a distributed system, but they make it difficult to keep
track of the replicated state-based object as it is queried, updated, and
merged over time. To more easily visualize the replicated state-based
object, we'll introduce a new type of diagram. Here's an example:
</p>
<div class="svgbox">
<svg viewBox="0 0 400 50" id="trivial_update_svg"></svg>
</div>
<center>
<table id="trivial_update_table" class="srtable"></table>
</center>
<p>
In these diagrams, snapshots of a state-based object are drawn from left
to right forwards through time. Here, we consider an <code>Average</code>
state-based object on a single server <code>a</code>. The initial
snapshot of the state-based object is labelled <code>a0</code>. Server
<code>a</code> receives an <code>update(1)</code> request from some
client which updates the state-based object to the next snapshot
<code>a1</code>. Server <code>a</code> then receives an
<code>update(3)</code> request which updates the state-based object to
the snapshot <code>a2</code>.
</p>
<p>
Each update request is assigned a unique identifier which is drawn below
the request. For example, the <code>update(1)</code> request is assigned
an identifier of <code>0</code>, and the <code>update(3)</code> request
is assigned an identifier of <code>1</code>.
</p>
<p>
Beneath the diagram, we draw a table with one row for each snapshot of
the state-based object.
</p>
<ul>
<li>
The <em>first</em> column is the internal state of the object. For
example, <code>a0</code> has a sum of <code>0</code> and a count of
<code>0</code> (as specified by <code>Average</code>'s constructor).
Similarly, <code>a1</code> has a sum of <code>1</code> and a count of
<code>1</code>.
</li>
<li>
The <em>second</em> column is the result of calling the snapshot's
<code>query()</code> method. For example, <code>a1.query()</code>
returns <code>1</code> because <code>a1.sum / a1.cnt</code> is equal to
<code>1</code>. Similarly, <code>a2.query()</code> returns
<code>2</code> because <code>a2.sum / a2.cnt</code> is equal to
<code>2</code>.
</li>
<li>
<p>
The <em>third</em> (and most complex) column is the <strong>causal
history</strong> of the snapshot: the set of identifiers of every
request that has contributed to the snapshot's internal state.
</p>
<p>
For example, <code>a0</code> hasn't
been affected by any requests, so its causal history is the empty set
<code>{}</code>. <code>a1</code> has been affected by the
<code>update(1)</code> request (id <code>0</code>), so
<code>a1</code>'s causal history is the set <code>{0}</code>. Finally,
<code>a2</code> has been affected by the <code>update(1)</code> request
(id <code>0</code>) <em>and</em> the <code>update(3)</code> request (id
<code>1</code>), so <code>a2</code>'s causal history is the set
<code>{0,1}</code>.
</p>
<p>
In general if <code>y = x.update()</code> where the update has
identifier <code>i</code>, then the causal history of <code>y</code>
is the causal history of <code>x</code> unioned with the singleton
set <code>{i}</code>.
</p>
</li>
</ul>
<p>
Here's an example diagram which traces a state-based object replicated on
two servers: <code>a</code> and <code>b</code>. Notice how the causal
history of <code>a1</code> is disjoint from the casual histories of
<code>b1</code> and <code>b2</code>. The <code>update(2)</code> and
<code>update(4)</code> requests have not contributed to the state of
<code>a1</code>, so their identifiers do not appear in its causal
history. Conversely, the <code>update(1)</code> request has not
contributed to the state of either <code>b1</code> or <code>b2</code>, so
it does not appear in either of their causal histories.
</p>
<div class="svgbox">
<svg viewBox="0 0 400 100" id="simple_update_svg"></svg>
</div>
<center>
<table id="simple_update_table" class="srtable"></table>
</center>
<p>
We can also use these diagrams to visualize <code>merge</code> requests.
In the diagram below, server <code>a</code> receives an
<code>update(2)</code> request which transitions <code>a0</code> to
<code>a1</code>. Similarly, server <code>b</code> receives an
<code>update(4)</code> request which transitions <code>b0</code> to
<code>b1</code>. Then, server <code>b</code> sends a copy of
<code>b1</code> to server <code>a</code>. When server <code>a</code>
receives the copy of <code>b1</code>, it merges <code>b1</code> into
<code>a1</code> which creates <code>a2</code>.
</p>
<div class="svgbox">
<svg viewBox="0 0 400 120" id="simple_merge_svg"></svg>
</div>
<center>
<table id="simple_merge_table" class="srtable"></table>
</center>
<p>
Also notice that the causal history of <code>a2</code> (i.e.
<code>{0,1}</code>) is the union of <code>a1</code>'s causal history
(i.e. <code>{0}</code>) and <code>b1</code>'s causal history (i.e.
<code>{1}</code>). This makes intuitive sense because the both the
<code>update(2)</code> request (id <code>0</code>) and the
<code>update(4)</code> request (id <code>1</code>) contribute to the
state of <code>a2</code>. In general, if <code>z = x.merge(y)</code>,
the causal history of <code>z</code> is the union of the causal histories
of <code>x</code> and <code>y</code>.
</p>
<p>
Finally, recall that a server periodically sends its replica of the
state-based object to other servers to be merged. Thus, our diagrams will
look something like the following, where servers continually merge with
one another.
</p>
<div class="svgbox">
<svg viewBox="0 0 400 120" id="converge_svg"></svg>
</div>
<div hidden>
<table id="converge_table" class="srtable"></table>
</div>
<h2 id="strong_eventual_consistency">(Strong) Eventual Consistency</h2>
<p>
Equipped with an understanding of replicated state-based objects and
causal history, we're ready to define eventual consistency and strong
eventual consistency! In this section, we'll also bolster our grasp on
the two definitions by looking at four state-based
objects—<code>Average</code>, <code>NoMergeAverage</code>,
<code>BMergeAverage</code>, and <code>SECAverage</code>—and decide
whether they are eventually consistent, strongly eventually consistent,
neither, or both.
</p>
<p>
We say a replicated state-based object is <strong>eventually
consistent</strong> (EC) if whenever two replicas of the state-based
object have the same causal history, the <em>eventually</em> (not
necessarily immediately) converge to the same internal state.
We say a replicated state-based object is <strong>strongly eventually
consistent</strong> (SEC) if whenever two snapshots of the state-based
object have the same causal history, they (immediately) have the same
internal state.
Note that strong eventual consistency implies eventual consistency.
</p>
<h3 id="average"><code>Average</code></h3>
<p>
The definitions of eventual consistency and strong eventual consistency
are best understood with examples. First, let's consider the
<code>Average</code> state-based object we defined above and ask
ourselves whether or not it is eventually consistent or strongly
eventually consistent. Consider the following execution (note that we've
abbreviated <code>update</code> as <code>u</code> and <code>merge</code>
as <code>m</code>).
</p>
<div class="svgbox">
<svg viewBox="0 0 400 120" id="diverge_svg"></svg>
</div>
<center>
<table id="diverge_table" class="srtable"></table>
</center>
<p>
Snapshots <code>a2</code>, <code>b2</code>, <code>a3</code>, and
<code>b3</code> (highlighted red) all have the same causal history
<code>{0,1}</code>. This means that <code>a</code>'s replica and
<code>b</code>'s replica have the same causal history. However, despite
having the same causal history, the two replicas do not converge to the
same internal state. In fact, they don't converge at all! As
<code>a</code> and <code>b</code> periodically send merge requests to one
another, the internal state of their replicas continues to change.
Neither replica converges on a single value. <strong>Thus,
<code>Average</code> is neither eventually consistent nor strongly
eventually consistent.</strong>
</p>
<h3 id="no_merge_average"><code>NoMergeAverage</code></h3>
<p>
Next, let's look at the following <code>NoMergeAverage</code>
state-based object which is exactly the same as <code>Average</code>
except that its <code>merge</code> method does nothing at all.
</p>
<pre><code class="python">class NoMergeAverage(Average):
# __init__, query, and merge
# inherited from Average.
def merge(self, avg):
# Ignore merge requests!
pass</code></pre>
<p>
Unlike <code>Average</code>, <code>NoMergeAverage</code> is convergent.
However (like <code>Average</code>), <strong><code>NoMergeAverage</code>
is neither eventually consistent nor strongly eventually
consistent.</strong> To see why, let's take a look at the following
execution:
</p>
<div class="svgbox">
<svg viewBox="0 0 400 120" id="no_merge_average_svg"></svg>
</div>
<center>
<table id="no_merge_average_table" class="srtable"></table>
</center>
<p>
Snapshots <code>a2</code>, <code>b2</code>, <code>a3</code>, and
<code>b3</code> (highlighted red) all have the same causal history.
Moreover, both <code>a</code>'s replica and <code>b</code>'s replica of
the state-based object converge. However, <code>a</code>'s replica
converges to an internal state <code>{sum=2,cnt=1}</code> while
<code>b</code>'s replica converges to an internal state
<code>{sum=4,cnt=1}</code>. Because the two replicas converge to
<em>different</em> internal states, <code>NoMergeAverage</code> is
neither eventually consistent nor strongly eventually consistent.
</p>
<h3 id="b_merge_average"><code>BMergeAverage</code></h3>
<p>
Next, let's look at the <code>BMergeAverage</code> state-based object
which is exactly the same as <code>Average</code> except for the
<code>merge</code> function. When server <code>b</code> receives a merge
request from server <code>a</code>, it throws away its replica of the
state-based object and overwrites it with <code>a</code>'s replica. On
the other hand, when server <code>a</code> receives a merge request from
server <code>b</code>, it ignores it.
</p>
<pre><code class="python">class BMergeAverage(Average):
# __init__, query, and merge
# inherited from Average.
def merge(self, avg):
if on_server_b():
self.sum = avg.sum
self.cnt = avg.cnt
else:
# Server a ignores
# merge requests!</code></pre>
<p>
<strong><code>BMergeAverage</code> is eventually consistent, but not
strongly eventually consistent.</strong> To see why, consider the
following execution.
</p>
<div class="svgbox">
<svg viewBox="0 0 400 120" id="b_merge_average_svg"></svg>
</div>
<center>
<table id="b_merge_average_table" class="srtable"></table>
</center>
<p>
Snapshots <code>b1</code>, <code>a1</code>, <code>b2</code>, and
<code>a2</code> (highlighted red) all have the same causal history.
Moreover, both <code>a</code>'s replica and <code>b</code>'s replica
eventually converge to the same internal state <code>{sum=0,cnt=0}</code>.
In fact, it's not difficult to convince yourself that this will happen in
every possible execution involving a <code>BMergeAverage</code>
state-based object. <code>a</code>'s replica and <code>b</code>'s replica
will always eventually converge to the same internal state! Thus,
<code>BMergeAverage</code> is eventually consistent.
</p>
<p>
However, let's take a closer look at snapshot <code>b1</code> and
<code>a1</code>. Both snapshots have the same causal history
<code>{0}</code>, but the two snapshots have different internal states.
<code>b1</code>'s internal state is <code>{sum=4,cnt=1}</code>, while
<code>a1</code>'s internal state is <code>{sum=0,cnt=0}</code>. Thus,
<code>BMergeAverage</code> is not strongly eventually consistent.
</p>
<h3 id="max_average"><code>MaxAverage</code></h3>
<p>
Finally, let's look at the <code>MaxAverage</code> state-based object
which is exactly the same as <code>Average</code> except that the
<code>merge</code> function now performs a pairwise maximum of
<code>sum</code> and <code>cnt</code>.
</p>
<pre><code class="python">class MaxAverage(Average):
# __init__, query, and merge
# inherited from Average.
def merge(self, avg):
self.sum = max(self.sum, avg.sum)
self.cnt = max(self.cnt, avg.cnt)</code></pre>
<p>
<strong><code>MaxAverage</code> is both eventually consistent and
strongly eventually consistent!</strong> To see why, consider the
following execution.
</p>
<div class="svgbox">
<svg viewBox="0 0 400 120" id="max_average_svg"></svg>
</div>
<center>
<table id="max_average_table" class="srtable"></table>
</center>
<p>
Snapshots <code>a2</code>, <code>b2</code>, <code>a3</code>, and
<code>b3</code> (highlighted red) all have the same causal history.
Better yet, they all have the same internal state too! This is no
coincidence. For <em>every</em> execution involving a replicated
<code>MaxAverage</code> state-based object, if two snapshots have the
same causal history, they are guaranteed to have the same internal state.
Thus, <code>MaxAverage</code> is strongly eventually consistent (and
therefore eventually consistent too).
</p>
<p>
Unfortunately, even though <code>MaxAverage</code> is strongly eventually
consistent, its semantics are a bit weird. For example, in the execution
above, the effects of the <code>update(2)</code> request were completely
overwritten by the <code>update(4)</code> request!
</p>
<h3 id="summary">Summary</h3>
<p>
In the following table, we summarize the consistency of the four
state-based objects we've covered. C stands for convergent, EC stands for
eventually consistent, and SEC stands for strongly eventually consistent.
</p>
<center>
<table id="average_summary">
<tr>
<th></th>
<th>C?</th>
<th>EC?</th>
<th>SEC?</th>
</tr>
<tr>
<th><code>Average</code></th>
<td class="no">no</td>
<td class="no">no</td>
<td class="no">no</td>
</tr>
<tr>
<th><code>NoMergeAverage</code></th>
<td class="yes">yes</td>
<td class="no">no</td>
<td class="no">no</td>
</tr>
<tr>
<th>BMergeAverage</th>
<td class="yes">yes</td>
<td class="yes">yes</td>
<td class="no">no</td>
</tr>
<tr>
<th>MaxAverage</th>
<td class="yes">yes</td>
<td class="yes">yes</td>
<td class="yes">yes</td>
</tr>
</table>
</center>
<h2 id="crdts">CRDTs</h2>
<p>
The last section demonstrated that designing a strongly eventually
consistent state-based object with intuitive semantics is challenging.
In this section, we define a particular class of state-based objects
known as conflict-free replicated data types (CRDTs) which are guaranteed
to be strongly eventually consistent and which usually have reasonable
semantics. We'll then present four CRDTs: GCounters, PN-Counters, G-Sets
and 2P-Sets.
</p>
<p>
A <strong>conflict-free replicated datatype</strong> (CRDT) is a
state-based object that satisfies the following properties. Let
<code>merge(x, y)</code> be the value of <code>x</code> after performing
<code>x.merge(y)</code>. Similarly, let <code>update(x, ...)</code> the
value of <code>x</code> after performing <code>x.update(...)</code>.
</p>
<ul>
<li>
The <code>merge</code> method is <strong>associative</strong>. This
means that for any three state-based objects <code>x</code>,
<code>y</code>, and <code>z</code>, <code>merge(merge(x, y), z)</code>
is equal to <code>merge(x, merge(y, z))</code>.
</li>
<li>
The <code>merge</code> method is <strong>commutative</strong>. This
means that for any two state-based objects, <code>x</code> and
<code>y</code>, <code>merge(x, y)</code> is equal to <code>merge(y,
x)</code>.
</li>
<li>
The <code>merge</code> method is <strong>idempotent</strong>. This
means that for any state-based object <code>x</code>, <code>merge(x,
x)</code> is equal to <code>x</code>.
</li>
<li>
Every update method is <strong>increasing</strong>. Let <code>x</code>
be an arbitrary state-based object and let <code>y = update(x,
...)</code> be the result of applying an arbitrary update
<code>update</code> to <code>x</code>. The <code>update</code> method
is increasing if <code>merge(x, y)</code> is equal to <code>y</code>.
</li>
</ul>
<p>
To make things concrete, let's look at a very simple state-based object
and prove it is a CRDT. The following <code>IntMax</code> state-based
object wraps a single integer <code>x</code> where <code>merge(a,
b)</code> computes the maximum of <code>a.x</code> and <code>b.x</code>.
</p>
<pre><code class="python">class IntMax(object):
def __init__(self):
self.x = 0
def query(self):
return self.x
def update(self, x):
assert x >= 0
self.x += x
def merge(self, other):
self.x = max(self.x, other.x)</code></pre>
<p>
We can easily verify that <code>IntMax</code> is a CRDT by proving that
(a) <code>merge</code> is <em>associative</em>, <em>commutative</em>, and
<em>idempotent</em> and that (b) <code>update</code> is
<em>increasing</em>. The proofs are straightforward because the
<code>max</code> function itself is associative, commutative, and
idempotent! Note that we'll abuse notation a bit and say an
<code>IntMax</code> object <code>a</code> is equal to the integer
<code>a.x</code> it contains.
</p>
<ul>
<li>
First, we prove that <code>merge</code> is <em>associative</em>.<br/>
<pre><code> merge(merge(a, b), c)
= max(max(a.x, b.x), c.x)
= max(a.x, max(b.x, c.x))
= merge(a, merge(b, c))</code></pre>
</li>
<li>
Next, we prove that <code>merge</code> is <em>commutative</em>.<br/>
<pre><code> merge(a, b)
= max(a.x, b.x)
= max(b.x, a.x)
= merge(b, a)</code></pre>
</li>
<li>
Then, we prove that <code>merge</code> is <em>idempotent</em>.<br/>
<pre><code> merge(a, a)
= max(a.x, a.x)
= a.x
= a</code></pre>
</li>
<li>
Finally, we prove that <code>update</code> is <em>increasing</em>.<br/>
<pre><code> merge(a, update(a, x))
= max(a.x, a.x + x)
= a.x + x
= update(a, x)</code></pre>
</li>
</ul>
<p>
We'll now turn our attention to four more complex CRDTS:
<em>G-Counters</em> (increment only counters);
<em>PN-Counters</em> (increment/decrement counters);
<em>G-Sets</em> (add only sets); and
<em>2P-Sets</em> (add/remove sets).
</p>
<h3 id="g_counter">G-Counter</h3>
<p>
A <strong>G-Counter</strong> CRDT represents a replicated counter which
can be added to but not subtracted from.
</p>
<ul>
<li>
The <em>internal state</em> of a G-Counter replicated on n machines is
n-length array of non-negative integers.
</li>
<li>
The <code>query</code> method returns the sum of every element in the
n-length array.
</li>
<li>
The <code>add(x)</code> update method, when invoked on the ith server,
increments the ith entry of the n-length array by <code>x</code>. For
example, server 0 will increment the 0th entry of the array, server 1
will increment the 1st entry of the array, and so on.
</li>
<li>
The <code>merge</code> method performs a pairwise maximum of the two
arrays.
</li>
</ul>
<p>
We can implement a G-Counter in Python as follows.
</p>
<pre><code class="python">class GCounter(object):
def __init__(self, i, n):
self.i = i # server id
self.n = n # number of servers
self.xs = [0] * n
def query(self):
return sum(self.xs)
def add(self, x):
assert x >= 0
self.xs[self.i] += x
def merge(self, c):
zipped = zip(self.xs, c.xs)
self.xs = [max(x, y) for (x, y) in zipped]</code></pre>
<p>
If a G-Counter object is replicated on n servers, then we construct an
<code>GCounter(0, n)</code> object on server 0, a <code>GCounter(1,
n)</code> object on server 1, and so on.
Let's take a look at an example execution in which a G-Counter is
replicated on two servers where server <code>a</code> is server 0 and
server <code>b</code> is server 1.
</p>
<div class="svgbox">
<svg viewBox="0 0 400 120" id="gcounter_svg"></svg>
</div>
<center>
<table id="gcounter_table" class="srtable"></table>
</center>
<h3 id="pn_counter">PN-Counter</h3>
<p>
A <strong>PN-Counter</strong> CRDT represents a replicated counter which
can be added to <em>and</em> subtracted from.
</p>
<ul>
<li>
The <em>internal state</em> of a PN-Counter is a pair of two G-Counters
named <code>p</code> and <code>n</code>. <code>p</code> represents the
total value added to the PN-Counter while <code>n</code> represents the
total value subtracted from the PN-Counter.
</li>
<li>
The <code>query</code> method returns the difference <code>p.query() -
n.query()</code>.
</li>
<li>
The <code>add(x)</code> method (the first of the two update methods)
invokes <code>p.add(x)</code>.
</li>
<li>
The <code>sub(x)</code> method (the second of the two update methods)
invokes <code>n.add(x)</code>.
</li>
<li>
The <code>merge</code> method performs a pairwise merge of
<code>p</code> and <code>n</code>.
</li>
</ul>
<p>
We can implement a PN-Counter in Python as follows.
</p>
<pre><code class="python">class PNCounter(object):
def __init__(self, i, n):
self.p = GCounter(i, n)
self.n = GCounter(i, n)
def query(self):
return self.p.query() - self.n.query()
def add(self, x):
assert x >= 0
self.p.add(x)
def sub(self, x):
assert x >= 0
self.n.add(x)
def merge(self, c):
self.p.merge(c.p)
self.n.merge(c.n)</code></pre>
<p>
As with G-Counters, if a PN-Counter object is replicated on n servers,
then we construct an <code>PNCounter(0, n)</code> object on server 0, a
<code>PNCounter(1, n)</code> object on server 1, and so on. Let's take a
look at another example execution. For brevity, we've only listed a
subset of a PN-Counter's internal state.
</p>
<div class="svgbox">
<svg viewBox="0 0 400 120" id="pncounter_svg"></svg>
</div>
<center>
<table id="pncounter_table" class="srtable"></table>
</center>
<h3 id="g_set">G-Set</h3>
<p>
A <strong>G-Set</strong> CRDT represents a replicated set which can be
added to but not removed from.
</p>
<ul>
<li>The <em>internal state</em> of a G-Set is just a set!</li>
<li>The <code>query</code> method returns the set.</li>
<li>The <code>add(x)</code> update method adds <code>x</code> to the
set.</li>
<li>The <code>merge</code> method performs a set union.</li>
</ul>
<p>
Pretty simple, huh? Not surprising considering set union is commutative,
associative, and idempotent. We can implement a G-Set in Python as
follows.
</p>
<pre><code class="python">class GSet(object):
def __init__(self):
self.xs = set()
def query(self):
return self.xs
def add(self, x):
self.xs.add(x)
def merge(self, s):
self.xs = self.xs.union(s.xs)</code></pre>
<p>
Let's take a look at an example execution.
</p>
<div class="svgbox">
<svg viewBox="0 0 400 120" id="gset_svg"></svg>
</div>
<center>
<table id="gset_table" class="srtable"></table>
</center>
<h3 id="2p_set">2P-Set</h3>
<p>
A <strong>2P-Set</strong> CRDT represents a replicated set which can be
added to <em>and</em> removed from.
</p>
<ul>
<li>
The <em>internal state</em> of a 2P-Set is a pair of two G-Sets named
<code>a</code> and <code>r</code>. <code>a</code> represents the set of
values added to the 2P-Set while <code>r</code> represents the set of
values removed from the 2P-Set.
</li>
<li>
The <code>query</code> method returns the set difference
<code>a.query() - r.query()</code>.
</li>
<li>
The <code>add(x)</code> method (the first of the two update methods)
invokes <code>a.add(x)</code>.
</li>
<li>
The <code>sub(x)</code> method (the second of the two update methods)
invokes <code>r.add(x)</code>.
</li>
<li>
The <code>merge</code> method performs a pairwise merge of
<code>a</code> and <code>r</code>.
</li>
</ul>
<p>
We can implement a 2P-Set in Python as follows.
</p>
<pre><code class="python">class TwoPSet(object):
def __init__(self):
self.a = GSet()
self.r = GSet()
def query(self):
return self.a.query() - self.r.query()
def add(self, x):
self.a.add(x)
def sub(self, x):
self.r.add(x)
def merge(self, s):
self.a.merge(s.a)
self.r.merge(s.r)</code></pre>
<p>
Let's look at an example execution.
</p>
<div class="svgbox">
<svg viewBox="0 0 400 120" id="twopset_svg"></svg>
</div>
<center>
<table id="twopset_table" class="srtable"></table>
</center>
</article>
</div>
<script src="js/snap.svg.js"></script>
<script src="js/distributed_systems.js"></script>
<script src="js/state_based_replication.js"></script>
<script src="js/3_crdt.js"></script>
<script src="js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-90310997-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>