Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Explain the difference between synchronous and asynchronous replication in PostgreSQL streaming replication. What does a commit wait for in each case, and what can be lost if the primary crashes?

With asynchronous replication, a PostgreSQL commit returns as soon as the primary has flushed the WAL to its own disk; standbys receive it a moment later, so the last committed transactions can be lost if the primary dies. With synchronous replication, the commit also waits until the standbys named in synchronous_standby_names confirm, and synchronous_commit decides what they confirm: received (remote_write), flushed to disk (on) or replayed (remote_apply). The price is a network round trip on every commit, and writes stop if no synchronous standby answers.

Why this question is asked

Replication is how we keep a copy of the database ready for when the primary fails. Whether that copy is complete at the moment of failure depends on one choice: does a commit wait for the standby or not. That choice decides how much data we can lose, how fast every write is, and whether a standby outage can stop our application. Interviewers ask it because many engineers assume a standby is always up to date, and that assumption causes real data loss. In this answer, we will recap WAL and streaming replication, follow a commit in async and sync mode, go through the synchronous_commit levels, look at the costs, and see how quorum settings soften them.

1A Quick Recap of WAL and Streaming Replication

PostgreSQL writes every change first to the write-ahead log, the WAL, a sequential file of change records. A transaction counts as durable once its commit record is flushed to the WAL on disk. If the server crashes, it replays the WAL on restart and gets back to a consistent state.

Streaming replication reuses that log. A standby connects to the primary, a process on the primary called the WAL sender streams the WAL records to it, and a WAL receiver process on the standby writes them to its own disk, where a startup process replays them into the standby's data files. The standby is therefore a physical copy that trails the primary by however much WAL is still on its way.

2What a Commit Waits For in Asynchronous Mode

Asynchronous is the default. When our application sends COMMIT, the primary flushes the commit record to its own WAL and immediately tells the client the transaction succeeded. The WAL sender ships those records to the standby independently, usually a few milliseconds later, sometimes seconds later under heavy load.

So there is a window. If the primary's disk is destroyed, or the server dies and we promote the standby, any transaction that was committed on the primary but not yet received by the standby is gone. The client was told success, yet the new primary has never heard of it. On a busy system with 1 ms of lag, that is a handful of transactions. With 30 seconds of lag during a batch load, it can be thousands.

Step throughAsynchronous commit
AppPrimaryStandby
COMMIT
flush WAL to local disk
success
stream WAL a moment later
write, flush, replay

5 messages in order. Play them, or step through them one at a time.

The client hears success before the standby has the change. A crash in between loses it.

3What a Commit Waits For in Synchronous Mode

Synchronous mode is switched on by naming standbys in synchronous_standby_names. Each standby identifies itself with application_name in its primary_conninfo. Now the primary flushes its own WAL, then waits until the chosen standbys report back, and only then answers the client. If the primary dies after that, the standby already has the commit.

The synchronous_commit setting decides how far the standby must have got before it reports. With remote_write, the standby has received the WAL and handed it to its operating system, but not yet flushed it, so we survive a primary crash but could lose data if both servers lose power together. With on, the default, the standby has flushed the WAL to its disk. With remote_apply, the standby has also replayed it, so a query on that standby immediately sees the new row, which matters when we read from replicas. Two more values exist: local waits only for the primary's own flush, and off does not even wait for that, risking the last few hundred milliseconds of commits on a primary crash.

SQL
-- on the primary, postgresql.conf or ALTER SYSTEM
ALTER SYSTEM SET synchronous_standby_names = 'FIRST 1 (s1, s2)';
ALTER SYSTEM SET synchronous_commit = 'on';
SELECT pg_reload_conf();
SELECT application_name, sync_state FROM pg_stat_replication;
-- s1 | sync
-- s2 | potential (takes over if s1 disappears)

Notice the sync_state column. With FIRST 1, s1 is the synchronous standby and s2 is the backup candidate. That is the priority form: the first available name in the list is the one we wait for.

4The Costs: Latency and Blocked Writes

