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
23
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
ISUCONに強くなるかもしれない日々の過ごしかた/Findy ISUCON 2024-11-14
fujiwara3
8
870
DynamoDB でスロットリングが発生したとき/when_throttling_occurs_in_dynamodb_short
emiki
0
130
Platform Engineering for Software Developers and Architects
syntasso
1
520
20241120_JAWS_東京_ランチタイムLT#17_AWS認定全冠の先へ
tsumita
2
270
これまでの計測・開発・デプロイ方法全部見せます! / Findy ISUCON 2024-11-14
tohutohu
3
370
Terraform CI/CD パイプラインにおける AWS CodeCommit の代替手段
hiyanger
1
240
BLADE: An Attempt to Automate Penetration Testing Using Autonomous AI Agents
bbrbbq
0
310
Amazon Personalizeのレコメンドシステム構築、実際何するの?〜大体10分で具体的なイメージをつかむ〜
kniino
1
100
iOS/Androidで同じUI体験をネ イティブで作成する際に気をつ けたい落とし穴
fumiyasac0921
1
110
iOSチームとAndroidチームでブランチ運用が違ったので整理してます
sansantech
PRO
0
140
【若手エンジニア応援LT会】ソフトウェアを学んできた私がインフラエンジニアを目指した理由
kazushi_ohata
0
150
TypeScript、上達の瞬間
sadnessojisan
46
13k
Featured
See All Featured
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
How to train your dragon (web standard)
notwaldorf
88
5.7k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Documentation Writing (for coders)
carmenintech
65
4.4k
Writing Fast Ruby
sferik
627
61k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Imperfection Machines: The Place of Print at Facebook
scottboms
265
13k
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 ͕සൟͳϢʔεέʔεͰ ༗ޮ͔