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. Firestore Ͱ CQRS ΍ͬͯΈͨ
    Firebase Meetup #13 @DeNA

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  6. Sub collection
    Map (Array) or Sub Collection

    View full-size slide

  7. ͍ΖΜͳཁૉͰܾ·ͬͯ͘Δʢઈରղ͸ͳ͍
    ɾଟॏ౓ => 1 : 1, 1 : n, n : n
    ɾ਌ࢠυΩϡϝϯτͷϥΠϑαΠΫϧ
    ɾΫΤϦ
    ɾॻ͖ࠐΈͷ੍ݶ
    document => 1 times/s
    collection => 500 times/s
    ɾetc.

    View full-size slide

  8. Firestore ͰͰ͖Δ͜ͱࣗମ૿͍͑ͯΔ
    ɾarrayUnion/array-contains
    ɾincrement
    ɾCollection Group
    ʢΩϟονΞοϓେมʣ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  17. ·ͣ͸γϯϓϧʹ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  23. ඇਖ਼نԽʢίϐʔʣͷ࣮ݱํ๏
    ɾTransaction or Batch Write
    ɾAtomic ͳॻ͖ࠐΈΛ࣮ݱ
    ɾ࠷େ 500 ݅ͷ੍ݶ͋Γ
    ɾFirestore Trigger
    ɾ݁Ռ੔߹ੑ
    ɾΤϥʔॲཧɺϦτϥΠॲཧͷඞཁੑ

    View full-size slide

  24. ͚ͩ͜͜ݟΕ͹ྑ͍

    View full-size slide

  25. Trigger functions

    View full-size slide

  26. Trigger functions

    View full-size slide

  27. Trigger functions

    View full-size slide

  28. ϐλΰϥεΠον

    View full-size slide

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

    View full-size slide

  30. h"ps://www.slideshare.net/AmazonWebServices/
    srv301op=mizing-serverless-applica=on-data-=ers-with-amazon-dynamodb

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  33. ͜͏͍͏Πϝʔδ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  37. ·ͱΊ
    - Firestore + Functions Ͱ CQRS ΛऔΓೖΕͨ
    σʔλϞσϦϯάΛ࣮૷
    - Command ͱ Query Ͱ׬શʹ෼཭
    - ϐλΰϥεΠονΛҰํ޲ʹͰ͖Δ
    - ͕
    - ঢ়ଶ͕ଟ͘ɺRead ͕සൟͳϢʔεέʔεͰ͸
    ༗ޮ͔΋

    View full-size slide