Kurochan
August 07, 2018
82

# Scala With Cats: Case Study: CRDTs

August 07, 2018

## Transcript

2. ### CRDTs • Commutative Replicated Data Types • or Conﬂict-free Replicated

Data Types • ݁Ռ੔߹ੑͷ͋Δσʔλߏ଄ • ϞϊΠυ͕ͲͷΑ͏ʹ໾ཱ͔ͭͷྫͱͯ͠CRDTsΛ঺հ
3. ### Consistency • γεςϜ͕εέʔϧ͢Δͱ୯ҰͷϚγϯΛ௒͑ͯσʔλ Λ؅ཧ͠ͳ͍ͱ͍͚ͳ͘ͳΔ • શͯͷϚγϯ͕ಉ͡σʔλΛ࣋ͭ؀ڥΛ૝ఆ͢Δ • 1ͭͷϚγϯ͕ϢʔβͷύεϫʔυΛߋ৽͢Δͱɺ  શ୆ͷϚγϯ͕ಉ͡ίϐʔΛ࣋ͭ •

શ୆ͷϚγϯ͕ͦΕΛड͚ೖΕͨ͋ͱ  ͦͷૢ࡞͸׬ྃ͢Δ
4. ### Eventual Consistency • લϖʔδͷྫͩͱ੔߹ੑ͕อͯΔ͕ɺ1ͭͷૢ࡞ͷͨΊʹશ୆ ͱಉظΛͱΔͷͰϨΠςϯγ͕ѱԽ͢Δ • ωοτϫʔΫো֐ʹΑΔ෼அ΋ൃੜ͢Δ • ੔߹ੑΛอͭͨΊʹ͸ૢ࡞Λڋ൱͢Δ͔͠ͳ͍ •

݁Ռ੔߹ੑͷ͋ΔγεςϜͰ͸֤Ϛγϯ͕Ұ࣌తʹҟͳΔ  ঢ়ଶΛ࣋ͭࣄΛڐ༰͢Δ • ݁Ռతʹ੔߹ੑ͕औΕΕ͹͍͍ͷͰૢ࡞ʹର͢ΔԠ౴͸  ૣ͘ͳΔ • ωοτϫʔΫো֐ʹΑΔ෼அ͕ൃੜͯ͠΋ॲཧΛܧଓͰ͖Δ
5. ### CRDT • ͲͷΑ͏ʹͯ͠ϚγϯؒͰ݁Ռ੔߹ੑΛऔΔͷ͔ʁ • CRDT͕ͻͱͭͷղͱͳΔ • Commutative Replicated Data Types

• CRDTͷ࣮૷ͷͻͱͭͱͯ͠G-CounterΛ঺հ͢Δ

11. ### 11.2.3 Exercise: GCounter Implementation • G-CounterΛ࣮૷ͯ͠Έ·͠ΐ͏ • counter: Map[String, Int]

• key͕machine id, value͕Χ΢ϯτ • increment: GCounter • ࢦఆͨ͠machineͷΧ΢ϯτΛࢦఆͨ͠  amount͚ͩ଍͢ • merge: GCounter • 2ͭͷGCounterΛϚʔδ͢Δ

15. ### Generalisation • ͖͞΄ͲͷG-CounterΛந৅Խ͍ͯ͘͠ • G-Counter͸ • Ճࢉ: ΠϯΫϦϝϯτ͞Εͨ߹ܭ஋Λ࣋ͭ • ࠷େ஋:

Ϛʔδ࣌͸࠷େ஋Λ࣋ͭ • ॳظ஋͸ 0 • => ϞϊΠυͰ͋Δ • (a + b) + c == a + (b + c) • 0 + a == a + 0 == a • a max a = a (mergeͷૢ࡞͸ႈ౳ੑ͕͋Δ)
16. ### Generalisation • ͖͞΄ͲͷG-CounterΛந৅Խ͍ͯ͘͠ • increment: monoid • total: monoid +

Մ׵ • merge: monoid + ႈ౳ + Մ׵ • => ༗ք൒ଋͱݴ͏

26. ### Summary • γϯϓϧͳCRDTͰ͋ΔG-CounterΛTypeClassΛ༻͍ͯ Ͳ͏࣮૷͢Δ͔Λࣔͨ͠ • ίʔυͷ࠶ར༻ੑΛߴΊɺΧ΢ϯτ͢Δͱ͍͏ૢ࡞ʹ ͚ͩʹറΒΕͳ͘ͳͬͨ • ଞʹ΋CRDTs͸͍Ζ͍Ζ͋ΔͷͰҎԼΛࢀߟʹ •

https://hal.inria.fr/inria-00609399v2/document
27. None