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
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
330
CSC305 Lecture 25
javiergs
PRO
0
130
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
190
As an Engineers, let's build the CRM system via LINE Official Account 2.0
clonn
1
670
数十万行のプロジェクトを Scala 2から3に完全移行した
xuwei_k
0
260
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
160
Semantic Kernelのネイティブプラグインで知識拡張をしてみる
tomokusaba
0
180
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
140
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
Security_for_introducing_eBPF
kentatada
0
110
Full stack testing :: basic to basic
up1
1
930
Featured
See All Featured
Writing Fast Ruby
sferik
628
61k
Why Our Code Smells
bkeepers
PRO
335
57k
We Have a Design System, Now What?
morganepeng
51
7.3k
Navigating Team Friction
lara
183
15k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
How GitHub (no longer) Works
holman
311
140k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
160
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
26
1.9k
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ͷ߹ΫϥελʔΤϯυϙΠϯτΛࢦఆ͓͚ͯ͠ྑ͞ ͦ͏ ·ͱΊ