Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I've got two MySQL servers, one as a master in the replication process and the other as the slave. I was wondering if anything bad would happen if I made the current master a slave to the current slave, and the current slave a master to the current master. So basically, both servers would be replicating to and from each other.

I saw some questions and posts on other sites about 'replication loops', which is how I would describe my question; but these posts seemed to be explaining something completely different.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Terminology

What you're describing is a common set up that may be variously described as master-master replication, dual-master, active-active, bidirectional, or a few other terms.

Good reasons not to do it

The team who wrote High Performance MySQL offer this pithy summary of master-master replication - specifically, "active-active" replication:

In general, allowing writes on both servers causes way more trouble than it’s worth.

Baron Schwartz, one of the authors, offers a slightly more blunt opinion on his blog.

Save yourself grief, work, and money. Never write to both masters.

There are many things that can go wrong if both servers are updating data at the same time, including (but by no means limited to):

  • If something goes wrong with replication - even a temporary network glitch, you'll likely end up with a split brain and it may be impossible to determine which data is "correct"

  • If AUTO_INCREMENT MySQL settings are wrong, it's easy for data collision to take place

  • Row locking no longer offers any protection - data can be manipulated on one master even while a transaction has it locked on another

Good reasons to do it - carefully

The most common reason for master-master replication is to so that you can very quickly switch the "current" master if something goes wrong with one of your servers.

The common way to implement this is to set up a barrier that ensures that only one of your servers is every written to by your application servers. This is usually a hardware load balancer, or software load balancer like HAProxy.

The authors of High Performance MySQL (mentioned above) describe this as "active-passive" master-master replication.

As long as the barrier is robust, you are fairly well protected from all of the issues described above. This may be supported by setting read_only on the "passive" server for an extra layer of protection.

A well-built master-master setup can offer you some amazing flexibility - you can do some really complex table or server maintenance on your passive server, wait for replication to catch up, then migrate your load balancer over to point at your shiny, reconfigured environment with almost no service downtime. This sort of thing does need to be planned carefully, though, and a lot can go wrong.

share|improve this answer
    
Would the setup with HAProxy be more efficient/faster/generally better than a real MySQL cluster (load balance & failover)? –  Libbux Jun 13 '13 at 5:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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