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

Rails6.0 での複数 DB 対応

Rails6.0 での複数 DB 対応

Yuta Horii

July 31, 2019
Tweet

More Decks by Yuta Horii

Other Decks in Programming

Transcript

  1. Ұൠతͳ Master / Slave ߏ੒ͷDB .BTUFS %# 3FQMJDB %# "QQ

    ϨϓϦέʔγϣϯ 8SJUF3FBE 3FBE0OMZ
  2. • ෳ਺ͷ Primary DB ͱͦΕͧΕʹରԠ͢Δ Replica DB • ՔಇதͷϞσϧͰͷࣗಈ઀ଓ੾Γସ͑ •

    HTTPಈࢺ΍ɺ௚ۙͷॻ͖ࠐΈʹԠͨ͡ Primary ͱ Replica ͷ ࣗಈ੾Γସ͑ • ෳ਺DBͷ࡞੒ɾ࡟আɾϚΠάϨʔγϣϯૢ࡞Λߦ͏ Rails Task αϙʔτ͞ΕΔ಺༰
  3. production: primary: database: primary_db user: root adapter: mysql primary_replica: database:

    secondary_db user: root_readonly adapter: mysql replica: true animals: database: animals_db user: root adapter: mysql migrations_paths: db/animals_migrate animals_replica: database: animals_secondary_db user: root_readonly adapter: mysql replica: true ෳ਺DB ͱ Replica ͷઃఆ
  4. • Replica DB ʹ͸ replica: true Λઃఆ͢Δ • ৽͘͠௥Ճͨ͠ DB

    ʹ͸ϚΠάϨʔγϣϯϑΝΠϧஔ͖ ৔ͷ migrations_paths Λઃఆ͢Δ • ΨΠυϥΠϯʹ͸ replica ͸ primary ͱಉ͡DB໊ͰϢʔ βʔΛ෼͚Δ΂͖ͱ͋Δ͕ɺผDB΋໰୊ͳ͘ઃఆͰ͖Δ ཁ఺
  5. ϞσϧͰͷࢦఆ class AnimalsBase < ApplicationRecord self.abstract_class = true connects_to database:

    { writing: :animals, reading: :animals_replica } end class ApplicationRecord < ActiveRecord::Base self.abstract_class = true connects_to database: { writing: :primary, reading: :primary_replica } end ֤Ϟσϧຖʹ্هͷઃఆΛߦ͏͜ͱ͸Ͱ͖Δ͕ɺυΩϡϝϯτ Ͱ͸୯ҰͷϞσϧʹઃఆΛهࡌ͠ɺͦΕΛܧঝ͢Δ͜ͱΛਪ঑ ※ DB ઀ଓ࣌ʹϞσϧͷΫϥε໊ຖʹίωΫγϣϯpool Λ࡞Δ ͷͰɺDB ΫϥΠΞϯτͷ઀ଓ਺Λফඅͯ͠͠·͏
  6. $ rails -T rails db:create rails db:create:animals Rails db:create:primary rails

    db:drop rails db:drop:animals Rails db:drop:primary rails db:migrate rails db:migrate:animals Rails db:migrate:primary Rails db:migrate:status Rails db:migrate:status:animals Rails db:migrate:status:primary Migrations
  7. • ैདྷͷ rails db:create Ͱ͸ primary ͱ animals ྆DB࡞੒͞Ε Δ

    • rails db:create:animals ͰࢦఆͷDBͷ࡞੒΋Մೳ • migrations_paths ͷϑΥϧμ໊ʹ͸DB໊Λ prefix ʹ͚ͭΔ ΂͖ • ෇͚ͳͯ͘΋ɺࢦఆ͞Ε͍ͯΕ͹໰୊ͳ͍Α͏Ͱ͢ ཁ఺
  8. • rails db:create Ͱ͸ replica: true ͷDB͸࡞੒͞Ε ͳ͍ • rails

    db:create:primary_replica ͸ΤϥʔʹͳΔ • ಉ༷ʹෳ਺DBͷઃఆ͕ͳ͍ database.yml Ͱ rails db:create:primary ΋ΤϥʔʹͳΔ μϝͩͬͨ͜ͱ
  9. ઀ଓͷ੾Γସ͑ʢΦʔτฤʣ config.active_record.database_selector = { delay: 2.seconds } config.active_record.database_resolver = ActiveRecord::Middleware::DatabaseSelector::Resolver

    config.active_record.database_resolver_context = ActiveRecord::Middleware::DatabaseSelector::Resolver::Session application.rb ʹ্هઃఆΛ௥Ճ͢Δ͚ͩ
  10. • POST / PUT / DELETE / PATCH ͷϦΫΤετ͸ primary

    • ॻ͖ࠐΈޙɺdelayʢσϑΥϧτ͸2ඵʣؒ͸ primary ͰҎ߱ ͸ replica ΁ • GET / HEAD ͸ delay ΑΓલʹॻ͖ࠐΈ͕ͳ͍৔߹͸ replica ΁ • ࣮૷ΛΈΔͱ session[:last_write] ͱϦΫΤετ࣌ࠁͷ͕ࠩ delay Λա͍͗ͯͳ͍͔νΣοΫ͢Δ͚ͩͷγϯϓϧͳ࣮૷ ཁ఺
  11. ઀ଓͷ੾Γସ͑ʢϚχϡΞϧฤʣ ActiveRecord::Base.connected_to(role: :reading) do # do something end ίʔυϒϩοΫ಺ͰɺroleΛࢦఆͯ͠ɺΫΤϦΛൃߦ͢Δ ActiveRecord::Base.

    connected_to(database: { reading_slow: :slow_replica }) do # do something end database.yml ʹఆ͕ٛ͋Ε͹ɺผͷ database Ҿ਺ΛࢦఆՄೳ
  12. • γϯϓϧʹ Write ͸ master ΁ Read ͸ replica ΁ͷέʔεͰ͸େม༗

    ޮɺطଘͷgem୤٫νϟϯε • ෳ਺DB Ͱͷ Migration ΋όονϦαϙʔτ • Slave ͷ όϥϯγϯά͸αϙʔτ֎ͳͷͰɺෳ਺ replica ͕͋Δ৔߹͸ผ ͷػߏ͕ඞཁ • RDS Auroraͷ৔߹͸ΫϥελʔΤϯυϙΠϯτΛࢦఆ͓͚ͯ͠͹ྑ͞ ͦ͏ ·ͱΊ