การจำลองข้อมูลแบบต่อเนื่อง
ใช้งานการจำลองข้อมูลแบบต่อเนื่องเพื่อกระจายภาระของเซิร์ฟเวอร์หลัก โดยให้เซิร์ฟเวอร์สำรองจำลองข้อมูลจากเซิร์ฟเวอร์สำรองอื่น
การจำลองข้อมูลแบบต่อเนื่อง เป็นบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
เรียนรู้ Advanced PostgreSQL: Indexing, Partitioning, Replication ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 11
- บทเรียน
- 44
คำถามที่พบบ่อย
บทเรียน “การจำลองข้อมูลแบบต่อเนื่อง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การจำลองข้อมูลแบบต่อเนื่อง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การจำลองข้อมูลแบบต่อเนื่อง”
ใช้งานการจำลองข้อมูลแบบต่อเนื่องเพื่อกระจายภาระของเซิร์ฟเวอร์หลัก โดยให้เซิร์ฟเวอร์สำรองจำลองข้อมูลจากเซิร์ฟเวอร์สำรองอื่น คุณปฏิบัติ Advanced PostgreSQL: Indexing, Partitioning, Replication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced PostgreSQL: Indexing, Partitioning, Replication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การจำลองข้อมูลแบบต่อเนื่อง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication นี้ได้ไหม
ได้ บทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจำลองข้อมูลแบบต่อเนื่อง
- การจำลองข้อมูลแบบหลายหลัก (BDR)
- สล็อตการจำลองข้อมูลและการจัดการ WAL
- การถอดรหัสเชิงตรรกะและการบันทึกการเปลี่ยนแปลงข้อมูล