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

安全かつ高速に進めるマイクロサービス化 / railsdm2018

安全かつ高速に進めるマイクロサービス化 / railsdm2018

Rails Developers Meetup 2018
https://railsdm.github.io/

Takashi Kokubun

March 24, 2018
Tweet

More Decks by Takashi Kokubun

Other Decks in Programming

Transcript

  1. ࣗݾ঺հ • GitHub, Twitter: @k0kubun • Treasure Data ॴଐ •

    Site Reliability Engineer • Ruby 2.6ͷJITίϯύΠϥ։ൃத
  2. Treasure Data Ͱͷ1೥ؒ • 2017 1Q: TD APIͷ։ൃ • 2017

    2Q: bigdam-queue • 2017 3Q: Fluentd Enterprise • 2017 4Q: Embulk Config Microservice • 2018 1Q: SRE: AutoScaling, Deployment
  3. ओͳςετͷબ୒ࢶ • ϝιουݺͼग़͠୯ҐͰڥքΛશͯελϒ͢Δ • VCRͰ֎෦ͷϦΫΤετΛશͯελϒ͢Δ • ಈ͔͢ίϯϙʔωϯτΛશͯ༻ҙͯ݁͠߹ςετ • Pact Ͱ

    Consumer Driven Contract Testing ࢀߟ: https://medium.com/@copyconstruct/testing-microservices-the-sane-way-9bb31d158c16
  4. ओͳςετͷબ୒ࢶ • ϝιουݺͼग़͠୯ҐͰڥքΛશͯελϒ͢Δ • VCRͰ֎෦ͷϦΫΤετΛશͯελϒ͢Δ • ಈ͔͢ίϯϙʔωϯτΛશͯ༻ҙͯ݁͠߹ςετ • Pact Ͱ

    Consumer Driven Contract Testing ࢓༷มߋΛ͢Δਓͨͪʹ͜ΕΛֶशͯ͠΋Β͏ίετ͕ߴ ͗͢Δ + Pact Brokerͷ༻ҙ΍ӡ༻΋ඞཁͳͷͰɺҰճͷ ੾Γग़͠Ͱಋೖ͢Δʹ͸ΦʔόʔΩϧ (௕ظతʹ͸ཉ͍͠)
  5. ੾Γग़͠ݩΞϓϦͷrspecϓϩηε spec_helper.rb at_exit ڞ༗DB (ޙͰ੾Γ཭͢) ࠓճ࢖༻ͨ͠ςετख๏: ੾Γग़ͨ͠ΞϓϦ Λىಈͯ͠ϢχοτςετΛ૸ΒͤΔ *_spec.rb FactoryBot

    ੾Γग़ͨ͠RailsΞϓϦ ͷϓϩηε ੾Γग़͠ݩ RailsΞϓϦ ੾Γग़ͨ͠DB DBͷ੾Γ཭͠ʹ൐͍FactoryBotΛଔۀ ͔ΘΓʹAPIͰϦιʔεΛ࡞Δ
  6. ͜ͷख๏ͷྑ͍ͱ͜Ζɺѱ͍ͱ͜Ζ • ྑ͍ͱ͜Ζ • ଞͷਓ͕มߋͨ͠ςετΛ΄΅ͦͷ··࢖͑ɺͱʹ͔͘࢓༷มߋʹڧ͍ • Կ΋ελϒ͠ͳ͍͍ͯ͘ͷͰςετΛॻ͘ͷָ͕ • ѱ͍ͱ͜Ζ •

    ੾Γग़͠ݩͷCIͷ͕࣌ؒ৳ͼΔ • ੾Γग़ͨ͠ΞϓϦΛىಈ͢ΔखஈΛϝϯς͢Δίετ͕͔͔Δ • ੾Γग़ͨ͠ΞϓϦ಺ͰյΕͨ࣌ͷσόοάํ๏͕গ͠มΘΔͷͰ໘౗
  7. Circuit BreakerύλʔϯͷͨΊͷϥΠϒϥϦ • circuit_breaker.gem • γϯϓϧɺ͚ͩͲmix-inͳͷͰݸਓతʹ͸͋·Γ࢖͍ͨ͘ͳ͍ • expeditor.gem • Circuit

    Breaker͚ͩͰͳ͘ɺϦτϥΠɺλΠϜΞ΢τɺࣦഊ࣌ ͷϑΥʔϧόοΫɺඇಉظԽͳͲ΋࣮૷ • ਖ਼௚expeditorʹඇಉظԽ͸͍Βͳ͔ͬͨΜ͡Όͳ͍͔ͱࢥͬͯ Δ͚ͲɺฐࣾͰ͸makimoto͞Μͱ๻͕ৄ͍͠ͷͰ͜ΕΛ࠾༻
  8. ϦτϥΠઓུ • exponential backoffΛ͚ͭɺແཧʹෛՙΛ͔͚ͳ͍Α͏ʹ • 5XXͰ΋ɺϦτϥΠՄೳͳ͜ͱ͕໌Β͔Ͱͳ͍Τϥʔ͸ઈରϦτϥΠ͠ͳ͍ • ΫϥΠΞϯτଆͷϫʔΧʔΛແବʹϒϩοΫͨ͠Γɺαʔόʔଆͷো֐࣌ʹෛՙΛ͔͚Α Γঢ়گΛѱԽͤͨ͞Γ͢ΔͨΊ •

    ྫ͑͹500͸NoMethodErrorͱ͔ͩͬͨΓ͢ΔͷͰϦτϥΠ͢΂͖Ͱͳ͍ • ϦτϥΠࠐΈͷશମͷλΠϜΞ΢τͱɺ֤ϦτϥΠͷλΠϜΞ΢τΛઃ͚Δ • ҰॠͰࢮ͵ΤϥʔΛΑΓଟΊʹϦτϥΠ͢ΔͨΊ • ͲͷΤϥʔ͕ϦτϥΠՄೳ͔͸APIʹΑͬͯҟͳΔ • ֤HTTPϝιου͝ͱʹ᠟౳ੑʹؔ͢ΔڍಈΛαʔόʔଆͰ౷Ұ͓ͯ͘͠ͱָ
  9. RailsͰසൟʹ࡞ΒΕͦ͏ͳҎԼͷύλʔϯͰ APIαʔόʔ/ΫϥΠΞϯτΛͲ͏࣮૷͢Δ͔ • GET /embulk_configs (index) • GET /embulk_configs/:id (show)

    • POST /embulk_configs (create) • PATCH /embulk_configs/:id (update) • DELETE /embulk_configs/:id (destroy) APIͩͱ #new, #edit ͸࡞Γ·ͤΜΑͶ?
  10. GET /embulk_configs (index) • αʔόʔ • ౰ͨΓલ͚ͩͲGET͸શͯϩάҎ֎ͷ෭࡞༻͕ͳ͍Α͏ʹ͢Δ • ΫϥΠΞϯτ •

    ΄΅ϦτϥΠ͠์୊͚ͩͲɺલड़ͷཧ༝ʹΑΓ404, 422, 500 ͱ͔͸΍Ί͓͖ͯ·͠ΐ͏ • αʔόʔ͕མͪͯΔܥ(502, 503)ͱΫϥΠΞϯτͰλΠϜΞ΢ τͨ͠ܥΛϦτϥΠ͢Δ
  11. POST /embulk_configs (create) • αʔόʔ • ࡞੒ର৅ͷϦιʔεʹunique index͕͋Δ: RecordNotUnique࣌ͷϨεϙϯεΛ ۠ผՄೳʹ͢Δ

    (409ฦ͢ͱ͔) • ͳ͍: ϦΫΤετIDΈ͍ͨͳ΋ͷΛΫϥΠΞϯτʹ༻ҙͤ͞ɺͦΕΛ͚࣋ͭͩͷ ςʔϒϧͷϨίʔυͱ࡞੒ର৅ͷϦιʔεΛτϥϯβΫγϣϯͰίϛοτ͢Δ • ΫϥΠΞϯτ • 409 Conflictͷ৔߹͸ϦτϥΠ͠ͳ͍ • ϦΫΤετIDΛ༻ҙ͢Δ৔߹͸SecureRandom.uuidͱ͔Λ༻ҙ͠ɺϦτϥΠͷ ؒͰ͸ಉ͡஋͕౉ΔΑ͏ʹ͢Δ 409ͷ࿩ͷݩωλ: http://frsyuki.hatenablog.com/entry/2014/06/09/164559
  12. PATCH /embulk_configs/:id (update) • αʔόʔ • ؾ߹Ͱ᠟౳ʹ͢Δ • ΞϓϦέʔγϣϯ࣍ୈ͕ͩɺ͘͝γϯϓϧͳཁ݅Ͱ͋Ε͹ ᠟౳ʹͳΔΑ͏ʹ࣮૷͢Δͷ͸೉͘͠ͳ͍

    • ΫϥΠΞϯτ • ᠟౳ʹ͑͞ͳ͍ͬͯΕ͹ɺGETͱ͔ͱϦτϥΠ͢΂͖Τ ϥʔ͸มΘΒͳ͍ (ͳͬͯͳ͍ͳΒৗʹϦτϥΠ͠ͳ͍) UPDATEͷ᠟౳ੑʹؔͯ͠͸ͪ͜Β΋ࢀߟʹ: http://frsyuki.hatenablog.com/entry/2017/08/10/140416
  13. DELETE /embulk_configs/:id (destroy) • αʔόʔ • ඞͣ͠΋ϦτϥΠͰਖ਼ৗͳϨεϙϯε͕ظ଴Ͱ͖Δํ๏Ͱ͸ͳ͍͕ɺී௨ʹfind͠ ͯdestroyͨ͠ΒϦιʔεʹର͢Δૢ࡞͸গͳ͘ͱ΋᠟౳ʹͳΔ • Ͳ͏ͯ͠΋᠟౳ʹ͍ͨ͠ͳΒɺ࿦ཧ࡟আ͔ͭաڈʹϨίʔυ͸ଘࡏ͚ͨ͠Ͳ࡟আ

    ࡁͰ͋Δ͜ͱ͕Θ͔Δ৔߹͸ɺ੒ޭΛฦͯ͠΋͍͍͔΋ • ෺ཧ࡟আͷ৔߹͸ͦ΋ͦ΋࡞ΒΕ͍ͯͳ͔ͬͨ৔߹ͱ൑ผͰ͖ͳ͍ͷͰ404 • ΫϥΠΞϯτ • ্هͷڍಈͰ͋Ε͹GETͱಉ͡ΤϥʔΛϦτϥΠͯ͠Ε͹͋·ΓࠔΒͳ͍͸ͣ • طʹ࡟আࡁͷ৔߹(404)͸ϦτϥΠ͠ͳ͍
  14. ੾Γग़͠ઌʹ͍͚࣋ͬͯͳ͍ ΞιγΤʔγϣϯ • ྫ͑͹੾Γग़ͦ͏ͱ͍ͯ͠ΔϞσϧ͕ belongs_to :user Λ࣋ͭ৔ ߹ • ֎෦ʹݟ͑Δܗͱͯ͠αʔϏε͕෼͔Ε͍ͯΔ৔߹͸usersςʔ

    ϒϧͷߋ৽ΛPubSubͰ௨஌ͯ͠੾Γग़͠ઌʹίϐʔΛ࣋ͭͱ͔ • ͋Δ͍͸ɺDBΛ෼཭͢Δ౎߹Ͳ͏΍ͬͯ΋Ұ؏ੑ͸ଛͳΘΕΔ ͷͰɺׂΓ੾ͬͯuser_id͚ͩ΍ΓऔΓ͢Δ౳ • JOINͰ͖ͳ͍͠ΧϥϜͷ஋ͷҙຯ͸ෆ໌ʹͳΔ͕ɺߏ੒͸ γϯϓϧʹอͯΔ
  15. JOINͷඞཁͳιʔτ • A.belongs_to :b ͰBΛผαʔϏεʹ੾Γग़͍͕ͨ͠ɺb.nameͰAΛ ιʔτ͢ΔAPI͕͋Δ৔߹ • ࢮ͋ΔͷΈ • ͦ΋ͦ΋ͦΜͳΠϯλʔϑΣʔεΛ࡞ΔͷΛ΍ΊΖ

    • DBͷ஋ͷҰ؏ੑΛ͋Δఔ౓͖͋ΒΊɺb.nameͷίϐʔͰ͋Δ a.b_nameΛੜ΍ͯ͠ϝϯς͢Δ͔͠ͳ͍ • ྫ͑͹ιʔτʹa.b_nameΛ࢖͏͕஋͸b.nameΛฦ͢ͳͲ͢Δɻ ͨ·ʹॱং͕͓͔͍͠ͷΛڐ༰Ͱ͖ͳ͍ͳΒ੾Γग़͠͸ఘΊΔ
  16. ؾܰʹղܾͰ͖ͳ͘ͳΔN+1໰୊ • ActiveRecordͷΫΤϦΛAPIݺͼग़͠ʹͨ͠ॠؒ includes/preload/eager_load ͕࢖͑ͣਏ͍໰୊ • ઈରʹ΍ΊΑ͏: ࢥߟఀࢭΩϟογϡ • ී௨ʹidͷϦετΛऔͬͯฦ͢APIΛੜ΍͠ɺฦ͖ͬͯͨ

    ࢠϦιʔεΛ਌ϦιʔεͷΠϯελϯεม਺ʹ২͑Δ͔ɺ { ਌Ϧιʔε => ࢠϦιʔε } ͳHashΛ࡞Δ • ΍͍͔ͬͯ͘͠ͳ͍͕ɺ͜ͷ໰୊͸ൺֱత؆୯ͳํ
  17. ਓྨͷσϓϩΠͷจ໌ͷൃల • ssh + git pull σϓϩΠ (capistrano) • ΞʔΧΠϒΛpull͢ΔλΠϓͷσϓϩΠ

    • DockerίϯςφͷσϓϩΠ • ίϯςφΫϥελ΁ͷσϓϩΠ (ECS, k8s) զʑ͸ݱࡏͲ͔ͬͪΛ ਐΊͯΔঢ়ଶ