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
NW-JAWS #14 re:Invent 2024(予選落ち含)で 発表された推しアップデートについて
nagisa53
0
250
なぜCodeceptJSを選んだか
goataka
0
150
マルチプロダクト開発の現場でAWS Security Hubを1年以上運用して得た教訓
muziyoshiz
2
2.1k
アップデート紹介:AWS Data Transfer Terminal
stknohg
PRO
0
170
組織に自動テストを書く文化を根付かせる戦略(2024冬版) / Building Automated Test Culture 2024 Winter Edition
twada
PRO
11
3.3k
watsonx.ai Dojo #5 ファインチューニングとInstructLAB
oniak3ibm
PRO
0
160
KnowledgeBaseDocuments APIでベクトルインデックス管理を自動化する
iidaxs
1
250
OpenShift Virtualizationのネットワーク構成を真剣に考えてみた/OpenShift Virtualization's Network Configuration
tnk4on
0
130
20241220_S3 tablesの使い方を検証してみた
handy
3
200
GitHub Copilot のテクニック集/GitHub Copilot Techniques
rayuron
23
11k
10個のフィルタをAXI4-Streamでつなげてみた
marsee101
0
160
サービスでLLMを採用したばっかりに振り回され続けたこの一年のあれやこれや
segavvy
2
360
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Rails Girls Zürich Keynote
gr2m
94
13k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Adopting Sorbet at Scale
ufuk
73
9.1k
The World Runs on Bad Software
bkeepers
PRO
65
11k
RailsConf 2023
tenderlove
29
940
Gamification - CAS2011
davidbonilla
80
5.1k
Speed Design
sergeychernyshev
25
670
Making the Leap to Tech Lead
cromwellryan
133
9k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.1k
Unsuck your backbone
ammeep
669
57k
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 ͕සൟͳϢʔεέʔεͰ ༗ޮ͔