级联复制
实现级联复制,让备用服务器从其他备用服务器进行复制,从而分担主服务器的负载。
级联复制 是 CoddyKit 上的免费 Advanced PostgreSQL: Indexing, Partitioning, Replication 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Advanced PostgreSQL: Indexing, Partitioning, Replication 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Cascading Replication?
Welcome to Cascading Replication! In typical PostgreSQL replication, all standby servers connect directly to the primary server.
However, for larger setups or when you need many read replicas, this can put a lot of load on the primary. Cascading replication offers a solution.
It means a standby server can itself act as a primary for other standbys, forming a chain.
How the Replication Chain Works
Imagine a chain: The primary server sends its Write-Ahead Log (WAL) to a first standby (let's call it Standby A).
Then, Standby A, instead of just consuming the WAL, also acts as a source, sending that same WAL data to a second standby (Standby B).
This creates a flow: Primary → Standby A → Standby B. Standby A is an 'intermediate' standby.
Key Benefits of Cascading
Why go through the trouble of chaining standbys? Cascading replication offers several advantages:
- Reduced Load on Primary: The primary only needs to send WAL to Standby A, not to all standbys directly.
- Scalability for Reads: You can create many read replicas without overwhelming the primary's network or I/O.
- Geographical Distribution: Standbys can be spread across different data centers, with intermediate standbys bridging the distance.
Preparing the First Standby (Standby A)
The first step is to set up Standby A as a regular streaming replication standby. This involves taking a base backup from the primary and configuring Standby A to connect to the primary.
The key here is that Standby A must also be configured to send WAL files to other standbys, not just receive them.
Configuring Standby A to be a Source
For Standby A to act as a source for other standbys, you need to adjust its PostgreSQL configuration (`postgresql.conf`).
The important parameters are wal_level and max_wal_senders. hot_standby should also be on to allow queries.
wal_level = replica
max_wal_senders = 5 -- Or more, for primary and other standbys
hot_standby = onStandby B: Connecting to Standby A
Now, for Standby B, instead of pointing its primary_conninfo to the actual primary server, you point it to Standby A.
This tells Standby B to receive its WAL stream from Standby A.
primary_conninfo = 'host=standby_a_ip port=5432 user=replication_user password=YOUR_PASSWORD'
primary_slot_name = 'standby_b_slot' -- Optional, for guaranteed WAL
hot_standby = onInitializing Standby B's Base
Just like any new standby, Standby B needs an initial copy of the data. You can take this base backup directly from the primary, or, more commonly in a cascading setup, you can take it from Standby A.
Using Standby A for the base backup further reduces load on the primary.
# On Standby B, after stopping PostgreSQL
pg_basebackup -h standby_a_ip -p 5432 -U replication_user -D /var/lib/postgresql/data -F p -Xs -P -RMonitoring the Cascade
It's crucial to monitor the health of your cascading setup. The pg_stat_replication view is your best friend.
On the primary, you'll see Standby A connected. On Standby A, you'll see Standby B connected.
-- On Standby A (or Primary):
SELECT application_name, client_addr, state, sync_state FROM pg_stat_replication;Practical Use Cases
Cascading replication shines in scenarios where:
- You have a high number of read queries and need many read replicas.
- You want to offload backup operations to a standby, but also need other standbys for reads.
- You are distributing your database across multiple regions and want to minimize cross-region traffic from the primary.
- You're building complex disaster recovery tiers.
Cascading Replication Quiz
Let's test your understanding of cascading replication.
Recap: Cascading Replication
In this lesson, we explored cascading replication, a powerful architecture for scaling PostgreSQL read replicas and distributing replication load.
You learned how standbys can form a chain, with one standby replicating from another, and the key configurations and benefits, such as reduced primary load and enhanced read scalability.
This technique is essential for robust, distributed PostgreSQL environments.
常见问题解答
「级联复制」课时是免费的吗?
是的 — 「级联复制」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程的其余内容,请升级到 CoddyKit PRO。 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程共包含 4 节课。
「级联复制」这节课中我会学到什么?
实现级联复制,让备用服务器从其他备用服务器进行复制,从而分担主服务器的负载。 你通过在浏览器中直接运行的动手代码来练习 Advanced PostgreSQL: Indexing, Partitioning, Replication,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Advanced PostgreSQL: Indexing, Partitioning, Replication 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Advanced PostgreSQL: Indexing, Partitioning, Replication 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「级联复制」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课中编写并运行代码吗?
能。每节 Advanced PostgreSQL: Indexing, Partitioning, Replication 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。