Firestore で CQRS やってみた

Firestore で CQRS やってみた

E67da6dea537bb62b09d55f52be58872?s=128

Yuji Kinjo

May 28, 2019
Tweet

Transcript

  1. 5.

    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. 7.

    ͍ΖΜͳཁૉͰܾ·ͬͯ͘Δʢઈରղ͸ͳ͍ ɾଟॏ౓ => 1 : 1, 1 : n, n

    : n ɾ਌ࢠυΩϡϝϯτͷϥΠϑαΠΫϧ ɾΫΤϦ ɾॻ͖ࠐΈͷ੍ݶ document => 1 times/s collection => 500 times/s ɾetc.
  3. 12.

    Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ਺ - ະಡ਺ -

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  4. 13.

    Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ਺ - ະಡ਺ -

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  5. 14.

    Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ਺ - ະಡ਺ -

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  6. 15.

    Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ਺ - ະಡ਺ -

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  7. 16.

    Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ਺ - ະಡ਺ -

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  8. 17.

    Contents - ϧʔϜ໊ - ΞΠίϯը૾ - ϝϯόʔ਺ - ະಡ਺ -

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  9. 22.

    ɾJOIN ͢Δύλʔϯ ɾਖ਼نԽ ɾΫϥΠΞϯταΠυ JOIN ɾ͍ΘΏΔ N+1 ໰୊ ʢઈରμϝͱ͍͏Θ͚Ͱ͸ͳ͍ʣ ɾJOIN

    ͠ͳ͍ύλʔϯ ɾඇਖ਼نԽ ɾඞཁͳσʔλΛ৑௕Խʢίϐʔʣ͢Δ ɾRDB Ͱ͸ߟ͑ʹ͍͕͘ NoSQL Ͱ͸Ұൠత
  10. 23.

    ɾJOIN ͢Δύλʔϯ ɾਖ਼نԽ ɾΫϥΠΞϯταΠυ JOIN ɾ͍ΘΏΔ N+1 ໰୊ ʢઈରμϝͱ͍͏Θ͚Ͱ͸ͳ͍ʣ ɾJOIN

    ͠ͳ͍ύλʔϯ ɾඇਖ਼نԽ ɾඞཁͳσʔλΛ৑௕Խʢίϐʔʣ͢Δ ɾRDB Ͱ͸ߟ͑ʹ͍͕͘ NoSQL Ͱ͸Ұൠత <= ࠾༻ͯ͠ΈΔ
  11. 24.

    ඇਖ਼نԽʢίϐʔʣͷ࣮ݱํ๏ ɾTransaction or Batch Write ɾAtomic ͳॻ͖ࠐΈΛ࣮ݱ ɾ࠷େ 500 ݅ͷ੍ݶ͋Γ

    ɾFirestore Trigger ɾ݁Ռ੔߹ੑ ɾΤϥʔॲཧɺϦτϥΠॲཧͷඞཁੑ
  12. 25.
  13. 32.

    CQRS (Command Query Responsibility Segregation) Command (ߋ৽ܥ) ͱ Query (ࢀরܥ)

    ͰػೳΛ෼ׂ DDD ͷจ຺ͰޠΒΕΔ͜ͱ͕ଟ͍ ES (Event Sourcing) ʮঢ়ଶʯͰ͸ͳ͘ʮΠϕϯτʯΛه࿥͍ͯ͘͠ߟ͑ํ ೚ҙͷঢ়ଶ͸ΠϕϯτΛ࠶ੜ͢Ε͹࠶ݱͰ͖Δ CQRS ͱ૊Έ߹ΘͤͰΑ͘ޠΒΕ͍ͯΔ
  14. 37.
  15. 41.

    ·ͱΊ - Firestore + Functions Ͱ CQRS ΛऔΓೖΕͨ σʔλϞσϦϯάΛ࣮૷ -

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