Master - Slave mysql database

Hi Guys Has anyone set up a phalcon project that uses mysql databases but which makes use of both a master and a slave databse server? So in other words we want to run some of the queries from a slave server and others from a master. But if the slave is too far behind we want to run all from the master. Do we have to create all the models twice or can we just switch the configuration to use one or the other? Thanx

The documentation is here for how to create connections to multiple databases. The part about sharding shows the selectReadConnection() method, which you may be able to use to check if the slave is up-to-date.

More than likely though, you don't want to be checking the slave every time. You could check the slave once in your bootstrap, then set a constant or global variable which you could then check in selectReadConnection().


Thank you for that, I will definitly look into that. I think the part our team is also looking into is to prevent writing to the slave by accident. So created double models and the slave models had an over ride on the save function to return false to make sure that the slave replication wont be broken but we will play around with trying to do a choose the correct db in the initialize.

So am I right in saying we can call setConnectionService() in the initialize but then call it again in other functions in the model to switch schemas? Or should we do all the checks in the initialize and based on how far behind the slave is we might set the read to be on the master?

Sorry, this is our first project on phalcon so we are still trying to find our way around some of the aspects.