Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Firestore で CQRS やってみた

Firestore で CQRS やってみた

Yuji Kinjo

May 28, 2019
Tweet

More Decks by Yuji Kinjo

Other Decks in Technology

Transcript

  1. 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
  2. ͍ΖΜͳཁૉͰܾ·ͬͯ͘Δʢઈରղ͸ͳ͍ ɾଟॏ౓ => 1 : 1, 1 : n, n

    : n ɾ਌ࢠυΩϡϝϯτͷϥΠϑαΠΫϧ ɾΫΤϦ ɾॻ͖ࠐΈͷ੍ݶ document => 1 times/s collection => 500 times/s ɾetc.
  3. Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ਺ - ະಡ਺ -

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  4. Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ਺ - ະಡ਺ -

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  5. Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ਺ - ະಡ਺ -

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  6. Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ਺ - ະಡ਺ -

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  7. Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ਺ - ະಡ਺ -

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  8. Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ਺ - ະಡ਺ -

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  9. ɾJOIN ͢Δύλʔϯ ɾਖ਼نԽ ɾΫϥΠΞϯταΠυ JOIN ɾ͍ΘΏΔ N+1 ໰୊ ʢઈରμϝͱ͍͏Θ͚Ͱ͸ͳ͍ʣ ɾJOIN

    ͠ͳ͍ύλʔϯ ɾඇਖ਼نԽ ɾඞཁͳσʔλΛ৑௕Խʢίϐʔʣ͢Δ ɾRDB Ͱ͸ߟ͑ʹ͍͕͘ NoSQL Ͱ͸Ұൠత
  10. ɾJOIN ͢Δύλʔϯ ɾਖ਼نԽ ɾΫϥΠΞϯταΠυ JOIN ɾ͍ΘΏΔ N+1 ໰୊ ʢઈରμϝͱ͍͏Θ͚Ͱ͸ͳ͍ʣ ɾJOIN

    ͠ͳ͍ύλʔϯ ɾඇਖ਼نԽ ɾඞཁͳσʔλΛ৑௕Խʢίϐʔʣ͢Δ ɾRDB Ͱ͸ߟ͑ʹ͍͕͘ NoSQL Ͱ͸Ұൠత <= ࠾༻ͯ͠ΈΔ
  11. ඇਖ਼نԽʢίϐʔʣͷ࣮ݱํ๏ ɾTransaction or Batch Write ɾAtomic ͳॻ͖ࠐΈΛ࣮ݱ ɾ࠷େ 500 ݅ͷ੍ݶ͋Γ

    ɾFirestore Trigger ɾ݁Ռ੔߹ੑ ɾΤϥʔॲཧɺϦτϥΠॲཧͷඞཁੑ
  12. CQRS (Command Query Responsibility Segregation) Command (ߋ৽ܥ) ͱ Query (ࢀরܥ)

    ͰػೳΛ෼ׂ DDD ͷจ຺ͰޠΒΕΔ͜ͱ͕ଟ͍ ES (Event Sourcing) ʮঢ়ଶʯͰ͸ͳ͘ʮΠϕϯτʯΛه࿥͍ͯ͘͠ߟ͑ํ ೚ҙͷঢ়ଶ͸ΠϕϯτΛ࠶ੜ͢Ε͹࠶ݱͰ͖Δ CQRS ͱ૊Έ߹ΘͤͰΑ͘ޠΒΕ͍ͯΔ
  13. ·ͱΊ - Firestore + Functions Ͱ CQRS ΛऔΓೖΕͨ σʔλϞσϦϯάΛ࣮૷ -

    Command ͱ Query Ͱ׬શʹ෼཭ - ϐλΰϥεΠονΛҰํ޲ʹͰ͖Δ - ͕ - ঢ়ଶ͕ଟ͘ɺRead ͕සൟͳϢʔεέʔεͰ͸ ༗ޮ͔΋