This question relates to implementing redundancy when having two databases syncronising on 2 different servers. First of all, I’ll explain the setup so that you can understand the backround of the issue.
I have two different servers, operating different operating systems with different DB types at 2 different locations.
Server 1 (local server):
Windows 2003 Small Business Server OS
MSSQL DB Server
Server-Side Language - C# ASP.NET
Server 2 (website server):
Linux CentOS 6
MySQL DB
Server-Side Language - PHP
Server 1 runs the internal management side of the software and server 2 is the external website interface (server 2 has some management aspects 2). Unfortunately, we have seen frequent loss of broadband at our office and this means that communciation between the two servers may not always be possible (we have 3G broadband redundancy but it takes time for Dynamic DNS to update the DNS record with our new IP address when we lose the primary broadband). This is what caused the need for each server to have its own DB – as we can’t allow one or the other of these two sides to go offline entirely.
As such, I’ve built it into both servers such that both have their own local database but apply any changes to other server as well. If the other server cannot be connected to, it will save the query to the local DB so that the queries can be applied when the other server becomes available again. This leaves two issues:
-
A general point about syncing between databases like this – how
can I best avoid conflicting queries being applied when the servers
reconnect? See example 1 below for a description of what I mean. This
problem is minimised because the connection should only be lost for a
couple minutes – until the DNS record is updated. -
When applying the back-logged queries, I would like to have the script automatically apply the queries (rather than having to manually launch a script to resolve back-logged queries). However, I’m unsure what effect this would have if the user navigated away from the page whilst it was still appying the back-logged queries in the background.
Example 1:
If a user on the website side (server 2) changes their email address, or phone number etc., and a user on the management side (sever 1) changes that user’s details then these two queries will have conflicting information. When the servers reconnect then the two queries will be applied to the other server – and the entries will still be out of sync.
P.S. Sorry the question is so long.
Don’t re-invent database replication. Use the one provided by your database software instead.
However, this means you should switch to the same RDBMS on both ends. MySQL replication is fairly reliable on WAN connections and will work reliably between platforms and versions.