2

I am struggling with the following issue: I have two MySQL servers, configured with as Master-Slave, but they are not on the same internal network, so sometimes there is a noticeable delay, and reading from the slave after a write, can lead to inconsistency issues.

I was wondering if there is a way to make mysql read from the slave, and somehow fallback to the slave if there are no results on the slave or some other solution.

Not sure if that can be even done, neither if should be on the application layer or at mysql level.

The current setup is php5 with mysqlnd extension and by following the documentation (adding slave on the yaml http://symfony.com/doc/current/reference/configuration/doctrine.html) I managed to get the app to read from the slave and write on the master.

1 Answer 1

9

Eventually, I found on the PHP-doc of Doctrine\DBAL\Connections\MasterSlaveConnection that it can be enforced the usage of the master at any point just by calling:

$conn->connect('master'); 

So, my solution was putting the following line at the beginning of the controller actions that require consistency on the data:

$this->getDoctrine()->getConnection()->connect('master');

This way, it always reads from the master during that execution flow, while the rest of the application keeps balancing the reads/writes. I am not sure if it's the best solution but it worked for my purposes.

I hope it helps someone in a similar situation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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