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
480
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
54k
カード発行会社(イシュア)を 支えるシステム解説
yutadayo
4
2.5k
Fintechプロダクトの開発事情とアーキテクチャ解説
yutadayo
1
2.9k
なぜレッドオーシャン化する前にサービスを グロースできなかったのか? - フリマアプリ編 - @yutadayo
yutadayo
101
77k
PCI DSS に準拠したシステム開発
yutadayo
1
1.2k
決済サービス 0 → 1 開発時のアーキテクチャについて / Start up × FinTech
yutadayo
2
1.8k
Rails Secrets の歴史
yutadayo
0
360
フリマアプリ「フリル」を支える Elasticsearch を用いた全文検索入門
yutadayo
1
1.1k
Rubyとスタートアップ
yutadayo
1
970
Other Decks in Programming
See All in Programming
42 best practices for Symfony, a decade later
tucksaun
1
180
선언형 UI에서의 상태관리
l2hyunwoo
0
150
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.6k
Haze - Real time background blurring
chrisbanes
1
510
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
720
テストコードのガイドライン 〜作成から運用まで〜
riku929hr
1
170
ドメインイベント増えすぎ問題
h0r15h0
1
250
「Chatwork」Android版アプリを 支える単体テストの現在
okuzawats
0
180
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
180
rails stats で紐解く ANDPAD のイマを支える技術たち
andpad
1
290
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
51
7.3k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
It's Worth the Effort
3n
183
28k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Into the Great Unknown - MozCon
thekraken
33
1.5k
A Tale of Four Properties
chriscoyier
157
23k
Building Applications with DynamoDB
mza
91
6.1k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
5
440
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
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ͷ߹ΫϥελʔΤϯυϙΠϯτΛࢦఆ͓͚ͯ͠ྑ͞ ͦ͏ ·ͱΊ