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
Firestore で CQRS やってみた
Search
Yuji Kinjo
May 28, 2019
Technology
22
4.8k
Firestore で CQRS やってみた
Yuji Kinjo
May 28, 2019
Tweet
Share
More Decks by Yuji Kinjo
See All by Yuji Kinjo
SLOを組織文化にするための挑戦
yukin01
1
2.9k
Other Decks in Technology
See All in Technology
サービスでLLMを採用したばっかりに振り回され続けたこの一年のあれやこれや
segavvy
2
420
多領域インシデントマネジメントへの挑戦:ハードウェアとソフトウェアの融合が生む課題/Challenge to multidisciplinary incident management: Issues created by the fusion of hardware and software
bitkey
PRO
2
100
Amazon VPC Lattice 最新アップデート紹介 - PrivateLink も似たようなアップデートあったけど違いとは
bigmuramura
0
190
祝!Iceberg祭開幕!re:Invent 2024データレイク関連アップデート10分総ざらい
kniino
3
260
小学3年生夏休みの自由研究「夏休みに Copilot で遊んでみた」
taichinakamura
0
150
alecthomas/kong はいいぞ / kamakura.go#7
fujiwara3
1
300
プロダクト開発を加速させるためのQA文化の築き方 / How to build QA culture to accelerate product development
mii3king
1
260
KubeCon NA 2024 Recap / Running WebAssembly (Wasm) Workloads Side-by-Side with Container Workloads
z63d
1
250
統計データで2024年の クラウド・インフラ動向を眺める
ysknsid25
2
840
Oracle Cloud Infrastructure:2024年12月度サービス・アップデート
oracle4engineer
PRO
0
180
ガバメントクラウドのセキュリティ対策事例について
fujisawaryohei
0
540
フロントエンド設計にモブ設計を導入してみた / 20241212_cloudsign_TechFrontMeetup
bengo4com
0
1.9k
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
The Invisible Side of Design
smashingmag
298
50k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
28
900
Testing 201, or: Great Expectations
jmmastey
40
7.1k
Bash Introduction
62gerente
608
210k
Documentation Writing (for coders)
carmenintech
66
4.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Rails Girls Zürich Keynote
gr2m
94
13k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Optimising Largest Contentful Paint
csswizardry
33
3k
Transcript
Firestore Ͱ CQRS ͬͯΈͨ Firebase Meetup #13 @DeNA
Kinjo Yuji iOS Engineer Twitter: @_yukin01 GitHub: @yukin01
ɾσʔλϞσϦϯά͕͍͠ͱ͍͏ ɾ࣮ࡍʹͬͯΈΔ ɾCQRS ͕͑Δ͔͠Εͳ͍ ɾ·ͱΊ ͜ΜͳΛ͠·͢
ϦϨʔγϣϯγοϓͲ͏දݱ͍ͯ͠·͔͢ʁ
Top-level Collection or Sub Collection collection("posts") .where("room_id", "==", "room_001") .orderBy("createdAt",
"desc") collection("rooms") .doc("room_001") .collection("posts") .orderBy("createdAt", "desc") Top-level collection Sub collection
Sub collection Map (Array) or Sub Collection
͍ΖΜͳཁૉͰܾ·ͬͯ͘Δʢઈରղͳ͍ ɾଟॏ => 1 : 1, 1 : n, n
: n ɾࢠυΩϡϝϯτͷϥΠϑαΠΫϧ ɾΫΤϦ ɾॻ͖ࠐΈͷ੍ݶ document => 1 times/s collection => 500 times/s ɾetc.
Firestore ͰͰ͖Δ͜ͱࣗମ૿͍͑ͯΔ ɾarrayUnion/array-contains ɾincrement ɾCollection Group ʢΩϟονΞοϓେมʣ
࠷ۙࢥ͏͜ͱ ϢʔεέʔεʢͲ͏͍͏σʔλΛऔಘɾߋ৽͍͔ͨ͠ʣ ʹΑͬͯ ࠷దͳσʔλߏ͕ܾ·Δʢ࠷దղ͋Δ => ߏʹΑͬͯσʔλͷऔಘɾߋ৽͕ ඇৗʹ͘͠ͳΔ͜ͱ…
νϟοτػೳͷσʔλߏΛߟ͑Δ
͜Μͳײ͡
Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ - ະಡ -
࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ߘ࣌ؒͰιʔτ
Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ - ະಡ -
࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ߘ࣌ؒͰιʔτ
Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ - ະಡ -
࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ߘ࣌ؒͰιʔτ
Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ - ະಡ -
࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ߘ࣌ؒͰιʔτ
Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ - ະಡ -
࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ߘ࣌ؒͰιʔτ
Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ - ະಡ -
࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ߘ࣌ؒͰιʔτ
·ͣγϯϓϧʹ
ɾϧʔϜ໊ɺ ΞΠίϯը૾ɺ ϝϯόʔ ɾarray-contains ͷΫΤϦ ɾະಡͲ͏͢Δ…ʁ
ɾະಡ ࠷৽ϝοηʔδ ɾཁ݅ຬͨͤΔ ɾWrite ͷ੍ݶ͕…" (unreadCount ʹूத͢Δ)
ɾSub Collection Խ ɾཁ݅ຬͨͤΔ ɾ1ճͷΫΤϦͰ औಘͰ͖ͳ͍ Rooms => Members =>
Messages
ɾJOIN ͢Δύλʔϯ ɾਖ਼نԽ ɾΫϥΠΞϯταΠυ JOIN ɾ͍ΘΏΔ N+1 ʢઈରμϝͱ͍͏Θ͚Ͱͳ͍ʣ ɾJOIN
͠ͳ͍ύλʔϯ ɾඇਖ਼نԽ ɾඞཁͳσʔλΛԽʢίϐʔʣ͢Δ ɾRDB Ͱߟ͑ʹ͍͕͘ NoSQL ͰҰൠత
ɾJOIN ͢Δύλʔϯ ɾਖ਼نԽ ɾΫϥΠΞϯταΠυ JOIN ɾ͍ΘΏΔ N+1 ʢઈରμϝͱ͍͏Θ͚Ͱͳ͍ʣ ɾJOIN
͠ͳ͍ύλʔϯ ɾඇਖ਼نԽ ɾඞཁͳσʔλΛԽʢίϐʔʣ͢Δ ɾRDB Ͱߟ͑ʹ͍͕͘ NoSQL ͰҰൠత <= ࠾༻ͯ͠ΈΔ
ඇਖ਼نԽʢίϐʔʣͷ࣮ݱํ๏ ɾTransaction or Batch Write ɾAtomic ͳॻ͖ࠐΈΛ࣮ݱ ɾ࠷େ 500 ݅ͷ੍ݶ͋Γ
ɾFirestore Trigger ɾ݁Ռ߹ੑ ɾΤϥʔॲཧɺϦτϥΠॲཧͷඞཁੑ
None
͚ͩ͜͜ݟΕྑ͍
Trigger functions
Trigger functions
Trigger functions
Batch Write
ϐλΰϥεΠον
CQRS (Command Query Responsibility Segregation) Command (ߋ৽ܥ) ͱ Query (ࢀরܥ)
ͰػೳΛׂ DDD ͷจ຺ͰޠΒΕΔ͜ͱ͕ଟ͍ ES (Event Sourcing) ʮঢ়ଶʯͰͳ͘ʮΠϕϯτʯΛه͍ͯ͘͠ߟ͑ํ ҙͷঢ়ଶΠϕϯτΛ࠶ੜ͢Ε࠶ݱͰ͖Δ CQRS ͱΈ߹ΘͤͰΑ͘ޠΒΕ͍ͯΔ
h"ps://www.slideshare.net/AmazonWebServices/ srv301op=mizing-serverless-applica=on-data-=ers-with-amazon-dynamodb
https://www.slideshare.net/AmazonWebServices/ srv301optimizing-serverless-application-data-tiers-with-amazon-dynamodb
Command ଆΠϕϯτ͚ͩΛه (ཁCreate ͷΈ) => ᘳʹ࣮ݱ͢ΔͷΊ͓ͯ͘ σʔλͷྲྀΕΛҰํʹ͢Δͱ͜ΖΛऔΓೖΕΔ
͜͏͍͏Πϝʔδ
None
ɾਖ਼نԽ ɾηΩϡϦςΟϧʔϧͰ Write ͷΈڐՄ ɾ΄΅ RDB ͷײ֮
ɾඇਖ਼نԽ ɾηΩϡϦςΟϧʔϧͰ Read ͷΈڐՄ ɾΫϥΠΞϯτͷॲཧָ͕ ɾཁ݅มߋʹରԠ͍͢͠
Trigger functions Command -> Query ͷҰํ
·ͱΊ - Firestore + Functions Ͱ CQRS ΛऔΓೖΕͨ σʔλϞσϦϯάΛ࣮ -
Command ͱ Query Ͱશʹ - ϐλΰϥεΠονΛҰํʹͰ͖Δ - ͕ - ঢ়ଶ͕ଟ͘ɺRead ͕සൟͳϢʔεέʔεͰ ༗ޮ͔