So how does this database synchronization work? Here’s the scheme, which I made up myself, so it may be completely beyond the pale. We shall find out.
The first piece is a table called sync_hosts that stores the ids of other hosts, the time they were last synced, and whether or not a sync is in progress.
Then each table indicated in the config file has two additional tables created to store metadata. These tables are suffixed with _map and _sync, so if the table is called “table”, then the two additional tables are called “table_map” and “table_sync”. In this initial version, it will be left to the user to ensure that table name conflicts are avoided.
The sync table
The sync table contains the primary keys of the original table, plus three additional columns: “created” (timestamp), “modified” (timestamp), and “deleted” (tinyint(1)). These columns contain, as one might suspect, the creation time, modification time, and whether or not the row has been deleted.
The map table
The map table contains two copies of the primary keys, plus a remote host id. This table allows one to look up what the primary keys of a row are on a remote host. This is necessary because each host will be creating different rows at different times, so the primary keys will only be the same by coincidence.
How it all works
The basic scheme is this. We install triggers on all the tables such that whenever a row is created, modified, or deleted in the original table, a corresponding row is created or updated in the sync table.
Then, when it is time to sync, we get all the rows from the sync table that have been created or updated since the last sync time. We take these rows over to the remote host, look them up in the map table, and make the updates (or create the rows if they aren’t found in the map table). We then do the entire operation in reverse, so that all of the updates from the remote host are brought to the local host as well. When we are doing our updates, we check the sync table of the destination host so that we don’t write over any changes that are newer than ours.
That’s about it! Things will surely get more complicated as we get deeper into this, and we may have to make more rules to make it work. Or it may not work at all! Who knows?