การขยายการอ่านด้วย Hot Standby และการกระจายโหลด
เรียนรู้การถ่ายโอนการรับส่งข้อมูลสำหรับการอ่านไปยังสแตนด์บายแบบจำลองที่สตรีมข้อมูล ทำความเข้าใจความล่าช้าในการจำลองข้อมูล และกำหนดเส้นทางคำสืบค้นระหว่างฐานข้อมูลหลักกับแบบจำลองเพื่อขยายการอ่านในแนวนอน
การขยายการอ่านด้วย Hot Standby และการกระจายโหลด เป็นบทเรียน PostgreSQL Performance & Query Optimization ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน PostgreSQL Performance & Query Optimization และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Scale Reads?
A single primary can become a bottleneck when read-heavy traffic grows. By sending SELECTs to read replicas, you free the primary to handle writes and scale reads horizontally by adding more standbys.
Hot Standby Basics
A hot standby is a streaming replica that accepts read-only queries while it continuously applies changes received from the primary. It stays nearly in sync via the write-ahead log (WAL).
Enabling Read Queries on a Standby
On the standby, hot_standby must be on (the default in modern versions) so it answers queries instead of just replaying WAL silently.
SHOW hot_standby;Detecting Recovery Mode
Your application can ask any node whether it is a read-only standby. A true result means do not send writes here.
SELECT pg_is_in_recovery();Understanding Replication Lag
Standbys apply WAL slightly behind the primary, so a read may not see the very latest write. This gap is replication lag. For most read traffic it is harmless, but read-after-write flows need care.
Measuring Lag
On a standby, compare how far replay is behind the last received WAL position to estimate lag in time.
SELECT now() - pg_last_xact_replay_timestamp() AS replay_lag;Routing in the Application
The simplest pattern keeps two connection pools: one to the primary for writes, one to replicas for reads. The app chooses based on the operation.
Read-After-Write Consistency
Right after a user writes data, reading from a lagging replica may show stale results. Mitigations:
- Route that user's next reads to the primary briefly
- Wait until the replica catches up to the write's WAL position
Load Balancing Across Replicas
A pooler or proxy such as PgBouncer, Pgpool-II, or HAProxy can distribute read connections across several standbys, spreading load and providing failover if one replica goes down.
Trade-offs and Limits
Read replicas are powerful but not magic:
- They do not scale write throughput
- Lag means eventual, not immediate, consistency on replicas
- Long queries on a standby can conflict with WAL replay
Plan routing around these realities.
Watching Replication from the Primary
The primary exposes every connected standby in a stats view, including how far behind each one is. Watch this to catch a replica falling dangerously behind.
SELECT client_addr, state,
replay_lag
FROM pg_stat_replication;Quick Check
Test your read-scaling knowledge.
Recap
You learned read scaling:
- Hot standbys serve read-only queries while replaying WAL
pg_is_in_recovery()identifies a standby- Replication lag means replicas can be slightly stale
- Route writes to primary, reads to replicas; handle read-after-write
- Use a proxy to load-balance and fail over across replicas
เรียนรู้ SQL ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 22
- บทเรียน
- 88
คำถามที่พบบ่อย
บทเรียน “การขยายการอ่านด้วย Hot Standby และการกระจายโหลด” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การขยายการอ่านด้วย Hot Standby และการกระจายโหลด” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส PostgreSQL Performance & Query Optimization ให้อัปเกรดเป็น CoddyKit PRO คอร์ส PostgreSQL Performance & Query Optimization มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การขยายการอ่านด้วย Hot Standby และการกระจายโหลด”
เรียนรู้การถ่ายโอนการรับส่งข้อมูลสำหรับการอ่านไปยังสแตนด์บายแบบจำลองที่สตรีมข้อมูล ทำความเข้าใจความล่าช้าในการจำลองข้อมูล และกำหนดเส้นทางคำสืบค้นระหว่างฐานข้อมูลหลักกับแบบจำลองเพื่อขยายการอ่านในแนวนอน คุณปฏิบัติ PostgreSQL Performance & Query Optimization ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน PostgreSQL Performance & Query Optimization หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน PostgreSQL Performance & Query Optimization บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การขยายการอ่านด้วย Hot Standby และการกระจายโหลด” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน PostgreSQL Performance & Query Optimization นี้ได้ไหม
ได้ บทเรียน PostgreSQL Performance & Query Optimization ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การจัดกลุ่มการเชื่อมต่อด้วย PgBouncer
- กลยุทธ์การจำลองข้อมูลแบบสตรีมและเชิงตรรกะ
- การแบ่งส่วนข้อมูลและ PostgreSQL แบบกระจาย
- การขยายการอ่านด้วย Hot Standby และการกระจายโหลด