Answers.Body: <p><strong>Craft 3</strong></p>
  
  <p>You can see an example of how to do read/write splitting in Craft 3 here: <a href="https://docs.craftcms.com/v3/config/app.html#database-component" rel="nofollow noreferrer">https://docs.craftcms.com/v3/config/app.html#database-component</a></p>
  
  <p><strong>Craft 2</strong></p>
  
  <p>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.</p>
  
  <p>In theory, something like the Yii DB <a href="http://www.yiiframework.com/extension/dbreadwritesplitting/" rel="nofollow noreferrer">splitting extension</a> that <a href="https://craftcms.stackexchange.com/questions/15217/implementing-read-replica-slave-database-connections#comment20332_15217">@RitterKnight</a> 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.</p>
  
  <p>Should be much more feasible in Craft 3 since the ability to swap out core components like that is planned.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-01T12:42:54.643Z
Answers.Text: 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](https://github.com/yiisoft/yii2/pull/4274) for more discussion on the matter and the [docs](http://www.yiiframework.com/doc-2.0/yii-db-connection.html).
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-01T17:24:33.883Z
Answers.Text: @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!
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-01T18:05:03.347Z
Answers.Text: @BradBell Sweet, thanks for the clarification!
Answers.UserDisplayName: Josh Angell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-02T14:28:40.993Z
Answers.Text: Totally good to know that, @JoshAngell & BradBell. Thanks for the update.
Answers.UserDisplayName: Neal Magee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2016-05-26T04:43:08.180Z
Answers.LastActivityDate: 2019-12-09T03:40:05.423Z
Answers.LastEditDate: 2019-12-09T03:40:05.423Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm thinking about ways to implement this but don't instinctively know the best route in Craft.</p>
  
  <p>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 <code>SELECT</code> requests to either the master OR the slave, and all <code>INSERT / UPDATE / DELETE</code> requests to the master only.</p>
  
  <p>What are some possible paths for incorporating this?</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-25T16:32:18.830Z
Comments.Text: This doesn't really seem to be a Craft-specific question. Perhaps it should be migrated to Stack Overflow?
Comments.UserDisplayName: Lindsey D
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-25T17:25:33.500Z
Comments.Text: 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.
Comments.UserDisplayName: Neal Magee
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-05-25T18:41:27.000Z
Comments.Text: I see someone wrote [this](http://www.yiiframework.com/extension/dbreadwritesplitting/) 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.
Comments.UserDisplayName: RitterKnight
Tags: <plugin-development><database><mysql>
Title: Implementing Read-Replica (slave) database connections
ThreadText: # Implementing Read-Replica (slave) database connections
  
  (Asked by: username_0 on 2016-05-25)
  
  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?
  
  **Tags:** plugin-development, database, mysql
  
  > **Comments:**
  > **username_1**: This doesn't really seem to be a Craft-specific question. Perhaps it should be migrated to Stack Overflow?  
  > **username_0**: 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.  
  > **username_2**: I see someone wrote [this](http://www.yiiframework.com/extension/dbreadwritesplitting/) 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.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2016-05-26. Score: 2 | Accepted answer
  
  **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](http://www.yiiframework.com/extension/dbreadwritesplitting/) that [@username_2](https://craftcms.stackexchange.com/questions/15217/implementing-read-replica-slave-database-connections#comment20332_15217) 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.
  
  > **Comments:**
  > **username_4**: 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 username_3 said the core component could be swapped out and replaced. See [here](https://github.com/yiisoft/yii2/pull/4274) for more discussion on the matter and the [docs](http://www.yiiframework.com/doc-2.0/yii-db-connection.html).  
  > **username_3**: @username_4 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!  
  > **username_4**: @username_3 Sweet, thanks for the clarification!  
  > **username_0**: Totally good to know that, @username_4 & username_3. Thanks for the update.  
  
  ---
  
  