Working with MySQL Read Replicas

Hi, I am using Yii on Amazon Web Services. We are continuously improving the Code to make the site faster, as we are working with big data. However, we are at a point where this all does not make any sense anymore and we need to start using RDS Read Replicas.

Basically our users perform just find functions on the database and just a few write functions (logging, settings, no big data). What I was thinking about is to extend the CActiveRecords Class and use the beforeFind method to connect to the Read Replica, when ever a write functions is called, use beforeSave to connect to the original database.

My question now is, if you guys think this could work or maybe if you have a better idea (Cronjobs, both read and write, will be constantly connected to the original). And I need to know how to setup extra database connections (from config file) in Yii which are active to the same time. I don’t want to open and close the connections all the time, but to have both open, or open it to the first time it is used, and just tell Yii which one it should use to make the next query.

Thanks!

Hi, I know this is rather old post, but I’m in the very same situation. Replicas are no problem, since RDS takes care of them automagically, just create one or the master and that’s all. But the Yii part is the interesting one. Was it possible?

Overload the getDbConnection method in your model and put there logic which would choose the connection. See the source code for reference how it reads the default connection.

Remember you can’t do eager loading between models that are using different db connections.