Question: What is PostgreSQL bidirectional replication?


Bidirectional replication in PostgreSQL, also known as multi-master replication, allows data to be replicated between two or more PostgreSQL servers, enabling both read and write operations on any server in the cluster. This setup enhances the availability, scalability, and redundancy of database systems.

Key Concepts

  • Synchronous vs Asynchronous Replication: Bidirectional replication can be configured for either synchronous or asynchronous replication, impacting data consistency and system performance differently.
  • Conflict Resolution: Essential for maintaining data integrity across all nodes, dealing with situations where the same record is modified on different servers at the same time.
  • Load Balancing: Distributes read and write operations across multiple servers to improve performance.

Implementation Tools

PostgreSQL does not natively support true bidirectional replication out of the box. However, third-party tools and extensions such as BDR (Bi-Directional Replication) and pglogical provide robust solutions.

BDR Example

BDR (Bi-Directional Replication) is one of the most popular tools for implementing bidirectional replication in PostgreSQL. Here's a high-level overview of setting it up:

  1. Install BDR Plugin: Ensure both servers have PostgreSQL and the BDR plugin installed.

  2. Configure PostgreSQL: Modify postgresql.conf to include BDR-specific parameters such as shared_preload_libraries, max_wal_senders, and others relevant to replication settings.

  3. Initialize BDR: On each node, initialize BDR and join other nodes to form a BDR group. Nodes will then synchronize and start replicating changes.

-- Run on each node to create a BDR group SELECT bdr.bdr_group_create( local_node_name := 'node_name', node_external_dsn := 'host=hostname port=5432 dbname=db_name' ); -- Then join additional nodes SELECT bdr.bdr_group_join( local_node_name := 'node_name', node_external_dsn := 'host=hostname port=5432 dbname=db_name', join_using_dsn := 'host=other_hostname port=5432 dbname=db_name' );


  • Monitoring and Management: Proper monitoring tools should be in place to track replication lag, conflicts, and overall health of the system.
  • Conflict Handling: Understand and plan for conflict resolution strategies that suit your application use case.
  • Performance Overheads: Be aware of the additional load and latency introduced by replication activities on your system.

Bidirectional replication offers significant advantages in terms of high availability and flexibility for PostgreSQL databases. However, it requires careful planning, setup, and maintenance to ensure data consistency and system reliability.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book
Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.