Synchronous vs Asynchronous Replication: What is the Difference?
Compare synchronous and asynchronous database replication, their durability guarantees, and the latency trade-offs involved.
Expected Interview Answer
Synchronous replication waits for at least one replica to confirm it received a write before the primary reports the transaction as committed, while asynchronous replication commits on the primary immediately and lets replicas catch up afterward.
Synchronous replication guarantees that a confirmed commit already exists on more than one server, so a primary failure right after commit cannot lose that data โ the cost is added write latency, since every commit waits on a network round trip to the replica. Asynchronous replication commits faster because it never waits on replicas, but it opens a window where a primary crash can lose the most recent transactions that had not yet reached any replica. Many systems use a middle ground, such as semi-synchronous replication, to balance durability against latency.
- Synchronous: zero data loss on primary failure
- Asynchronous: lower write latency, higher throughput
- Choice can be tuned per transaction or per replica
- Semi-synchronous offers a practical middle ground
AI Mentor Explanation
A synchronous scorer refuses to announce a run as official until the third umpire's screen also shows it, so both records match before anyone hears the score โ safe, but the announcement is a beat slower. An asynchronous scorer calls the run immediately and only updates the third umpire's screen moments later, which is faster for the crowd but risks the two records briefly disagreeing if something interrupts the relay. This exact trade-off between waiting for confirmation and moving on immediately is what separates synchronous from asynchronous replication.
Step-by-Step Explanation
Step 1
Client sends a write
The primary receives the transaction and prepares to apply it.
Step 2
Synchronous path
The primary waits for the replica to acknowledge receipt before returning "commit" to the client.
Step 3
Asynchronous path
The primary commits and returns success immediately, streaming the change to replicas afterward.
Step 4
Choose based on requirements
Pick synchronous for zero-data-loss guarantees, asynchronous for lower latency and higher throughput.
What Interviewer Expects
- Clear statement of what "wait for acknowledgment" means
- Explicit trade-off between durability and latency
- Mention of the data-loss window in asynchronous mode
- Awareness of semi-synchronous as a middle ground
Common Mistakes
- Saying asynchronous replication never loses data
- Not mentioning the latency cost of synchronous replication
- Confusing this with read-replica vs write-primary distinction
- Ignoring semi-synchronous as an option
Best Answer (HR Friendly)
โSynchronous replication waits for a replica to confirm a write before telling the client it succeeded, so no confirmed data can be lost, but it is slower. Asynchronous replication confirms writes immediately and syncs replicas afterward, which is faster but means a crash right after a write could lose that last bit of data.โ
Code Example
-- Synchronous: wait for at least one replica to confirm the write
ALTER SYSTEM SET synchronous_commit = 'on';
ALTER SYSTEM SET synchronous_standby_names = 'replica_1';
-- Asynchronous: commit locally, replicate afterward
ALTER SYSTEM SET synchronous_commit = 'off';
-- A write under synchronous mode blocks here until replica_1 acks:
INSERT INTO Orders (customer_id, total) VALUES (101, 250.00);Follow-up Questions
- What is semi-synchronous replication and when is it used?
- How does synchronous replication affect write throughput at scale?
- What data-loss scenario is unique to asynchronous replication?
- How would you choose replication mode for a financial ledger vs a social feed?
MCQ Practice
1. In synchronous replication, when does the primary report a commit as successful?
Synchronous replication waits for replica acknowledgment before confirming the commit to the client.
2. What is the main risk of asynchronous replication?
Because the primary commits without waiting, a crash before replication completes can lose the latest writes.
3. What does semi-synchronous replication attempt to balance?
Semi-synchronous replication waits for partial acknowledgment, aiming for a middle ground between full durability and low latency.
Flash Cards
Synchronous replication? โ Primary waits for replica acknowledgment before confirming a commit.
Asynchronous replication? โ Primary commits immediately; replicas catch up afterward.
Main risk of asynchronous? โ Recent writes can be lost if the primary fails before replicating.
Main cost of synchronous? โ Higher write latency due to waiting on replica acknowledgment.