同步复制与异步复制
了解 PostgreSQL 中同步物理复制与异步物理复制在持久性和延迟之间的权衡。
同步复制与异步复制 是 CoddyKit 上的免费 Advanced PostgreSQL: Indexing, Partitioning, Replication 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Advanced PostgreSQL: Indexing, Partitioning, Replication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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.
常见问题解答
「同步复制与异步复制」课时是免费的吗?
是的 — 「同步复制与异步复制」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程的其余内容,请升级到 CoddyKit PRO。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。
「同步复制与异步复制」这节课中我会学到什么?
了解 PostgreSQL 中同步物理复制与异步物理复制在持久性和延迟之间的权衡。 你通过在浏览器中直接运行的动手代码来练习 Advanced PostgreSQL: Indexing, Partitioning, Replication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Advanced PostgreSQL: Indexing, Partitioning, Replication 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「同步复制与异步复制」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课中编写并运行代码吗?
能。每节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。