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
150
Scala With Cats: Case Study: CRDTs
Kurochan
August 07, 2018
Tweet
Share
More Decks by Kurochan
See All by Kurochan
入門 電気通信事業者
kurochan
12
5.2k
AWS x さくらのクラウドのハイブリッドクラウドによる安価なフレッツ閉域網接続の実装
kurochan
9
5.1k
GoでTCP Proxyを実装してみよう
kurochan
1
850
サイバーエージェントの広告配信におけるIPoEトラフィックの概況
kurochan
0
390
スケールするというのはどういうことなのか
kurochan
14
4.5k
サイバーエージェントのGitHub Copilot導入と 開発生産性
kurochan
45
42k
Cloudflare Zero Trustを利用したセキュアな開発環境へのアクセス手法の確立
kurochan
10
3.1k
セキュキャンを卒業してその後
kurochan
0
1.3k
サイバーエージェントの実践×実験Snowflake 導入の経緯から最新機能のトライアルまで / How Snowflake Is Used In CyberAgent - Go To the Future
kurochan
1
1k
Featured
See All Featured
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Automating Front-end Workflow
addyosmani
1366
200k
KATA
mclloyd
29
14k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Embracing the Ebb and Flow
colly
84
4.5k
Music & Morning Musume
bryan
46
6.2k
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