Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Scala With Cats: Case Study: CRDTs
Search
Kurochan
August 07, 2018
0
190
Scala With Cats: Case Study: CRDTs
Kurochan
August 07, 2018
Tweet
Share
More Decks by Kurochan
See All by Kurochan
AWS Elemental MediaPackageと格闘🤼
kurochan
2
37
サイバーエージェントでのSlack活用事例 @ 2025
kurochan
5
47
15年入社者に聞く! これまでのCAのキャリアとこれから
kurochan
1
240
入門 電気通信事業者
kurochan
13
5.6k
AWS x さくらのクラウドのハイブリッドクラウドによる安価なフレッツ閉域網接続の実装
kurochan
9
5.7k
GoでTCP Proxyを実装してみよう
kurochan
1
1.2k
サイバーエージェントの広告配信におけるIPoEトラフィックの概況
kurochan
0
500
スケールするというのはどういうことなのか
kurochan
14
5k
サイバーエージェントのGitHub Copilot導入と 開発生産性
kurochan
52
46k
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
820
Testing 201, or: Great Expectations
jmmastey
45
7.6k
Practical Orchestrator
shlominoach
190
11k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Six Lessons from altMBA
skipperchong
28
4k
Typedesign – Prime Four
hannesfritz
42
2.8k
Side Projects
sachag
455
43k
Automating Front-end Workflow
addyosmani
1370
200k
4 Signs Your Business is Dying
shpigford
184
22k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
131
19k
Imperfection Machines: The Place of Print at Facebook
scottboms
268
13k
Transcript
Scala with Cats Case Study: CRDTs https://underscore.io/books/scala-with-cats/
CRDTs • Commutative Replicated Data Types • or Conflict-free Replicated
Data Types • ݁Ռ߹ੑͷ͋Δσʔλߏ • ϞϊΠυ͕ͲͷΑ͏ʹཱ͔ͭͷྫͱͯ͠CRDTsΛհ
Consistency • γεςϜ͕εέʔϧ͢Δͱ୯ҰͷϚγϯΛ͑ͯσʔλ Λཧ͠ͳ͍ͱ͍͚ͳ͘ͳΔ • શͯͷϚγϯ͕ಉ͡σʔλΛ࣋ͭڥΛఆ͢Δ • 1ͭͷϚγϯ͕ϢʔβͷύεϫʔυΛߋ৽͢Δͱɺ શͷϚγϯ͕ಉ͡ίϐʔΛ࣋ͭ •
શͷϚγϯ͕ͦΕΛड͚ೖΕͨ͋ͱ ͦͷૢ࡞ྃ͢Δ
Eventual Consistency • લϖʔδͷྫͩͱ߹ੑ͕อͯΔ͕ɺ1ͭͷૢ࡞ͷͨΊʹશ ͱಉظΛͱΔͷͰϨΠςϯγ͕ѱԽ͢Δ • ωοτϫʔΫোʹΑΔஅൃੜ͢Δ • ߹ੑΛอͭͨΊʹૢ࡞Λڋ൱͢Δ͔͠ͳ͍ •
݁Ռ߹ੑͷ͋ΔγεςϜͰ֤Ϛγϯ͕Ұ࣌తʹҟͳΔ ঢ়ଶΛ࣋ͭࣄΛڐ༰͢Δ • ݁Ռతʹ߹ੑ͕औΕΕ͍͍ͷͰૢ࡞ʹର͢ΔԠ ૣ͘ͳΔ • ωοτϫʔΫোʹΑΔஅ͕ൃੜͯ͠ॲཧΛܧଓͰ͖Δ
CRDT • ͲͷΑ͏ʹͯ͠ϚγϯؒͰ݁Ռ߹ੑΛऔΔͷ͔ʁ • CRDT͕ͻͱͭͷղͱͳΔ • Commutative Replicated Data Types
• CRDTͷ࣮ͷͻͱͭͱͯ͠G-CounterΛհ͢Δ
G-Counters • ࢄΧϯλͱͯ͑͠Δ • ૿Ճ͢ΔΦϖϨʔγϣϯʹͷΈରԠ͢Δ • webαʔόͷ๚ऀͷΧϯτʹ͑Δ
Simple counters • ୯ʹड͚औͬͨϦΫΤετΛ௨ͯ͠͠߹ΘͤΔ
Simple counters • ՃͰϦΫΤετ͕དྷͨͱ͖ʹΧϯλͷΛ্͠ ͛Δͱ͓͔͘͠ͳΔ • 2ͱ"6"Λอ͍࣋ͯ͠ͳ͍ͱ͍͚ͳ͍ͣ…
G-Counters • G-Counter֤ϚγϯͷΧϯλΛ͍࣋ͬͯΔ • ΛΠϯΫϦϝϯτͰ͖ΔͷࣗͷΧϯλ͚ͩ • ঢ়ଶߋ৽ͷϝοηʔδ͕དྷͨΒɺͦͷϚγϯͷΧ ϯλͷ࠷େͰஔ͖͑Δ(Grow only)
G-Counters • ͦͷϚγϯͷΧϯλͷ࠷େͰஔ͖͑Δ
11.2.3 Exercise: GCounter Implementation • G-CounterΛ࣮ͯ͠Έ·͠ΐ͏ • counter: Map[String, Int]
• key͕machine id, value͕Χϯτ • increment: GCounter • ࢦఆͨ͠machineͷΧϯτΛࢦఆͨ͠ amount͚ͩ͢ • merge: GCounter • 2ͭͷGCounterΛϚʔδ͢Δ
11.2.3 Exercise: GCounter Implementation • G-CounterΛ࣮ͯ͠Έ·͠ΐ͏
Additional Exercise: PN-Counter Implementation • PN-CounterΛ࣮ͯ͠Έ·͠ΐ͏ • Ҿ͖ࢉՄೳʹͨ͠CRDTs (Positive, Negative)
Additional Exercise: PN-Counter Implementation • PN-CounterΛ࣮ͯ͠Έ·͠ΐ͏ • Ҿ͖ࢉՄೳʹͨ͠CRDTs
Generalisation • ͖͞΄ͲͷG-CounterΛநԽ͍ͯ͘͠ • G-Counter • Ճࢉ: ΠϯΫϦϝϯτ͞Εͨ߹ܭΛ࣋ͭ • ࠷େ:
Ϛʔδ࣌࠷େΛ࣋ͭ • ॳظ 0 • => ϞϊΠυͰ͋Δ • (a + b) + c == a + (b + c) • 0 + a == a + 0 == a • a max a = a (mergeͷૢ࡞ႈੑ͕͋Δ)
Generalisation • ͖͞΄ͲͷG-CounterΛநԽ͍ͯ͘͠ • increment: monoid • total: monoid +
Մ • merge: monoid + ႈ + Մ • => ༗քଋͱݴ͏
Generalisation • લड़ͷಛΑΓɺGCounterࣗવͱಉ༷ͳૢ࡞͕Ͱ ͖Δͷʹஔ͖͑ΒΕΔ • ࣗવͷΘΓʹSet[A]ΛͬͯGSetͱ͍͏ܕΛ࣮ ͢Δ͜ͱͰ͖Δͣ
Bounded SemiLattice • ༗քଋ • https://qiita.com/yasuabe2613/items/ ba2c5136a4e0822b6ba4
11.3.2 Exercise: BoundedSemiLattice Instances • IntͱSet༻ͷ༗քଋΛ࣮ͯ͠Έ·͠ΐ͏ • Intࣗવ(0ΛؚΉ)͔࣋ͨ͠ͳ͍ͱԾఆͯ͠Α͍
11.3.2 Exercise: BoundedSemiLattice Instances • IntͱSet༻ͷ༗քଋΛ࣮ͯ͠Έ·͠ΐ͏ • Intࣗવ(0ΛؚΉ)͔࣋ͨ͠ͳ͍ͱԾఆͯ͠Α͍
11.3.3 Exercise:Generic G-Counter • MonoidͱBoundedSemiLatticeΛͬͯGCounterΛ ࣮͠·͠ΐ͏ • Monoidͷจ๏Λͬͯγϯϓϧʹॻ͖·͠ΐ͏
Abstracting G-Counter to a Type Class • TypeClassΛͬͯG-CounterΛ͞ΒʹநԽ͢Δ • Map2ͭͷܕύϥϝʔλΛऔΔԿ͔ͳΒΑ͍ͷͰ
Abstracting a Key Value Store • MapKVSʹஔ͖͑ΒΕΔͣ • Mapͩͬͨ෦ΛநԽ͢Δ
Abstracting a Key Value Store • MapKVSʹஔ͖͑ΒΕΔͣ • σʔλܕͷૢ࡞Λ֦ு͢Δimplicit class
Abstracting a Key Value Store • KVS͕நԽͰ͖ͨ͜ͱͰKVSͷ༷Λຬ͍ͨͯ͠Ε ͲΜͳܕͰG-Counter͕࡞ΕΔΑ͏ʹͳͬͨ
Summary • γϯϓϧͳCRDTͰ͋ΔG-CounterΛTypeClassΛ༻͍ͯ Ͳ͏࣮͢Δ͔Λࣔͨ͠ • ίʔυͷ࠶ར༻ੑΛߴΊɺΧϯτ͢Δͱ͍͏ૢ࡞ʹ ͚ͩʹറΒΕͳ͘ͳͬͨ • ଞʹCRDTs͍Ζ͍Ζ͋ΔͷͰҎԼΛࢀߟʹ •
https://hal.inria.fr/inria-00609399v2/document
None