Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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.8k
なぜレッドオーシャン化する前にサービスを グロースできなかったのか? - フリマアプリ編 - @yutadayo
yutadayo
101
77k
PCI DSS に準拠したシステム開発
yutadayo
1
1.2k
決済サービス 0 → 1 開発時のアーキテクチャについて / Start up × FinTech
yutadayo
2
1.8k
Rails Secrets の歴史
yutadayo
0
350
フリマアプリ「フリル」を支える Elasticsearch を用いた全文検索入門
yutadayo
1
1k
Rubyとスタートアップ
yutadayo
1
960
Other Decks in Programming
See All in Programming
かんたんデザイン編集やってみた~「完全に理解した」までの道のり~
morit4ryo
1
120
Full stack testing :: basic to basic
up1
1
870
PipeCDの歩き方
kuro_kurorrr
4
140
CSC509 Lecture 13
javiergs
PRO
0
150
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
120
デザインパターンで理解するLLMエージェントの作り方 / How to develop an LLM agent using agentic design patterns
rkaga
11
3.2k
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
350
気をつけたい!Desktop対応で陥りやすい罠とその対策
goto_tsl
0
190
型のインスタンス化は非常に深く、無限である可能性があります。
kimitashoichi
0
120
PaaSとSaaSの境目で信頼性と開発速度を両立する 〜TROCCO®︎のこれまでとこれから〜
gtnao
6
6.7k
Symfony Mapper Component
soyuka
2
460
第5回日本眼科AI学会総会_AIコンテスト_3位解法
neilsaw
0
130
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
95
5.2k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
KATA
mclloyd
29
14k
Optimising Largest Contentful Paint
csswizardry
33
3k
Typedesign – Prime Four
hannesfritz
40
2.4k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Thoughts on Productivity
jonyablonski
67
4.3k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.2k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Fireside Chat
paigeccino
34
3.1k
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ͷ߹ΫϥελʔΤϯυϙΠϯτΛࢦఆ͓͚ͯ͠ྑ͞ ͦ͏ ·ͱΊ