Synchronous vs Asynchronous Replication
Understand the durability and latency trade-offs between synchronous and asynchronous physical replication in PostgreSQL.
Synchronous vs Asynchronous Replication is a free Advanced PostgreSQL: Indexing, Partitioning, Replication lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Advanced PostgreSQL: Indexing, Partitioning, Replication learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Two Modes of Streaming
Physical streaming replication can run in two modes: asynchronous (the default) and synchronous. They differ in when the primary considers a commit complete.
Asynchronous Replication
In async mode the primary commits as soon as the WAL is flushed locally. It does not wait for the standby. This is fast but a crash can lose the most recent commits not yet shipped.
Synchronous Replication
In sync mode the primary waits until at least one standby confirms it received (or applied) the WAL before reporting commit success. This guarantees zero data loss for confirmed commits at the cost of extra latency.
Configuring Sync Standbys
Name your standbys with application_name and list them in synchronous_standby_names on the primary.
-- on the primary, in postgresql.conf
synchronous_standby_names = 'standby1, standby2';Quorum-based Sync
You can require any N of several standbys to confirm, balancing durability and availability.
synchronous_standby_names = 'ANY 1 (standby1, standby2)';synchronous_commit Levels
The synchronous_commit setting controls how far a commit must travel:
off/local— local onlyremote_write— standby received WALon— standby flushed WAL to diskremote_apply— standby applied WAL (visible on replica)
Per-transaction Control
This setting can be changed per transaction, letting unimportant writes go fast while critical writes wait for confirmation.
SET synchronous_commit = 'remote_apply';
INSERT INTO payments (amount) VALUES (100);Availability Risk of Sync
If the only synchronous standby goes down, commits on the primary will block until a standby returns. Always pair sync replication with multiple candidates or quorum settings.
Checking Replication State
Inspect the live state from the primary using pg_stat_replication.
SELECT application_name, state, sync_state
FROM pg_stat_replication;Choosing a Mode
Use async for throughput and geographically distant replicas. Use sync when losing even one committed transaction is unacceptable, such as financial ledgers.
Latency Reality
Synchronous commit latency is roughly the network round-trip to the standby plus its fsync time. Keeping sync standbys on a low-latency link is essential.
Quick Check
What is the key trade-off of synchronous replication?
Recap
You compared asynchronous and synchronous replication, configured synchronous_standby_names and synchronous_commit levels, and learned the durability-vs-latency-vs-availability trade-offs.
Frequently asked questions
Is the “Synchronous vs Asynchronous Replication” lesson free?
Yes — the full text of “Synchronous vs Asynchronous Replication” is free to read here on the web, and the Advanced PostgreSQL: Indexing, Partitioning, Replication course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Advanced PostgreSQL: Indexing, Partitioning, Replication course, upgrade to CoddyKit PRO.
What will I learn in “Synchronous vs Asynchronous Replication”?
Understand the durability and latency trade-offs between synchronous and asynchronous physical replication in PostgreSQL. You practise Advanced PostgreSQL: Indexing, Partitioning, Replication with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Advanced PostgreSQL: Indexing, Partitioning, Replication?
No prior experience is required. Advanced PostgreSQL: Indexing, Partitioning, Replication on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Synchronous vs Asynchronous Replication” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Advanced PostgreSQL: Indexing, Partitioning, Replication lesson?
Yes. Every Advanced PostgreSQL: Indexing, Partitioning, Replication lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Understanding Replication Concepts
- Physical Replication (Streaming)
- Setting Up a Standby Server
- Synchronous vs Asynchronous Replication