Mysql – Multi master replication with MySQL and Django

multi-masterMySQLreplication

I am working on scaling out a webapp and providing some database redundancy for protection against failures and to keep the servers up when updates are needed. The app is still in development, so I have chosen a simple multi-master redundancy with two separate database servers to try and achieve this. Each server will have the Django code and host its own database, and the databases should be as closely mirrored as possible (updated within a few seconds).

I am trying to figure out how to set up the multi-master (master-master) replication between databases with Django and MySQL. There is a lot of documentation about setting it up with MySQL only (using various configurations), but I cannot find any for making this work from the Django side of things.

From what I understand, I need to approach this by adding two database entries in the Django settings (one for each master) and then write a database router that will specify which database to read from and which to write from. In this scenario, both databases should accept both reads and writes, and writes/updates should be mirrored over to the other database. The logic in the router could simply use a round-robin technique to decide which database to use. From there on, further configuration to set up the actual replication should be done through MySQL configuration.

Does this approach sound correct, and does anyone have any experience with getting this to work?

Best Answer

Master-Master has potential problems. If you have AUTO_INCREMENTs, they need to be configured correctly. If there is a chance of "duplicate key" for other reasons, that will stop replication.

Hence, I recommend not writing to both Masters. Instead write to only on, keeping the other one as a 'hot backup'.

Consider Galera.