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

Rails6.0 での複数 DB 対応

Rails6.0 での複数 DB 対応

97feb505b46c6d64890c0dd52daab4b4?s=128

yuta horii

July 31, 2019
Tweet

More Decks by yuta horii

Other Decks in Programming

Transcript

  1. Rails6.0 Ͱͷෳ਺ DB ରԠ Ebisu.rb #24

  2. @yutadayo • ງҪ ༤ଠ • Ebisu.rb ͷΦʔΨφΠβʔ͍ͯ͠·͢ • ANGEL PORTʢhttps://angl.jpʣͱ͍

    ͏αʔϏεΛ࡞͍ͬͯ·͢
  3. ࠓ೔࿩͢͜ͱ ɾRails 6.0.0.rc2 ͕ 7/22 ʹϦϦʔε ɾMultiple Databases αϙʔτͷ঺հ

  4. https://edgeguides.rubyonrails.org/active_record_multiple_databases.html

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

    ϨϓϦέʔγϣϯ 8SJUF3FBE 3FBE0OMZ
  6. • switch_point • https://github.com/eagletmt/switch_point • Octopus • https://github.com/thiagopradi/octopus • multi_db

    • https://github.com/schoefmann/multi_db ෳ਺DBΛѻ͏ gems
  7. • ෳ਺ͷ Primary DB ͱͦΕͧΕʹରԠ͢Δ Replica DB • ՔಇதͷϞσϧͰͷࣗಈ઀ଓ੾Γସ͑ •

    HTTPಈࢺ΍ɺ௚ۙͷॻ͖ࠐΈʹԠͨ͡ Primary ͱ Replica ͷ ࣗಈ੾Γସ͑ • ෳ਺DBͷ࡞੒ɾ࡟আɾϚΠάϨʔγϣϯૢ࡞Λߦ͏ Rails Task αϙʔτ͞ΕΔ಺༰
  8. • γϟʔσΟϯά • ΫϥελΛ·͕ͨΔJoinจͷൃߦ • Replica DB ͷϩʔυόϥϯγϯά • ෳ਺DB༻ͷ

    schema cache ͷ Dumping ݱ࣌఺Ͱαϙʔτ͞Εͳ͍಺༰
  9. 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 ͷઃఆ
  10. • Replica DB ʹ͸ replica: true Λઃఆ͢Δ • ৽͘͠௥Ճͨ͠ DB

    ʹ͸ϚΠάϨʔγϣϯϑΝΠϧஔ͖ ৔ͷ migrations_paths Λઃఆ͢Δ • ΨΠυϥΠϯʹ͸ replica ͸ primary ͱಉ͡DB໊ͰϢʔ βʔΛ෼͚Δ΂͖ͱ͋Δ͕ɺผDB΋໰୊ͳ͘ઃఆͰ͖Δ ཁ఺
  11. ϞσϧͰͷࢦఆ 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 ΫϥΠΞϯτͷ઀ଓ਺Λফඅͯ͠͠·͏
  12. $ 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
  13. • ैདྷͷ rails db:create Ͱ͸ primary ͱ animals ྆DB࡞੒͞Ε Δ

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

    db:create:primary_replica ͸ΤϥʔʹͳΔ • ಉ༷ʹෳ਺DBͷઃఆ͕ͳ͍ database.yml Ͱ rails db:create:primary ΋ΤϥʔʹͳΔ μϝͩͬͨ͜ͱ
  15. ઀ଓͷ੾Γସ͑ʢΦʔτฤʣ 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 ʹ্هઃఆΛ௥Ճ͢Δ͚ͩ
  16. • POST / PUT / DELETE / PATCH ͷϦΫΤετ͸ primary

    • ॻ͖ࠐΈޙɺdelayʢσϑΥϧτ͸2ඵʣؒ͸ primary ͰҎ߱ ͸ replica ΁ • GET / HEAD ͸ delay ΑΓલʹॻ͖ࠐΈ͕ͳ͍৔߹͸ replica ΁ • ࣮૷ΛΈΔͱ session[:last_write] ͱϦΫΤετ࣌ࠁͷ͕ࠩ delay Λա͍͗ͯͳ͍͔νΣοΫ͢Δ͚ͩͷγϯϓϧͳ࣮૷ ཁ఺
  17. ઀ଓͷ੾Γସ͑ʢϚχϡΞϧฤʣ 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 Ҿ਺ΛࢦఆՄೳ
  18. • γϯϓϧʹ Write ͸ master ΁ Read ͸ replica ΁ͷέʔεͰ͸େม༗

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