What happens to one write under asynchronous replication
- The client sends
UPDATEandCOMMITto the primary in region A. - The primary writes the change to its local log (the WAL in PostgreSQL, the binary log in MySQL) and flushes it to disk.
- The primary tells the client the commit succeeded.
- Some time later, the log record is shipped over the network to the replica in region B.
- The replica writes the record to its own log and applies it to its data.
What synchronous replication changes, and what it costs
synchronous_standby_names and synchronous_commit. MySQL offers semi-synchronous replication through a plugin, where the source waits until at least one replica acknowledges receipt of the transaction.
The gain is clear. When the primary reports a commit, a second region already has it, so losing region A does not lose acknowledged writes.
The cost is that every commit now includes a cross-region round trip. If the regions are far apart, that round trip is added to every write transaction, and applications that commit many small transactions feel it the most. The bigger problem is what happens when region B becomes unreachable. The primary has to choose between two bad options:
- Keep waiting. Commits hang until the standby returns. You keep consistency and lose availability, so an outage in region B stops writes in region A. PostgreSQL behaves this way if the configured synchronous standby cannot be reached.
- Give up and continue asynchronously. MySQL semi-sync does this after its timeout expires. Writes keep flowing, but you are quietly back to async, with the same data-loss window as before, often without anyone noticing.
Why active-active turns replication into a conflict problem
- A user in region A sets an order's status to
shipped. Region A commits locally. - At about the same time, a support agent routed to region B sets the same order to
cancelled. Region B commits locally. - Each region ships its change to the other.
- Each region now receives a change that contradicts what it already holds.
- Last write wins keeps the change with the later timestamp. It depends on clocks agreeing across regions, and it silently throws away the other update.
- Application-level merging works for data that can be merged, such as counters or sets. It does not help for an order that cannot be both shipped and cancelled.
- Rejecting and flagging keeps both versions for later repair. This is safe, but someone has to do the repair.
auto_increment_increment and auto_increment_offset give each writer its own interleaved range, and UUIDs avoid the issue entirely. Both fix key collisions. Neither fixes two regions updating the same row.
Teams that run active-active writes successfully usually avoid conflicts by design instead of resolving them afterwards. One approach is to give each record a home region: a user's data is written only in the region that owns it, and other regions forward writes there or only serve reads. The other is to use a database built on a consensus protocol, where a write commits only after a majority of replicas agree. Consensus removes conflicts but puts a cross-region round trip in the write path again, and it needs an odd number of voting locations. With only two regions, losing either one loses the majority, so a third region or a lightweight voter becomes required.
Failover promotion and where split-brain comes from
- Detect. Something decides region A's primary is gone. A health check failing from one location does not prove the primary is dead. It may only mean the network path between the checker and the primary is broken.
- Fence. Make sure the old primary cannot accept writes, even if it is still running. Common methods are cutting its network access, revoking the application's credentials, or forcing it read-only.
- Choose. Pick the replica with the most recent data. If you have more than one standby, compare their replay positions rather than picking one at random.
- Promote. Make that replica writable.
- Redirect. Point the application at the new primary, through DNS, a load balancer, or connection configuration.
Checking what your replication is actually doing
BASH
# PostgreSQL (on the primary): replica state, sync mode, and lag
psql -c 'SELECT application_name, client_addr, state, sync_state, write_lag, flush_lag, replay_lag FROM pg_stat_replication;'
# MySQL 8.0.22+ (on the replica); use SHOW SLAVE STATUS on older versions
mysql -e 'SHOW REPLICA STATUS\G' | grep -E 'Replica_IO_Running|Replica_SQL_Running|Seconds_Behind_Source'sync_state shows async, sync, potential, or quorum for each standby, so you can see right away whether the zero-data-loss assumption holds. In MySQL, Seconds_Behind_Source is a rough estimate based on event timestamps. It can read zero while the replica is not receiving anything at all, so always check both running threads alongside it. Watch these values during your busiest write period, not only at quiet times. The lag at peak load is the data you would lose if the primary region failed at that moment.
On managed database services, the equivalent information usually appears as replica lag metrics in the provider's console or API. Promotion behavior and fencing differ between providers, so read how your provider handles a cross-region promotion before you rely on it.
