$30 off During Our Annual Pro Sale. View Details »

Scalebaseバックエンド構成について/the backend design of Scalebase

Scalebaseバックエンド構成について/the backend design of Scalebase

ma2k8
PRO

July 29, 2021
Tweet

More Decks by ma2k8

Other Decks in Technology

Transcript

  1. Scalebase όοΫΤϯυߏ੒ʹ͍ͭͯ

  2. Agenda 1ɽઃܭࢥ૝ 2ɽsbtϓϩδΣΫτߏ੒ 3ɽϞδϡϥϞϊϦε 4ɽEff

  3. ઃܭࢥ૝ 33

  4. ഇΕͳ͍ίʔυ͸ͳ͍ͷͰపఈͨ͠ϞδϡʔϧԽ ελʔτΞοϓʹݶΒͣɺ࠷ॳظͷίʔυ͕ෛ࠴Խ͠ɺͦͷվमʹ༏लͳਓࡐ͕ଟ େͳ࿑ྗΛඅ΍͍ͯ͠Δέʔε͸ଟ͍ɻ ͲΜͳʹྑ͍ίʔυ΋࣌੎΍࢓༷ͷมԽʹΑΓ௠෗Խ͢ΔՄೳੑ͕͋Δ͕ɺӨڹൣ ғΛߏ଄Ͱ੍ޚ͢Δ͜ͱʹΑͬͯলྗͰͷஔ͖׵͕͑Ͱ͖ΔΑ͏ʹsbtͷαϒϓϩ δΣΫτΛࡉ͔͘੾Δબ୒͍ͯ͠Δɻ ϞδϡʔϧԽͷࢦ਑͸CleanArchitecture ୯ʹࡉ͔͘੾Δ͚ͩͰ͸ແடংͰอकෆՄೳʹͳΔͷͰɺCleanArchitecture Λࢀߟʹ͠ɺґଘํ޲ͷ੍ޚ(DIP)ʹΑٕͬͯज़తؔ৺ࣄͱυϝΠϯϩδοΫͷ෼ ཭Λߦ͍ͬͯΔɻ

    ௒େن໛ΞϓϦέʔγϣϯΛݎ࿚͔ͭ҆શʹ࡞Δ DI DI͸ґଘੑ੍ޚͷDI(Guice)ͱEffectͷDI(Eff)ͷ2छྨͰߦ͍ͬͯΔɻ Guice(ಈతDI)͸ɺେن໛Λ૝ఆ͍ͯ͠Δނʹauto wireͳ஫ೖ͕Ͱ͖ͳ͍ͱϘ ΠϥϓϨʔτͷྔ͕๲େʹͳΔͨΊ࠾༻͍ͯ͠Δɻ(ςετͰDIίϯςφ෇͖ͷى ಈΛߦ͓ͬͯΓɺઃఆ࿙Ε,ॳظԽΤϥʔͳͲͷϥϯλΠϜΤϥʔ͸ͦ͜Ͱݕ஌͠ ͍ͯΔɻ) EffectͷDI͸ɺখճΓͷޮ࣮͘ߦ࣌ͷࠩ͠ସ͑Λ੩తʹߦ͏ͨΊEffͷΠϯλϓ Ϧλʹد͍ͤͯΔɻ EffͷγϯϓϧͳγϯλοΫεʹΑΔมߋ༰қੑ ੩తܕ෇͚ݴޠ͸ݎ࿚͕ͩෳࡶͳ͜ͱΛ͢ΔͱϩδοΫͱ͸ؔ܎ͷͳ͍ܕ߹Θͤͱ ෇͖߹͏͜ͱʹͳΔɻ ؔ਺ܕϓϩάϥϛϯάͷΤοηϯε͸͜ͷܕ߹ΘͤΛָʹ͠ɺදݱྗΛ֦ு͢Δଆ ໘Λ࣋ͭͷͰɺՄಡੑͱͷόϥϯεΛݟͳ͕Βੵۃతʹಋೖ͍ͯ͠Δɻ υϝΠϯϞσϧͷ୳ٻ ෳࡶͳυϝΠϯΛදݱ͢ΔυϝΠϯϞσϦϯάΛ୳ٻ͍ͯ͠Δɻ ٕज़తؔ৺ࣄ͔Β੾Γ཭͞ΕͨϞσϧ͕ͩɺ۪௚ʹϞσϦϯά͢Δͱڊେͳू໿͕ ݱΕͯ͠·͏ͷͰɺ1ͭͷ֓೦ʹΈ͑Δ΋ͷΛద੾ͳ෼ׂ୯Ґʹ෼͚͍ͯ͘ ܖ໿ද໌ ओʹΫϥεෆมද໌Λར༻ͯ͠ɺcase class͚ͩͰ͸දݱͰ͖ͳ͍ଘࡏͯ͠͸ͳ Βͳ͍ঢ়ଶΛݕ஌Ͱ͖ΔΑ͏ʹ͢Δ
  5. SBTϓϩδΣΫτߏ੒ 53

  6. 1ίϯςΩετʹ͓͚ΔSBTϓϩδΣΫτߏ੒ SecondaryAdapter૚ UseCase૚ Domain૚ Http ڞ௨Http ڞ௨SecondaryAdapter૚ ڞ௨Library૚ ڞ௨Domain૚ Batch

    ڞ௨Batch Kinesis ڞ௨Kinesis gRPC ڞ௨gRPC 1αʔϏεͷϓϩδΣΫτ͸ɺ PrimaryAdapter,SecondaryAdapter,UseCase,Doma in͕جຊͰ࣮૷͢Δʹ͋ͨΓڞ௨Խͨ͠΄͏͕লྗͰศར ͳ΋ͷ͸શίϯςΩετԣஅͰࢀরͰ͖Δڞ௨xxx૚ʹ഑ஔ ͢ΔΑ͏ʹ͍ͯ͠Δɻ ͨͩɺকདྷϚΠΫϩαʔϏεԽͨ͠ࡍʹ͸ɺڞ௨૚ʹஔ͘΋ ͷΛ͍͡Δ౓ʹσϓϩΠಠཱੑΛ્֐ͯ͠͠·͏ͷͰɺجຊ తʹ͸มΘΓಘͳ͍΋ͷ(ID౳)΍ɺશαʔϏεҰׅͰมߋ ͢Δඞཁ͕͋Δ΋ͷ(ந৅Batch Class౳)Λ഑ஔ͢ΔΑ͏ ʹ͍ͯ͠Δɻ
  7. AuthN༻SecondaryAdapter૚ AuthN༻Domain૚ ෳ਺ίϯςΩετΛ၆ᛌͰݟͨਤ Subscription༻SecondaryAdapter૚ Subscription༻UseCase૚ Subscription༻Domain૚ Subscription༻PrimaryAdapter૚ Subscription AuthZ༻SecondaryAdapter૚ AuthZ༻UseCase૚

    AuthZ༻Domain૚ AuthZ༻PrimaryAdapter૚ ڞ௨PrimaryAdapter૚ ڞ௨SecondaryAdapter૚ ڞ௨Library૚ ڞ௨Domain૚ AuthZ AuthN༻UseCase૚ AuthN༻PrimaryAdapter૚ AuthN ɾɾɾetc
  8. ϞδϡϥϞϊϦε ৄࡉ: https://speakerdeck.com/showmant/expressing-complex-domain-regions-and-boundaries-with-modular-monoliths AuthN༻SecondaryAdapter૚ AuthN༻Domain૚ Subscription༻SecondaryAdapter૚ Subscription༻UseCase૚ Subscription༻Domain૚ Subscription༻PrimaryAdapter૚ Subscription

    AuthZ༻SecondaryAdapter૚ AuthZ༻UseCase૚ AuthZ༻Domain૚ AuthZ༻PrimaryAdapter૚ ڞ௨PrimaryAdapter૚ ڞ௨SecondaryAdapter૚ ڞ௨Library૚ ڞ௨Domain૚ AuthZ AuthN༻UseCase૚ AuthN༻PrimaryAdapter૚ AuthN ɾɾɾetc ModularMonolith༻PrimaryAdapter૚
  9. ϞδϡϥϞϊϦε 93

  10. Ώ͘Ώ͘͸ϚΠΫϩαʔϏεԽ͍ͯ͘͠ ґଘϥΠϒϥϦͷ؅ཧ΍σϓϩΠස౓ͳͲ͕͋·Γʹ΋ണ཭ͨ͠ίϯςΩετ͕ొ ৔͢ΔͱϚΠΫϩαʔϏεԽ͠ͳ͍ͱπϥΠ͜ͱʹͳ͍ͬͯ͘ͷͰͦΕʹඋ͑ͨߏ ੒Λ૊Ή େن໛͔ͭυϝΠϯ͕ෳࡶ େن໛͔ͭυϝΠϯ͕ෳࡶͩͱɺࡉ෼Խ͠ͳ͍ͱ͢΂͕ͯڊେू໿ʹͳΓ͕ͪͰɺ ͦ͏͠ͳ͍Α͏ʹ༻్͝ͱʹू໿Λ෼͚ͯ΋ɺେྔͩͱɺͲͷΑ͏ʹ࢖ΘΕΔͷ͔ ͕΅΍͚ͯ͠·͏ɻ ίϯςΩετʹΑΔॎͷ෼ׂʹΑͬͯɺ͜ͷೝ஌೉౓ΛԼ͛ΔޮՌΛૂ͍ͬͯΔɻ ͳͥϞδϡϥϞϊϦε͔

    ίϯςΩετڥքͷҙࣝ෇͚ ίΞυϝΠϯͱαϒυϝΠϯͷڥք͸ɺҙ͍ࣝͯͯ͠΋ڥքҾ͖͢Δ͜ͱ͕೉͍͠ ͷͰɺԿ͔͠Βͷ੍໿͕ͳ͍ͱ͙͢ʹࠞͬͯ݁͟߹ͯ͠͠·͏ɻ ڥքͷҙࣝΛڧ੍&໌֬ʹ͢Δ͜ͱʹΑͬͯΫϦʔϯͳυϝΠϯ͕อक͠΍͘͢ͳ ΓɺڥքҾ͖ͷࢼߦ΋͠΍͘͢ͳΔͷͰΑΓྑ͍ڥքΛಥ͖٧ΊΔ͜ͱ͕Ͱ͖Δɻ ͔ΜͨΜʹಋೖՄೳ ScalaͱSBTͷදݱྗͩͱɺ͔ΜͨΜʹಋೖ͢Δ͜ͱ͕Ͱ͖Δɻ ࢀߟ: https://speakerdeck.com/showmant/expressing-complex-domain-regions-and-boundaries-with-modular-monoliths
  11. Eff 11 3

  12. มߋ༰қੑ ෳ਺ͷޮՌΛϑϥοτʹऔΓѻ͏͜ͱ͕Ͱ͖ΔͷͰɺܕ߹ΘͤΛҙࣝͤͣʹϩδο Ϋͷมߋ͕Մೳɻ ΠϯλϓϦλʹΑΔॊೈͳڍಈࠩ͠ସ͑ Auto-wireͳDIίϯςφ͸ศརͰ͸͋Δ͕ɺϦΫΤετϕʔεͰͷڍಈࠩ͠ସ͑ ͳͲΛߦ͏ͷʹ͸ద͍ͯ͠ͳ͍ɻ EffͰ͋Ε͸ΠϯλϓϦλʹΑͬͯॊೈ͔ͭ҆શɺ༰қʹڍಈΛࠩ͠ସ͑Δ͜ͱ͕ Ͱ͖Δɻ ͳͥEff͔ FPΤοηϯεͱOOPΤοηϯεͷૄ݁߹ͳڠௐ

    Ϟφυɺࣗવม׵ɺܧଓͳͲͷίΞͳFPΤοηϯε͸͢΂ͯΠϯλϓϦλʹด͡ࠐ ΊΒΕΔͷͰɺϩδοΫΛॻ͘ਓ͕ҙࣝ͢Δඞཁ͕ͳ͍ɻ FP޷͖ͳਓ͸ΠϯλϓϦλΛੵۃతʹ৮ΕΔ͠ɺϩδοΫΛॻ͘ਓ͸FPͷศརͳ ෦෼͚ͩΛڗड͢Δ͜ͱ͕Ͱ͖Δɻ ଟछଟ༷ͳEffectͷடং͋Δ؅ཧ EffΛ࢖Θͳ͍Scala project͸ɺऔΓѻ͏Effect͕1ͭ૿͑Δ͝ͱʹࢦ਺ؔ਺ తʹܕ߹Θͤͷਏ͕͕͍͋ͬͯ͘͞ͷͰɺศརͳEffect͕͋ͬͯ΋ؾָʹ࢖͏͜ ͱ͕Ͱ͖ͳ͍ɻ EffͰ͋Ε͹Reader,Writer΍ಠࣗఆٛͷޮՌΛ͍͘Βಋೖͯ͠΋γϯλοΫε ʹӨڹ͠ͳ͍ɻ(࣮ߦ࣌ͷ഑ઢ໰୊͸͋Δ͕ɺ࣮ߦॱΛݻఆ͢Δ࢓૊ΈͰճආͯ͠ ͍Δ) ࢀߟ: http://okmij.org/ftp/Haskell/extensible/more.pdf https://tech.recruit-mp.co.jp/server-side/post-18728/ 
 https://halcat.org/scala/extensible/ ϩδοΫͷ໌֬ԽͱՄಡੑ ϑϥοτʹϩδοΫΛهड़͢Δ͜ͱ͕Ͱ͖ΔͷͰܕ߹ΘͤʹΑͬͯϩδοΫ͕΅΍ ͚Δ͜ͱ͕ͳ͘ɺUseCaseɺυϝΠϯϩδοΫͷՄಡੑΛߴ͘อͭͷ͕༰қͰ͋ Δɻ