PostgreSQL asymmetric replication
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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
- Set Up the Primary NodeEdit the
postgresql.conffile to enable the necessary settings for replication.
Ensure to add replication privileges to the pg_hba.conf file.
- Initialize the Standby NodeUse
pg_basebackupto create a base backup of the primary.
- Configure Standby to Follow PrimaryCreate a
recovery.conf(or editstandby.signalin newer versions) to specify the primary node.
- Enable Logical Replication for Asymmetric SetupCreate a publication on the primary.
On the standby or logical node, create a subscription.
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
| Aspect | Description |
| Primary Node | Handles writes and is the source of truth. |
| Standby Node | Read-only replicas, ideal for load distribution. |
| Cascading Replication | Standby nodes forward changes, reducing load on the primary. |
| Logical Replication | Allows high-level replication for more flexible use cases. |
| Use Cases | Scalability, specialized nodes for analytics, disaster recovery, etc. |
| Challenges | Configuration complexity, potential replication lag. |
Additional Details
- Monitoring Tools: Utilizing PostgreSQL extensions like
pg_stat_statementscan 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
- Postgresql replication in rails with data-fabric gem
- PostgreSQL restoration throwing error replication slot does not exist
- Postgresql slave for Mysql Master. Possible?
- PostgreSQL Slony replication - scheduled sync
- PostgreSQL bitnami Helm Chart does not update the user password
- PostgreSQL error Fatal role username does not exist
- Practical uses for AtomicInteger
- PRECONDITION_FAILED Delivery Acknowledge Timeout on Celery & RabbitMQ with Gevent and concurrency

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.