PostgreSQL
asymmetric replication
database replication
database management
data synchronization

PostgreSQL asymmetric replication

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

PostgreSQL, a powerful open-source object-relational database system, has a wide range of features including advanced data types, powerful indexing techniques, and robust performance optimizations. One of the less commonly discussed features of PostgreSQL is its support for asymmetric replication.

Asymmetric replication refers to a replication topology where not all database nodes are equal in terms of roles or capabilities. This contrasts with symmetric replication, where nodes generally have equivalent roles. Asymmetric replication can be particularly useful in scenarios where read and write loads are unevenly distributed, or where certain nodes have specialized purposes.

Key Concepts of Asymmetric Replication

Asymmetric replication in PostgreSQL usually involves primary and standby roles which can be further extended using techniques like cascading replication and logical replication.

  • Primary Node: The primary node handles all the write operations. It is the source of truth for the data.
  • Standby Node: These nodes replicate data from the primary node and are usually read-only replicas.
  • Cascading Replication: Allows a standby node to forward replication data to other standbys, reducing the load on the primary node.
  • Logical Replication: Involves replicating changes at a higher level, like table rows and columns, allowing more flexibility in what is replicated.

Configuration of Asymmetric Replication

Setting up asymmetric replication in PostgreSQL can involve a combination of streaming replication and logical replication, depending on the use case.

Step-by-step Configuration Example

  1. Set Up the Primary Node
    Edit the postgresql.conf file to enable the necessary settings for replication.
plaintext
   wal_level = replica
   max_wal_senders = 10

Ensure to add replication privileges to the pg_hba.conf file.

plaintext
   host replication replica_user <standby_ip> md5
  1. Initialize the Standby Node
    Use pg_basebackup to create a base backup of the primary.
bash
   pg_basebackup -h <primary_ip> -D /var/lib/pgsql/12/data -U replica_user -vP --wal-method=stream
  1. Configure Standby to Follow Primary
    Create a recovery.conf (or edit standby.signal in newer versions) to specify the primary node.
plaintext
   standby_mode = 'on'
   primary_conninfo = 'host=<primary_ip> port=5432 user=replica_user password=<password>'
  1. Enable Logical Replication for Asymmetric Setup
    Create a publication on the primary.
sql
   CREATE PUBLICATION my_publication FOR ALL TABLES;

On the standby or logical node, create a subscription.

sql
   CREATE SUBSCRIPTION my_subscription CONNECTION 'host=<primary_ip> dbname=<dbname> user=replica_user password=<password>' PUBLICATION my_publication;

Benefits and Use Cases

  • Scalability: Asymmetric replication allows distributing load across different nodes, improving scalability for read-heavy applications.
  • Specialization: Nodes can be optimized for specific roles, such as analytics or reporting, without affecting the primary's performance.
  • Disaster Recovery: Standby nodes can take over in case of primary node failure, ensuring business continuity.

Limitations

  • Complexity: Asymmetric replication setups can be more complex, needing careful configuration and monitoring.
  • Latency: Data consistency might be affected due to replication lag, impacting real-time applications.

Summary Table

AspectDescription
Primary NodeHandles writes and is the source of truth.
Standby NodeRead-only replicas, ideal for load distribution.
Cascading ReplicationStandby nodes forward changes, reducing load on the primary.
Logical ReplicationAllows high-level replication for more flexible use cases.
Use CasesScalability, specialized nodes for analytics, disaster recovery, etc.
ChallengesConfiguration complexity, potential replication lag.

Additional Details

  • Monitoring Tools: Utilizing PostgreSQL extensions like pg_stat_statements can help monitor replication lag and performance.
  • Security: Secure connections and proper authentication should be ensured to protect against unauthorized access. Use SSL/TLS for encrypted connections between nodes.
  • Backup Strategies: Regular backups and testing of failover scenarios are crucial for maintaining data integrity.

Conclusion

PostgreSQL's asymmetric replication offers a powerful mechanism to optimize database performance and reliability across diverse use cases. While it introduces additional complexity, the benefits in scalability, specialization, and disaster recovery make it a worthwhile endeavor for modern database architectures. Understanding and correctly implementing asymmetric replication can lead to significant advantages in efficiently managing database systems.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.