Which is the best way to bi-directionally synchronize dynamic data in real time using mysql Which is the best way to bi-directionally synchronize dynamic data in real time using mysql database database

Which is the best way to bi-directionally synchronize dynamic data in real time using mysql


There isn't much performance to be gained from replicating your database on two masters. However, there is a nifty bit of failover if you code your application correct.

Master-Master setup is essentially the same as the Slave-Master setup but has both Slaves started and an important change to your config files on each box.

Master MySQL 1:

auto_increment_increment = 2auto_increment_offset = 1 

Master MySQL 2:

auto_increment_increment = 2auto_increment_offset = 2

These two parameters ensure that when two servers are fighting over a primary key for some reason, they do not duplicate and kill the replication. Instead of incrementing by 1, any auto-increment field will by default increment by 2. On one box it will start offset from 1 and run the sequence 1 3 5 7 9 11 13 etc. On the second box it will start offset at 2 and run along 2 4 6 8 10 12 etc. From current testing, the auto-increment appears to take the next free number, not one that has left before.
E.g. If server 1 inserts the first 3 records (1 3 and 5) when Server 2 inserts the 4th, it will be given the key of 6 (not 2, which is left unused).

Once you've set that up, start both of them up as Slaves.
Then to check both are working ok, connect to both machines and perform the command SHOW SLAVE STATUS and you should note that both Slave_IO_Running and Slave_SQL_Running should both say “YES” on each box.

Then, of course, create a few records in a table and ensure one box is only inserting odd numbered primary keys and the other is only incrementing even numbered ones.

Then do all the tests to ensure that you can perform all the standard applications on each box with it replicating to the other.

It's relatively simple once it's going.
But as has been mentioned, MySQL does discourage it and advise that you ensure you are mindful of this functionality when writing your application code.

Edit: I suppose it's theoretically possible to add more masters if you ensure that the offsets are correct and so on. You might more realistically though, add some additional slaves.


MySQL does not support synchronous replication, however, even if it did, you would probably not want to use it (can't take the performance hit of waiting for the other server to sync on every transaction commit).

You will have to consider more appropriate architectural solutions to it - there are third party products which will do a merge and resolve conflicts in a predetermined way - this is the only way really.

Expecting your architecture to function in this way is naive - there is no "easy fix" for any database, not just MySQL.


Is it important that the UIDs are the same? Or would you entertain the thought of having a table or column mapping the remote UID to the local UID and writing custom synchronisation code for objects you wish to replicate across that does any necessary mapping of UIDs for foreign key columns, etc?