I'm thinking about ways to implement this but don't instinctively know the best route in Craft.

Our infrastructure is in AWS and, due to other applications, we have access to a read-replica DB instance alongside the master instance. I'm wondering about how to write an extension/plugin that would direct all SELECT requests to either the master OR the slave, and all INSERT / UPDATE / DELETE requests to the master only.

What are some possible paths for incorporating this?

  • 1
    This doesn't really seem to be a Craft-specific question. Perhaps it should be migrated to Stack Overflow?
    – Lindsey D
    May 25 '16 at 16:32
  • I am thinking this is Craft-related since I'm looking to create a resource usable only by Craft to do this work. For example, HyperDB is one of the plugins you could use to implement this for WordPress.
    – Neal Magee
    May 25 '16 at 17:25
  • I see someone wrote this for Yii re: splitting DBs... not sure what you'd need to abstract into Craft land since you're literally touching every sql query... I wonder about the performance gains as well, especially since craft does a lot of read/write when expiring caches, etc. May 25 '16 at 18:41

Craft 3

You can see an example of how to do read/write splitting in Craft 3 here: https://docs.craftcms.com/v3/config/app.html#database-component

Craft 2

Definitely not current possibly natively in Craft since Yii's CDbConnection (which is what Craft uses for database connections) has no concept of master/slave connections.

In theory, something like the Yii DB splitting extension that @RitterKnight mentioned is a start in the right direction, but even then, you'd have to do some pretty significant core hacking to utilize it in Craft 2.

Should be much more feasible in Craft 3 since the ability to swap out core components like that is planned.

  • 2
    Just a note for people following this that Yii now natively supports DB read-write splitting as of version 2.0.9. This means it should theoretically be relatively simple in Craft 3 as all Craft would need to do would be expose the configuration of the \yii\db\Connection class. And even if they couldn’t do that as Brad said the core component could be swapped out and replaced. See here for more discussion on the matter and the docs. Aug 1 '16 at 12:42
  • 2
    @JoshAngell Actually looks like that was added in the 2.0 launch. Enh #3708: Added database replication and automatic read-write splitting support for yii\db\Connection (qiangxue). Good to know, though!
    – Brad Bell
    Aug 1 '16 at 17:24
  • @BradBell Sweet, thanks for the clarification! Aug 1 '16 at 18:05
  • Totally good to know that, @JoshAngell & BradBell. Thanks for the update.
    – Neal Magee
    Aug 2 '16 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.