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
生成AIによるテスト設計支援プロセスの構築とプロセス内のボトルネック解消の取り組み / 20241220 Suguru Ishii
shift_evolve
0
160
DevFest 2024 Incheon / Songdo - Compose UI 조합 심화
wisemuji
0
240
12 Days of OpenAIから読み解く、生成AI 2025年のトレンド
shunsukeono_am
0
970
TypeScript開発にモジュラーモノリスを持ち込む
sansantech
PRO
3
840
サーバーなしでWordPress運用、できますよ。
sogaoh
PRO
0
170
AWS環境におけるランサムウェア攻撃対策の設計
nrinetcom
PRO
1
310
クレカ・銀行連携機能における “状態”との向き合い方 / SmartBank Engineer LT Event
smartbank
3
130
SpiderPlus & Co. エンジニア向け会社紹介資料
spiderplus_cb
0
270
いまからでも遅くないコンテナ座学
nomu
0
190
Unlearn Product Development - Unleashed Edition
lemiorhan
PRO
2
160
Fabric 移行時の躓きポイントと対応策
ohata_ds
1
110
プロダクト組織で取り組むアドベントカレンダー/Advent Calendar in Product Teams
mixplace
0
640
Featured
See All Featured
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Being A Developer After 40
akosma
89
590k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Building Applications with DynamoDB
mza
92
6.1k
A designer walks into a library…
pauljervisheath
205
24k
Gamification - CAS2011
davidbonilla
80
5.1k
Designing Experiences People Love
moore
139
23k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.3k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
The Pragmatic Product Professional
lauravandoore
32
6.3k
Side Projects
sachag
452
42k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
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 ͕සൟͳϢʔεέʔεͰ ༗ޮ͔