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
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
Click-free releases & the making of a CLI app
oheyadam
2
110
どうして僕の作ったクラスが手続き型と言われなきゃいけないんですか
akikogoto
1
120
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
CSC509 Lecture 09
javiergs
PRO
0
140
Macとオーディオ再生 2024/11/02
yusukeito
0
370
Jakarta EE meets AI
ivargrimstad
0
610
TypeScriptでライブラリとの依存を限定的にする方法
tutinoko
2
670
色々なIaCツールを実際に触って比較してみる
iriikeita
0
330
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
100
Contemporary Test Cases
maaretp
0
140
GitHub Actionsのキャッシュと手を挙げることの大切さとそれに必要なこと
satoshi256kbyte
5
430
Featured
See All Featured
How to Ace a Technical Interview
jacobian
276
23k
Facilitating Awesome Meetings
lara
50
6.1k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Scaling GitHub
holman
458
140k
Statistics for Hackers
jakevdp
796
220k
Why Our Code Smells
bkeepers
PRO
334
57k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
Optimising Largest Contentful Paint
csswizardry
33
2.9k
Speed Design
sergeychernyshev
25
610
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ͷ߹ΫϥελʔΤϯυϙΠϯτΛࢦఆ͓͚ͯ͠ྑ͞ ͦ͏ ·ͱΊ