6

The question might be silly and it's not practiced in real world. Anyway kindly give your thoughts/pros/cons....

Lets say I am having two database read replica database and master database

Scenario 1:

Model.all # It should query from read replica database

Scenario 2:

Model.create(attributes) # It should create data in master database

Scenario 3:

Model.where(condition: :some_condition).update(attributes) # It should read data from replica database and update the data in master database

Note: In runtime database should detect the query and process the above 3 scenario.

Questions:

  1. Is this a valid expectation?
  2. if Yes, How to achieve this case completely or partially?
  3. if No, What wrong in this case and what issues we will be facing?

1 Answer 1

2

Rails 6 provides a framework for auto-routing incoming requests to either the primary database connection, or a read replica.

By default, this new functionality allows your app to automatically route read requests (GET, HEAD) to a read-relica database if it has been at least 2 seconds since the last write request (any request that is not a GET or HEAD request) was made.

The logic that specifies when a read request should be routed to a replica is specified in a resolver class, ActiveRecord::Middleware::DatabaseSelector::Resolver by default, which you would override if you wanted custom behavior.

The middleware also provides a session class, ActiveRecord::Middleware::DatabaseSelector::Resolver::Session that is tasked with keeping track of when the last write request was made. Like the resolver, this class can also be overridden.

To enable the default behavior, you would add the following configuration options to one of your app's environment files - config/environments/production.rb for example:

config.active_record.database_selector = { delay: 2.seconds }
config.active_record.database_resolver = 
  ActiveRecord::Middleware::DatabaseSelector::Resolver
config.active_record.database_operations = 
  ActiveRecord::Middleware::DatabaseSelector::Resolver::Session

If you decide to override the default functionality, you can use these configuration options to specify the delay you'd like to use, the name of your custom resolver class, and the name of your custom session class, both of which should be descendants of the default classes

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer, the auto switching can work in HTTP verbs/REST Api calls to rails application. Basically middleware will take care of auto switching by keeping read/write session in database or cookies. But my questions is different, with in model or for simplicity within rails console if I try the actual scenarios in the question, will it work? if not how to achieve that?
Upvoted your answer, as it will definitely help others :)
@Jak in rails 6.1.3.2 you can do something like this guides.rubyonrails.org/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.