<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>

CC BY-SA 3.0
2016-08-01T12:42:54.643Z
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).
Josh Angell
CC BY-SA 3.0
2016-08-01T17:24:33.883Z
@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
CC BY-SA 3.0
2016-08-01T18:05:03.347Z
@BradBell Sweet, thanks for the clarification!
Josh Angell
CC BY-SA 3.0
2016-08-02T14:28:40.993Z
Totally good to know that, @JoshAngell & BradBell. Thanks for the update.
Neal Magee
CC BY-SA 4.0
2016-05-26T04:43:08.180Z
2019-12-09T03:40:05.423Z
2019-12-09T03:40:05.423Z
Brad Bell
<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>

CC BY-SA 3.0
2016-05-25T16:32:18.830Z
This doesn't really seem to be a Craft-specific question. Perhaps it should be migrated to Stack Overflow?
Lindsey D
CC BY-SA 3.0
2016-05-25T17:25:33.500Z
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
CC BY-SA 3.0
2016-05-25T18:41:27.000Z
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.
RitterKnight
<plugin-development><database><mysql>
Implementing Read-Replica (slave) database connections
# 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 [@RitterKnight](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 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).  
> **username_3**: @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!  
> **username_4**: @BradBell Sweet, thanks for the clarification!  
> **username_0**: Totally good to know that, @JoshAngell & BradBell. Thanks for the update.  

---

