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
Security-JAWS【第35回】勉強会クラウドにおけるマルウェアやコンテンツ改ざんへの対策
4su_para
0
170
データプロダクトの定義からはじめる、データコントラクト駆動なデータ基盤
chanyou0311
2
270
dev 補講: プロダクトセキュリティ / Product security overview
wa6sn
1
2.3k
インフラとバックエンドとフロントエンドをくまなく調べて遅いアプリを早くした件
tubone24
1
430
TanStack Routerに移行するのかい しないのかい、どっちなんだい! / Are you going to migrate to TanStack Router or not? Which one is it?
kaminashi
0
560
IBC 2024 動画技術関連レポート / IBC 2024 Report
cyberagentdevelopers
PRO
0
110
Amplify Gen2 Deep Dive / バックエンドの型をいかにしてフロントエンドへ伝えるか #TSKaigi #TSKaigiKansai #AWSAmplifyJP
tacck
PRO
0
370
【Pycon mini 東海 2024】Google Colaboratoryで試すVLM
kazuhitotakahashi
2
490
AGIについてChatGPTに聞いてみた
blueb
0
130
Can We Measure Developer Productivity?
ewolff
1
150
隣接領域をBeyondするFinatextのエンジニア組織設計 / beyond-engineering-areas
stajima
1
270
Making your applications cross-environment - OSCG 2024 NA
salaboy
0
180
Featured
See All Featured
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Building Applications with DynamoDB
mza
90
6.1k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Become a Pro
speakerdeck
PRO
25
5k
Automating Front-end Workflow
addyosmani
1366
200k
The Language of Interfaces
destraynor
154
24k
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 ͕සൟͳϢʔεέʔεͰ ༗ޮ͔