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
170
Scala With Cats: Case Study: CRDTs
Kurochan
August 07, 2018
Tweet
Share
More Decks by Kurochan
See All by Kurochan
15年入社者に聞く! これまでのCAのキャリアとこれから
kurochan
1
190
入門 電気通信事業者
kurochan
13
5.5k
AWS x さくらのクラウドのハイブリッドクラウドによる安価なフレッツ閉域網接続の実装
kurochan
9
5.5k
GoでTCP Proxyを実装してみよう
kurochan
1
1k
サイバーエージェントの広告配信におけるIPoEトラフィックの概況
kurochan
0
460
スケールするというのはどういうことなのか
kurochan
14
4.8k
サイバーエージェントのGitHub Copilot導入と 開発生産性
kurochan
52
45k
Cloudflare Zero Trustを利用したセキュアな開発環境へのアクセス手法の確立
kurochan
10
3.4k
セキュキャンを卒業してその後
kurochan
0
1.4k
Featured
See All Featured
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
227
22k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.4k
A better future with KSS
kneath
239
17k
Automating Front-end Workflow
addyosmani
1369
200k
Building a Modern Day E-commerce SEO Strategy
aleyda
39
7.2k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
45
9.5k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.3k
Statistics for Hackers
jakevdp
798
220k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
Making the Leap to Tech Lead
cromwellryan
133
9.2k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
The Cult of Friendly URLs
andyhume
78
6.3k
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