Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Rails6.0 での複数 DB 対応
Search
Yuta Horii
July 31, 2019
Programming
2
470
Rails6.0 での複数 DB 対応
Talked at Ebisu.rb #24
https://ebisurb.connpass.com/event/138395
Yuta Horii
July 31, 2019
Tweet
Share
More Decks by Yuta Horii
See All by Yuta Horii
クレジットカードを製造する技術
yutadayo
87
53k
カード発行会社(イシュア)を 支えるシステム解説
yutadayo
4
2.4k
Fintechプロダクトの開発事情とアーキテクチャ解説
yutadayo
1
2.8k
なぜレッドオーシャン化する前にサービスを グロースできなかったのか? - フリマアプリ編 - @yutadayo
yutadayo
101
76k
PCI DSS に準拠したシステム開発
yutadayo
1
1.2k
決済サービス 0 → 1 開発時のアーキテクチャについて / Start up × FinTech
yutadayo
2
1.7k
Rails Secrets の歴史
yutadayo
0
350
フリマアプリ「フリル」を支える Elasticsearch を用いた全文検索入門
yutadayo
1
1k
Rubyとスタートアップ
yutadayo
1
960
Other Decks in Programming
See All in Programming
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
330
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
330
シールドクラスをはじめよう / Getting Started with Sealed Classes
mackey0225
4
640
Macとオーディオ再生 2024/11/02
yusukeito
0
370
CSC509 Lecture 12
javiergs
PRO
0
160
Jakarta EE meets AI
ivargrimstad
0
520
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
Remix on Hono on Cloudflare Workers
yusukebe
1
280
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
Nurturing OpenJDK distribution: Eclipse Temurin Success History and plan
ivargrimstad
0
870
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
Featured
See All Featured
Typedesign – Prime Four
hannesfritz
40
2.4k
How to Think Like a Performance Engineer
csswizardry
20
1.1k
Bash Introduction
62gerente
608
210k
5 minutes of I Can Smell Your CMS
philhawksworth
202
19k
Ruby is Unlike a Banana
tanoku
97
11k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Scaling GitHub
holman
458
140k
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Transcript
Rails6.0 Ͱͷෳ DB ରԠ Ebisu.rb #24
@yutadayo • ງҪ ༤ଠ • Ebisu.rb ͷΦʔΨφΠβʔ͍ͯ͠·͢ • ANGEL PORTʢhttps://angl.jpʣͱ͍
͏αʔϏεΛ࡞͍ͬͯ·͢
ࠓ͢͜ͱ ɾRails 6.0.0.rc2 ͕ 7/22 ʹϦϦʔε ɾMultiple Databases αϙʔτͷհ
https://edgeguides.rubyonrails.org/active_record_multiple_databases.html
Ұൠతͳ Master / Slave ߏͷDB .BTUFS %# 3FQMJDB %# "QQ
ϨϓϦέʔγϣϯ 8SJUF3FBE 3FBE0OMZ
• switch_point • https://github.com/eagletmt/switch_point • Octopus • https://github.com/thiagopradi/octopus • multi_db
• https://github.com/schoefmann/multi_db ෳDBΛѻ͏ gems
• ෳͷ Primary DB ͱͦΕͧΕʹରԠ͢Δ Replica DB • ՔಇதͷϞσϧͰͷࣗಈଓΓସ͑ •
HTTPಈࢺɺۙͷॻ͖ࠐΈʹԠͨ͡ Primary ͱ Replica ͷ ࣗಈΓସ͑ • ෳDBͷ࡞ɾআɾϚΠάϨʔγϣϯૢ࡞Λߦ͏ Rails Task αϙʔτ͞ΕΔ༰
• γϟʔσΟϯά • ΫϥελΛ·͕ͨΔJoinจͷൃߦ • Replica DB ͷϩʔυόϥϯγϯά • ෳDB༻ͷ
schema cache ͷ Dumping ݱ࣌Ͱαϙʔτ͞Εͳ͍༰
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 ͷઃఆ
• Replica DB ʹ replica: true Λઃఆ͢Δ • ৽͘͠Ճͨ͠ DB
ʹϚΠάϨʔγϣϯϑΝΠϧஔ͖ ͷ migrations_paths Λઃఆ͢Δ • ΨΠυϥΠϯʹ replica primary ͱಉ͡DB໊ͰϢʔ βʔΛ͚Δ͖ͱ͋Δ͕ɺผDBͳ͘ઃఆͰ͖Δ ཁ
ϞσϧͰͷࢦఆ 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 ΫϥΠΞϯτͷଓΛফඅͯ͠͠·͏
$ 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
• ैདྷͷ rails db:create Ͱ primary ͱ animals ྆DB࡞͞Ε Δ
• rails db:create:animals ͰࢦఆͷDBͷ࡞Մೳ • migrations_paths ͷϑΥϧμ໊ʹDB໊Λ prefix ʹ͚ͭΔ ͖ • ͚ͳͯ͘ɺࢦఆ͞Ε͍ͯΕͳ͍Α͏Ͱ͢ ཁ
• rails db:create Ͱ replica: true ͷDB࡞͞Ε ͳ͍ • rails
db:create:primary_replica ΤϥʔʹͳΔ • ಉ༷ʹෳDBͷઃఆ͕ͳ͍ database.yml Ͱ rails db:create:primary ΤϥʔʹͳΔ μϝͩͬͨ͜ͱ
ଓͷΓସ͑ʢΦʔτฤʣ 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 ʹ্هઃఆΛՃ͢Δ͚ͩ
• POST / PUT / DELETE / PATCH ͷϦΫΤετ primary
• ॻ͖ࠐΈޙɺdelayʢσϑΥϧτ2ඵʣؒ primary ͰҎ߱ replica • GET / HEAD delay ΑΓલʹॻ͖ࠐΈ͕ͳ͍߹ replica • ࣮ΛΈΔͱ session[:last_write] ͱϦΫΤετ࣌ࠁͷ͕ࠩ delay Λա͍͗ͯͳ͍͔νΣοΫ͢Δ͚ͩͷγϯϓϧͳ࣮ ཁ
ଓͷΓସ͑ʢϚχϡΞϧฤʣ 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 ҾΛࢦఆՄೳ
• γϯϓϧʹ Write master Read replica ͷέʔεͰେม༗
ޮɺطଘͷgem٫νϟϯε • ෳDB Ͱͷ Migration όονϦαϙʔτ • Slave ͷ όϥϯγϯάαϙʔτ֎ͳͷͰɺෳ replica ͕͋Δ߹ผ ͷػߏ͕ඞཁ • RDS Auroraͷ߹ΫϥελʔΤϯυϙΠϯτΛࢦఆ͓͚ͯ͠ྑ͞ ͦ͏ ·ͱΊ