Every synchronous commit now includes a network round trip to the standby plus its disk flush. Within one data centre that adds well under a millisecond; across availability zones about 1 to 2 ms; across regions 30 to 100 ms or more. For a service doing many small transactions, that can be the difference between 2 ms and 80 ms per request.

There is a sharper cost too. If the only synchronous standby goes down, the primary keeps waiting. Commits hang, because PostgreSQL will not tell a client success without the promise it was configured to give. Note what actually happens: the transaction is already committed locally, and if the client cancels the wait, PostgreSQL warns that it committed locally but might not have been replicated. So synchronous replication trades availability of writes for durability.

5Quorum Commit: Tolerating One Slow Standby

To soften that, PostgreSQL offers the quorum form. With ANY 1 (s1, s2), a commit waits for whichever of the two standbys answers first. If s1 is slow or down, s2 confirms and writes continue. Both standbys must fail before commits block.

SQL
ALTER SYSTEM SET synchronous_standby_names = 'ANY 1 (s1, s2)';
SELECT pg_reload_conf();
SELECT application_name, sync_state FROM pg_stat_replication;
-- s1 | quorum
-- s2 | quorum

So quorum gives us the same guarantee, at least one other server has every acknowledged commit, while removing a single standby as a cause of write outages. Latency also improves slightly, because we wait for the faster of the two. The catch is that after a failover we must promote the standby that actually has the latest WAL, which a failover manager such as Patroni checks for us.

6Follow-up questions

Can synchronous_commit be set per transaction?

Yes. synchronous_commit can be changed at any time, for the server, a database, a role, a session or a single transaction. Inside a transaction we write SET LOCAL synchronous_commit = local, or off, and the setting applies only until that transaction ends. This lets us keep payments fully synchronous while letting, for example, a click-tracking insert skip the standby wait. synchronous_standby_names, on the other hand, is a server-wide setting.

Does synchronous replication mean the standby is always readable with the newest data?

Only with remote_apply. With on, the standby has the WAL safely on disk but may not have replayed it yet, so a read on the standby right after a commit can still return the old row. If an application reads its own writes from replicas, remote_apply closes that gap at the cost of waiting for replay as well.

What happens to a client waiting for a synchronous standby that never answers?

The commit waits indefinitely. If the client or an operator cancels it, PostgreSQL reports a warning that the transaction has already committed locally but might not have been replicated to the standby. That is why monitoring sync_state and having a second candidate, via FIRST or ANY, is essential.

7Conclusion

In this answer, we saw that asynchronous replication confirms a commit once the primary's own WAL is flushed, so a crash can lose whatever had not reached the standby yet, while synchronous replication waits for named standbys, with synchronous_commit choosing between received, flushed and replayed. We saw that this adds a round trip to every commit and can block writes, and that the quorum form ANY 1 (s1, s2) keeps the guarantee while tolerating one failed standby.

What to say in the interviewAbout 90 seconds aloud

Open with

In PostgreSQL streaming replication, the primary writes every change to the WAL and streams it to standbys. In asynchronous mode, which is the default, a commit returns as soon as the primary has flushed the commit record to its own disk.

Then, if they want more

The standby gets it shortly after, so if the primary dies in that window, transactions the client was told succeeded are missing on the promoted standby. With a busy batch job and seconds of lag, that can be a lot of data.

In synchronous mode, we list standbys in synchronous_standby_names, and the commit also waits for them. synchronous_commit says what they must confirm: remote_write means the standby received it into its OS, on means it flushed it to disk, and remote_apply means it replayed it, so reads on that standby see the change.

You can also set local or off for less important work, even per transaction with SET LOCAL. The cost is a network round trip on every commit, about a millisecond or two across zones, and if no synchronous standby is available, commits just wait, so writes stop.

To avoid one standby causing an outage, I would use the quorum form, ANY 1 (s1, s2), so whichever standby answers first satisfies the commit, and pair it with a failover manager that promotes the most up-to-date one.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Explain the difference between synchronous and asynchronous replication in PostgreSQL...": https://scrivio-interview-library.netlify.app/databases/replication-and-ha/88bb956f35/