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

Firestore で CQRS やってみた

Firestore で CQRS やってみた

E67da6dea537bb62b09d55f52be58872?s=128

Yuji Kinjo

May 28, 2019
Tweet

Transcript

  1. Firestore Ͱ CQRS ΍ͬͯΈͨ Firebase Meetup #13 @DeNA

  2. Kinjo Yuji iOS Engineer Twitter: @_yukin01 GitHub: @yukin01

  3. ɾσʔλϞσϦϯά͕೉͍͠ͱ͍͏࿩ ɾ࣮ࡍʹ΍ͬͯΈΔ ɾCQRS ͕࢖͑Δ͔΋͠Εͳ͍ ɾ·ͱΊ ͜Μͳ࿩Λ͠·͢

  4. ϦϨʔγϣϯγοϓͲ͏දݱ͍ͯ͠·͔͢ʁ

  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
  6. Sub collection Map (Array) or Sub Collection

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

    : n ɾ਌ࢠυΩϡϝϯτͷϥΠϑαΠΫϧ ɾΫΤϦ ɾॻ͖ࠐΈͷ੍ݶ document => 1 times/s collection => 500 times/s ɾetc.
  8. Firestore ͰͰ͖Δ͜ͱࣗମ૿͍͑ͯΔ ɾarrayUnion/array-contains ɾincrement ɾCollection Group ʢΩϟονΞοϓେมʣ

  9. ࠷ۙࢥ͏͜ͱ ϢʔεέʔεʢͲ͏͍͏σʔλΛऔಘɾߋ৽͍͔ͨ͠ʣ ʹΑͬͯ ࠷దͳσʔλߏ଄͕ܾ·Δʢ࠷దղ͸͋Δ => ߏ଄ʹΑͬͯ͸σʔλͷऔಘɾߋ৽͕ ඇৗʹ೉͘͠ͳΔ͜ͱ΋…

  10. νϟοτػೳͷσʔλߏ଄Λߟ͑Δ

  11. ͜Μͳײ͡

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

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

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

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

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

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

    ࠷৽ϝοηʔδ Conditions - ॴଐ͍ͯ͠ΔϧʔϜͷΈ - ౤ߘ࣌ؒͰιʔτ
  18. ·ͣ͸γϯϓϧʹ

  19. ɾϧʔϜ໊ɺ ΞΠίϯը૾ɺ ϝϯόʔ਺ ɾarray-contains ͷΫΤϦ ɾະಡ਺Ͳ͏͢Δ…ʁ

  20. ɾະಡ਺ ࠷৽ϝοηʔδ ɾཁ݅͸ຬͨͤΔ ɾWrite ͷ੍ݶ͕…" (unreadCount ʹूத͢Δ)

  21. ɾSub Collection Խ ɾཁ݅͸ຬͨͤΔ ɾ1ճͷΫΤϦͰ औಘͰ͖ͳ͍ Rooms => Members =>

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

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

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

    ɾFirestore Trigger ɾ݁Ռ੔߹ੑ ɾΤϥʔॲཧɺϦτϥΠॲཧͷඞཁੑ
  25. None
  26. ͚ͩ͜͜ݟΕ͹ྑ͍

  27. Trigger functions

  28. Trigger functions

  29. Trigger functions

  30. Batch Write

  31. ϐλΰϥεΠον

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

    ͰػೳΛ෼ׂ DDD ͷจ຺ͰޠΒΕΔ͜ͱ͕ଟ͍ ES (Event Sourcing) ʮঢ়ଶʯͰ͸ͳ͘ʮΠϕϯτʯΛه࿥͍ͯ͘͠ߟ͑ํ ೚ҙͷঢ়ଶ͸ΠϕϯτΛ࠶ੜ͢Ε͹࠶ݱͰ͖Δ CQRS ͱ૊Έ߹ΘͤͰΑ͘ޠΒΕ͍ͯΔ
  33. h"ps://www.slideshare.net/AmazonWebServices/ srv301op=mizing-serverless-applica=on-data-=ers-with-amazon-dynamodb

  34. https://www.slideshare.net/AmazonWebServices/ srv301optimizing-serverless-application-data-tiers-with-amazon-dynamodb

  35. Command ଆ͸Πϕϯτ͚ͩΛه࿥ (ཁ͸Create ͷΈ) => ׬ᘳʹ࣮ݱ͢Δͷ͸΍Ί͓ͯ͘ σʔλͷྲྀΕΛҰํ޲ʹ͢Δͱ͜ΖΛऔΓೖΕΔ

  36. ͜͏͍͏Πϝʔδ

  37. None
  38. ɾਖ਼نԽ ɾηΩϡϦςΟϧʔϧͰ Write ͷΈڐՄ ɾ΄΅ RDB ͷײ֮

  39. ɾඇਖ਼نԽ ɾηΩϡϦςΟϧʔϧͰ Read ͷΈڐՄ ɾΫϥΠΞϯτͷॲཧָ͕ ɾཁ݅มߋʹରԠ͠΍͍͢

  40. Trigger functions Command -> Query ͷҰํ޲

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

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