• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working with multiple databases - "reader" database

Actual numbers are not important at the moment - "high" depends on many factors and could be a result of a traffic spike. 

Caches will be put in place - of course, but since the infrastructure will be designed with many nodes and the "reader" nodes will be within an autoscaling configuration i would like to take advantage of that - there are certain things that i wont be able to cache so calls to the database will be necessary. 

Here is a very basic example on the situation with an say e-commerce site:
- in the backend i open an order to view/edit - i should also get "connected/similar" orders - which in some cases be several hundred. Also get some external details for that orders, some warehouse information about stocks and packages for each one. 
- now imagine there are 150 people doing the same thing on a database with several million records in the order tables alone while the frontend traffic continues - several thousand people all doing all sort of crap - making new orders, updating old ones, tracking packages and stuff.
- i would like to be able to direct all those read-only queries to the "reader" nodes as they need to be accurate. 

Assuming the question is not on cache, query optimisations and weather or not i would have user logins, whats the best way to achieve this?  

Can i switch model database on-the-fly by only changing $DBgroup? 

Is if in my controllers constructor i instantiate a model like that: 

PHP Code:
<?php namespace App\Controllers;


Test extends BaseController
$this->model = new OrdersModel();

This will create an instance using the default database (which will be the writer one);

Now if i would like to have a function that would like to call many "SELECT" queries on that model, and would like to direct them to reader nodes, how should i do that? 

If i write a function on my model that switches $DBGroup on the model, will that work to change the database on-the-fly? 
If its possible at all, how do i return to my "default" (writer) db after that? Do another switch?

Messages In This Thread
RE: Working with multiple databases - "reader" database - by korgoth - 04-22-2020, 02:51 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.