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
15年入社者に聞く! これまでのCAのキャリアとこれから
kurochan
1
230
入門 電気通信事業者
kurochan
13
5.6k
AWS x さくらのクラウドのハイブリッドクラウドによる安価なフレッツ閉域網接続の実装
kurochan
9
5.7k
GoでTCP Proxyを実装してみよう
kurochan
1
1.1k
サイバーエージェントの広告配信におけるIPoEトラフィックの概況
kurochan
0
490
スケールするというのはどういうことなのか
kurochan
14
4.9k
サイバーエージェントのGitHub Copilot導入と 開発生産性
kurochan
52
46k
Cloudflare Zero Trustを利用したセキュアな開発環境へのアクセス手法の確立
kurochan
10
3.5k
セキュキャンを卒業してその後
kurochan
0
1.4k
Featured
See All Featured
RailsConf 2023
tenderlove
30
1.1k
Product Roadmaps are Hard
iamctodd
PRO
54
11k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.4k
Docker and Python
trallard
45
3.5k
Embracing the Ebb and Flow
colly
86
4.7k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
Practical Orchestrator
shlominoach
189
11k
How to train your dragon (web standard)
notwaldorf
96
6.1k
We Have a Design System, Now What?
morganepeng
53
7.7k
Into the Great Unknown - MozCon
thekraken
40
1.9k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
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