Using Replica database with your ActiveRecord models

Tram Ho

Occasionally, we face the problem: along with the regular Mysql model, we also have a replica database.

This replica database seems to be kept in sync with database production, but it is not used by web servers or background workers, everything in it can only be used as read-only data.

Why do we care about it? In today’s large systems, there are often page dashboards, on which there are some features that work with large amounts of data such as: displaying statistics, information about the status of some modules in system … In these problems, the problem will be simple if we do not have to care about realtime statistics, then just query the data in the database and store the results in the cache.

Everything becomes complicated if we care about accurate data in real time, accurate to the second, then the problem of optimizing the query to return the data quickly and accurately. is the key task. That’s when you may need to replica the database, and learn the model of connecting it to Active Record in the Rails application.

Our original idea was to implement on each separate table we want to use when querying replica:

In particular, config leadfeeder_replica _ # {env} is defined in the database.yml file for each environment we use (development, test, production …), this way we will have flexibility in the query:

As you can see, there doesn’t seem to be a real DRY to define these modules in each model. Also we have to remember to call readonly! on replica logs to prevent writing to this database. This can be done automatically for each model by using the after_initialize callback in the active record:

But this has not yet solved the DRY issue on the models. The solution is to use ActiveSupport :: Concern in combination with the included callback:

Base use cases only

What happens when querying on relational models, on tables joined together:

Unfortunately, in this case, it will not return Account :: Replica but only Account instance. It also queries directly into the database production when we use the lazy loading account.

Source:

https://medium.com/@konole/using-replica-database-with-your-activerecord-models-b2a8ce9ef46

Share the news now

Source : Viblo