Nâng CaoDatabase iconDatabase

Database replication là gì? Primary-replica architecture hoạt động như thế nào?

Replication scale reads với primary-replica; async replication nhanh nhưng risk mất data; sync đảm bảo zero data loss nhưng tăng write latency; Patroni tự động failover với distributed consensus.

  • Replication copy data từ primary sang một hoặc nhiều replicas để scale read throughput và high availability.
  • Streaming replication (PostgreSQL): primary liên tục stream WAL records đến replicas real-time — replica lag thường dưới 1 giây trong normal conditions; đo lag bằng pg_stat_replication view.
  • Logical replication: replicate specific tables hoặc row changes (không toàn bộ WAL) — cho phép selective replication, version upgrades zero-downtime, replicate sang different schema.
  • Synchronous vs asynchronous: sync replication (synchronous_standby_names) đảm bảo replica nhận data trước khi commit confirm — zero data loss nhưng write latency tăng (cần ít nhất một replica online); async replication confirm ngay, replica có thể lag vài giây — risk mất data nếu primary crash.
  • Replication lag measurement: SELECT now() - pg_last_xact_replay_timestamp() trên replica.
  • Failover automation: Patroni là HA solution phổ biến cho PostgreSQL — dùng etcd/ZooKeeper/Consul để distributed consensus, tự động promote replica thành primary khi primary fail, update DNS/HAProxy để redirect traffic.
  • Read-after-write consistency issue: sau khi write đến primary, immediate read từ replica có thể không thấy data mới do replication lag — cần route reads sau writes đến primary hoặc implement lag-aware routing.

Xem toàn bộ Database cùng filter theo level & chủ đề con.

Mở danh sách Database