Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Rails 6 Multi-DB 実戦投入

646 views

Published on

Rails 6 Multi-DB導入時のはまりどころについて話します。

こちらのイベントで発表した内容です。

Rails Developers Beer Bash 〜Railsのトレンドとこれから〜
https://techplay.jp/event/749901

Published in: Software
  • Be the first to comment

Rails 6 Multi-DB 実戦投入

  1. 1. Rails 6 Multi-DB
  2. 2. • (0x26) • Rails (native) • kiyot • Falcon
  3. 3. • • https://wap.nikkei.co.jp/ • Rails 6 • Multi-DB
  4. 4. • • https://wap.nikkei.co.jp/ • Rails 6 • Multi-DB
  5. 5. Rails 6 Multi-DB • ! Rails 6 Multi-DB •
  6. 6. Rails 6 Multi-DB Rails 6 Multi-DB
  7. 7. Rails • primary 1 replica • • HTTP verb primary replica • Rails
  8. 8. Rails • DB (replica ) • DB • DB DB • DB Rails DB
  9. 9. Rails • ! DB (replica ) • DB • DB DB • DB Rails DB
  10. 10. DB production: primary: database: my_primary_database user: root adapter: mysql2 primary_replica: database: my_primary_database user: root_readonly adapter: mysql2 replica: true animals: database: my_animals_database user: animals_root adapter: mysql2 migrations_paths: db/animals_migrate animals_replica: database: my_animals_database user: animals_readonly adapter: mysql2 replica: true
  11. 11. Rails • DB (replica ) • ! DB • DB DB • DB Rails DB
  12. 12. DB (1) DB animals DB class AnimalsBase < ApplicationRecord self.abstract_class = true connects_to database: { writing: :animals, reading: :animals_replica } end primary DB class ApplicationRecord < ActiveRecord::Base self.abstract_class = true connects_to database: { writing: :primary, reading: :primary_replica } end
  13. 13. DB (2) transaction connection # animals # primary DB Monkey ApplicationRecord.transaction do Monkey.create! raise ActiveRecord::Rollback end # animal DB ApplicationRecord.connection.execute 'INSERT INTO monkeys ...'
  14. 14. DB (3) ActiveRecord::Base.connected_to(database: { reading_slow: :animals_slow_replica }) do # replica end
  15. 15. Rails • DB (replica ) • DB • ! DB DB • DB Rails DB
  16. 16. middleware (1) 2 1. HTTP … • GET/HEAD DB • DB 2. DB GET/HEAD DB
  17. 17. middleware (2) application.rb 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
  18. 18. middleware (3) • •
  19. 19. Rails • DB (replica ) • DB • DB DB • ! DB Rails DB
  20. 20. DB Rails $ 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
  21. 21. Rails 6 Multi-DB • Rails 6 Multi-DB • !
  22. 22. Multi-DB • / DB • middleware …
  23. 23. middleware
  24. 24. middleware • HTTP read/write
  25. 25. middleware • HTTP read/write • • DB •
  26. 26. middleware • HTTP read/write • • DB • • ActiveRecord::Base.connected_to DB
  27. 27. • gem Rails
  28. 28. • gem Rails • mountable engine • middleware
  29. 29. • • /
  30. 30. • middleware • middleware around_action • ApplicationController • • rails around_action
  31. 31. mountable engine
  32. 32. mountable engine …
  33. 33. Rails 6.0.0
  34. 34. Rails 6.0.0 Multi-DB • middleware • 6.0.1 • https://github.com/rails/rails/pull/37065
  35. 35. Middleware ( ) middleware • DB GET/HEAD DB •
  36. 36. Middleware ( ) middleware • DB GET/HEAD DB • ↑ DB
  37. 37. Middleware ( ) middleware • DB GET/HEAD DB • ↑ DB •
  38. 38. 1. DB 2. connected_to GET/HEAD 3. !
  39. 39. 6.0.0 connected_to(role: :writing) while_preventing_writes(false) ActiveRecord::Base.connected_to(role: :writing) do # ↓ ActiveRecord::Base.connection_handler.while_preventing_writes(false) do # code that writes to the primary DB end end ※ Rails 6.0.1
  40. 40. Middleware 1. Middleware mountable engine • Middleware around_action 2. 6.0.0 Multi-DB • 6.0.1
  41. 41. Multi-DB

×