We are introducing a master / slave replication schema in our web application. At certain scenarios we would prefer to explicitly direct reads to a slave and writes to the master, which to me is an obvious advantage of the schema.

Instinctually, I'd prefer for the switch to happen somewhere in the ORM, we use Doctrine and it offers a neat solution. But that's not really a solution for us, because we don't have a very practical way of distinguishing when a read is in reality a write (such as in update scenarios).

The next best thing would be to delegate the responsibillity for the switch to the controller and / or some custom libraries that in a cliche MVC approach could be considered as supercharged models. Although this is a very practical solution and probably the one that would require less time, I somehow fear there's something very important I'm missing and it will come back and bite me hard.

There are vague time constraints, and management support to feveresly refactor, but for this specific scenario we mostly agree that we should go with the practical approach, even if it means limit ourselves only where we've identifed the database as a major bottleneck.

To add to the confusion, I can't really say I'm experienced in replication schemes, when the issue was first discussed I thought a better solution would be to adopt a multi-master scheme, but when I started researching how easy those fail and how robust their implementation should be, it stopped feeling reasonable.


  • And could someone think of a better title? Edit at will...
    – yannis
    Jan 5, 2012 at 16:30

1 Answer 1


You'll need to be able to identify the entire transaction as readonly or read-write. The transaction guarantees durability on the database you're operating on, not on another database.

That said, I think the controller is the right place to put this. You should maintain two sources for connections and use whichever is appropriate.

I don't think your proposed scenario is very common. Generally an entire service will either talk to the master or to the slave, but not to both.

  • 1
    I agree fully. Maintain two connection pools (or however you do it in your app), and choose one or the other for a transaction depending on the need.
    – gahooa
    Jan 5, 2012 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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