I have just finished following RoR Multiple Databases with Active Record however, my Rails app still seems to be completely ignoring my read replica configuration. In fact, I specifically made a typo in the hostname and have never been able to trigger an error yet on reading from the read replica.
Here's my config/initializers/multi_db.rb
file:
Rails.application.configure do
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
end
My config/database.yml
file:
development:
primary:
<<: *default
host: <%= Rails.application.credentials.development[:host] %>
username: <%= Rails.application.credentials.development[:username] %>
password: <%= Rails.application.credentials.development[:password] %>
database: <%= Rails.application.credentials.development[:database] %>
primary_replica:
<<: *default
host: aurora-prod-test.cluster-rox-.us-west-2.rds.amazonaws.com
username: <%= Rails.application.credentials.development[:username] %>
password: <%= Rails.application.credentials.development[:password] %>
database: <%= Rails.application.credentials.development[:database] %>
replica: true
And in my app/models/application_record.rb
file, I have specified:
connects_to database: { writing: :primary, reading: :primary_replica }
According to the article, that seems to be it. However, when I try a basic GET request, it doesn't seem to use the read replica. I have a typo in the name where it shows 'rox' instead of 'ro' and I'm not experiencing any errors, indicating that the read replica is not working properly.
CodePudding user response:
Turns out this is a bug in Rails: https://github.com/rails/rails/issues/45162
Was able to get it fixed by adding the code that belonged in config/initializers/multi_db.rb
into my config/application.rb
file instead.