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

DB外の副作用をトランザクションから分離しよう / Isolate out-of-DB side effects from transactions

DB外の副作用をトランザクションから分離しよう / Isolate out-of-DB side effects from transactions

gotanda.rb#52@オンライン "DB外の副作用をトランザクションから分離しよう"

Masato Ohba

April 20, 2023
Tweet

More Decks by Masato Ohba

Other Decks in Programming

Transcript

  1. %#֎ͷ෭࡞༻Λ
    τϥϯβΫγϣϯ͔Β෼཭͠Α͏

    !HPUBOEBSC
    PICBSZF

    View full-size slide


  2. τϥϯβΫγϣϯͷ"UPNJDJUZ
    ෼͚Δ͜ͱͷͰ͖ͳ͍Ұ࿈ͷ৘ใॲ
    ཧͷ୯Ґɻ͜͜Ͱ͸3%#.4ͷػೳΛ
    Πϝʔδ͢Δɻ
    τϥϯβΫγϣϯ಺ͷॲཧ͕શ࣮ͯ
    ߦ͞ΕΔ͔ɺ͋Δ͍͸·࣮ͬͨ͘ߦ
    ͞Εͳ͍͔Λอূ͠ͳ͚Ε͹ͳΒͳ
    ͍ʢ"UPNJDJUZݪࢠੑʣɻ
    τϥϯβΫγϣϯͱ͸ʁ
    ஫จσʔλͷ࡞੒
    ഑ૹσʔλͷ࡞੒
    Λ·ͱΊͯߦ͏

    View full-size slide


  3. τϥϯβΫγϣϯͷ"UPNJDJUZ
    τϥϯβΫγϣϯ಺ʹσʔλϕʔε
    ͕อޢͰ͖ͳ͍෭࡞༻ΛಥͬࠐΉ
    ϒϩοΫ಺ͷॲཧ͕શ࣮ͯߦ͞ΕΔ
    ͔ɺ͋Δ͍͸·࣮ͬͨ͘ߦ͞Εͳ͍
    ͔Λอূ͢Δ͜ͱ͕Ͱ͖ͳ͘ͳΔ
    "UPNJDJUZͷյ͠ํ
    ஫จ׬ྃϝʔϧ
    ૹ৴Λ௥Ճ
    ϝʔϧ͸ωοτϫʔΫΛܦ༝ͯ͠ૹ৴͞ΕΔ΋ͷͱ͢Δ

    View full-size slide


  4. ͳͥյΕΔʁ
    ʮ஫จσʔλ࡞੒ʯ
    ʮ഑ૹσʔλ࡞੒ʯ
    ʮϝʔϧૹ৴ʯ
    ͢΂͕ͯҰ؏ͯ͠੒ޭ͢Ε
    ͹Α͍
    ΫϥΠΞϯτ αʔό ϝʔϧαʔό
    ੒ޭ

    View full-size slide


  5. ͳͥյΕΔʁ
    )551ϦΫΤετͰωοτ
    ϫʔΫΤϥʔ͕ى͖ɺϩʔϧ
    όοΫ͢Δέʔε
    Ϣʔβʔʹ஫จ׬ྃϝʔϧ
    ͕ૹΒΕͨͷʹ஫จɾ഑ૹ
    σʔλ͕ଘࡏ͠ͳ͍ͱݴͬ
    ͨ໰୊͕ى͖͏Δ
    💥
    αʔόͰૹ৴੒൱Λ
    ஌Δ͜ͱ͸Ͱ͖ͳ͍
    ωοτϫʔΫΤϥʔ͸ඞͣى͖ΔIUUQTFOXJLJQFEJBPSHXJLJ'BMMBDJFT@PG@EJTUSJCVUFE@DPNQVUJOH

    ଞʹ΋ίϛοτͰࣦഊ͢ΔέʔεͳͲ͕͋Γ͏Δ
    ΫϥΠΞϯτ αʔό ϝʔϧαʔό

    View full-size slide


  6. ໰୊ͷຊ࣭
    w ʮ஫จσʔλ࡞੒ʯʮ഑ૹσʔλ࡞੒ʯʮϝʔϧૹ৴ʯΛͭͷ࿦
    ཧτϥϯβΫγϣϯʹ͍ͨ͠
    w ͔͠͠3%#.4ͷ෺ཧτϥϯβΫγϣϯͰ͸αϙʔτͰ͖ͳ͍
    w ݎ࿚ੑΛอͭʹ͸%#τϥϯβΫγϣϯͱɺ%#֎ͷ෭࡞༻͸෼཭͠
    ͳ͚Ε͹ͳΒͳ͍
    ෼ࢄτϥϯβΫγϣϯΛؤுΔɺͱ͍͏ಓ΋͋Δ͕ଟ͘ͷέʔεͰ͸ίετ͕ߴͭ͘͘ͱࢥΘΕΔ

    ͜ͷଞʹ΋τϥϯβΫγϣϯͷ௕࣌ؒԽɺϩοΫղ์଴ͪͳͲͷ໰୊΋ى͖Δ

    View full-size slide


  7. 4NBSU#BOLͷࣄ৘
    w δϣϒΩϡʔγεςϜʹEFMBZFEKPCΛ࢖͍ͬͯͨ
    w Ωϡʔͱͯ͠3%#.4Λ࢖͏
    w ΤϯΩϡʔ·ͰͷBUPNJDJUZΛอূͰ͖ΔͨΊɺઌड़ͷྫͷΑ͏ʹ
    %#τϥϯβΫγϣϯ಺ͰΤϯΩϡʔ͍ͯ͠ΔՕॴ͕͋ͬͨ
    w 424ʹҠߦ͢Δ͜ͱʹͳΓɺݕग़ͯ͠෼཭͢Δඞཁ͕ੜͨ͡
    IUUQTHJUIVCDPNDPMMFDUJWFJEFBEFMBZFE@KPC

    View full-size slide

  8. ͳΜ͔࡞Εͦ͏ͳؾ͕ͨ͠ͷͰ
    ࣗ࡞ͯ͠ΈΔ

    View full-size slide


  9. ݕग़ʢࣗ࡞Ҋʣ
    w "DUJWF3FDPSE#BTFDPOOFDUJPOUSBOTBDUJPO@PQFO
    ΍PQFO@USBOTBDUJPOTͰτϥϯβΫγϣϯ಺͔Ͳ͏͔൑ఆͰ͖Δ
    w )551ϦΫΤετʢ/FU)551
    OFXʣ΍δϣϒΤϯΩϡʔ
    ʢ"DUJWF+PC&ORVFVJOHFORVFVFʣͰݺͼग़͞ΕΔϝιουʹ൑
    ఆϑοΫΛ࢓ࠐΊ͹Α͍
    τϥϯβΫγϣϯ಺͔Ͳ͏͔ಈతʹ൑ఆ͢Δํ๏

    View full-size slide


  10. ݕग़ʢࣗ࡞Ҋʣ
    ࠷ॳʹࢼΈͨࣗ࡞ྫ
    τϥϯβΫγϣϯதʹݺͼग़͢ͱ
    raiseͯ͘͠ΕΔ

    View full-size slide


  11. ෼཭ʢࣗ࡞Ҋʣ
    w ஍ಓʹ%#τϥϯβΫγϣϯͷ֎ʹҠ͢
    w "DUJWF3FDPSE#BTFΛܧঝͯ͠ΔΫϥεͰ͋Ε͹BGUFS@DPNNJUϑο
    ΫΛ࢖͏
    τϥϯβΫγϣϯ͔Β෼཭͢Δํ๏

    View full-size slide

  12. ࣗ࡞͸Ͱ͖ͦ͏͚ͩͲ
    ΑΓચ࿅͞ΕͨHFN͕͋Δ͜ͱʹؾͮ͘

    View full-size slide


  13. ݕग़ʢHFNʣ
    τϥϯβΫγϣϯ಺ͷ෭࡞༻Λݕग़
    ͯ͘͠ΕΔ
    "DUJWF+PC͚ͩͰͳ͘4JEFLJR΍
    3FTRVFΛαϙʔτ
    )551ϦΫΤετͷ؂ࢹʹ͸4OJ
    ff
    FS
    Λ࢖༻͠ɺ/FU)551Ҏ֎ͷϥΠϒ
    ϥϦ΋αϙʔτ
    *TPMBUPS
    IUUQTHJUIVCDPNQBMLBOJTPMBUPS

    View full-size slide


  14. ݕग़ʢHFNʣ
    "DUJWF4VQQPSU/PUJ
    fi
    DBUJPOTVCTDSJCF
    ͰTRMBDUJWF@SFDPSEΠϕϯτΛ
    TVCTDSJCF͢ΔMJTUFOFSΛ࣮૷͠ɺHFNͷ
    ίʔυ಺ͰτϥϯβΫγϣϯ؅ཧΛͯ͠
    ͍Δ
    VTF@USBOTBDUJPOBM@UFTUT΋αϙʔτͯ͠
    ͍Δ
    💭JOTUSVNFOUBUJPOରԠ͍ͯ͠Ε͹"3Ҏ
    ֎Ͱ΋࢖͑ΔΑ͏ʹ͍ͯ͠Δ
    *TPMBUPS
    IUUQTHJUIVCDPNQBMLBOJTPMBUPS

    View full-size slide


  15. ෼཭ʢHFNʣ
    BGUFS@DPNNJU BGUFS@SPMMCBDLΛίʔ
    υϕʔεͷͲ͜ʹͰ΋ॻ͚ΔΑ͏ʹ
    ͯ͘͠ΕΔ
    💭ϏδωεϩδοΫΛू໿ͨ͠··
    τϥϯβΫγϣϯ෼཭Ͱ͖Δͷ͕ͱ
    ͯ΋ྑ͍
    "GUFS$PNNJU&WFSZXIFSF
    IUUQTHJUIVCDPN&OWFLBGUFS@DPNNJU@FWFSZXIFSF

    View full-size slide


  16. ෼཭ʢHFNʣ
    "DUJWF3FDPSE$POOFDUJPO"EBQUFST%BUBCB
    TF4UBUFNFOUTBEE@USBOTBDUJPO@SFDPSEʹϞ
    σϧ෩ͷΦϒδΣΫτΛ౉͠ɺτϥϯβΫ
    γϣϯίϛοτޙͷίʔϧόοΫΛݺ͹ͤͯ
    ͍Δ
    💭ͦΜͳ͜ͱͰ͖ΔΜͩ
    "GUFS$PNNJU&WFSZXIFSF

    View full-size slide


  17. ͜ΕΒΛ૊Έ߹Θͤͯ
    ίʔυϕʔε্Ͱ͢΂ͯͷ෭࡞༻Λ
    %#τϥϯβΫγϣϯ͔Β෼཭׬ྃ✅

    View full-size slide


  18. ·ͱΊ
    *TPMBUPS "GUFS$PNNJU&WFSZXIFSFศར
    📡 *TPMBUPS͸FOWJSPONFOUUFTUͰಥͬࠐΜͰҰ౓$*૸ΒͤͯΈΔͱίʔυϕʔεͷ݈શ
    ੑ͕Θ͔ΔͷͰΦεεϝɻฐࣾͩͱՕॴ͔͠ͳ͘ɺΑ͘Ͱ͖ͯͯײಈͨ͠
    👐 طଘͷHFNΛ࢖͏࣌΋ʮࣗ࡞͢ΔͳΒͲ͏͢Δ͔ʁʯΛߟ͑Δͱ໘ന͍
    👁 ͦͷ্ͰίʔυϦʔσΟϯά͢Δͱ৽ͨͳൃݟ͕ੜ·Εͯ͞Βʹ໘ന͍

    3BJMTͷJOTUSVNFOUBUJPO΍DBMMCBDLػߏʹ͍ͭͯ஌Δ͜ͱ͕Ͱ͖ͨ

    View full-size slide


  19. 4NBSU#BOL *OD
    4PGUXBSF&OHJOFFS
    .BTBUP0ICB
    !PICBSZF
    8IP*BN
    !PICBSZF

    View full-size slide