Last modified: December 03, 2024

This article is written in: 🇺🇸

Synchronous and Asynchronous Replication

Replication is a vital concept in database systems, involving the copying of data from one database server, known as the primary, to one or more other servers called replicas. This process enhances data availability, fault tolerance, and load balancing across the system. Understanding the two main replication strategies—synchronous and asynchronous replication—is crucial for designing robust and efficient database architectures.

Understanding Replication Strategies

At its core, replication ensures that data is consistently available across multiple servers. The key difference between synchronous and asynchronous replication lies in how and when data changes are propagated from the primary server to the replicas.

Synchronous Replication

In synchronous replication, every write operation on the primary database is immediately propagated to the replicas. The primary server waits for acknowledgments from all replicas before confirming the transaction to the client. This means that data is consistent across all servers at any given moment.

How Synchronous Replication Works:

  1. A client sends a write request to the primary server.
  2. The primary server writes the data and sends the changes to all replicas.
  3. Each replica writes the data and sends an acknowledgment back to the primary server.
  4. Once all acknowledgments are received, the primary server confirms the transaction to the client.

Illustrative Diagram:

Client Write Request
          |
          v
+--------------------+
|   Primary Server   |
+---------+----------+
          |
Sends Data to Replicas
          |
          v
+---------+----------+       +---------+----------+
|    Replica 1       |       |    Replica 2       |
+--------------------+       +--------------------+
          ^                            ^
          |                            |
Acknowledgment from Replica 1          |
          |                            |
          +----------------------------+
                       |
Acknowledgment from Replica 2
                       |
                       v
Transaction Confirmed to Client

Advantages of Synchronous Replication:

Disadvantages of Synchronous Replication:

Asynchronous Replication

Asynchronous replication allows the primary server to confirm transactions without waiting for replicas to acknowledge the data writes. Data changes are sent to replicas after the transaction has been committed on the primary server, which means there may be a delay before replicas are updated.

How Asynchronous Replication Works:

  1. A client sends a write request to the primary server.
  2. The primary server writes the data and immediately confirms the transaction to the client.
  3. The primary server queues the data changes for replication.
  4. Replicas receive the data changes asynchronously and update their data.

Illustrative Diagram:

Client Write Request
          |
          v
+--------------------+
|   Primary Server   |
+---------+----------+
          |
Transaction Confirmed to Client
          |
          v
Data Changes Queued for Replication
          |
          v
+---------+----------+       +---------+----------+
|    Replica 1       |       |    Replica 2       |
+--------------------+       +--------------------+

Advantages of Asynchronous Replication:

Disadvantages of Asynchronous Replication:

Choosing Between Synchronous and Asynchronous Replication

Selecting the appropriate replication strategy depends on the specific needs of your application and infrastructure.

When to Use Synchronous Replication:

When to Use Asynchronous Replication:

Best Practices for Implementing Replication

Implementing replication effectively requires careful planning and consideration of several factors.

Understanding Application Requirements:

Monitoring and Maintenance:

Optimizing Network Infrastructure:

Data Safety Measures:

Example: Implementing Replication in PostgreSQL

Let's explore how to set up both synchronous and asynchronous replication in PostgreSQL.

Setting Up Synchronous Replication

On the Primary Server:

Edit the postgresql.conf file to include:

wal_level = replica
synchronous_commit = on
synchronous_standby_names = 'replica1'
max_wal_senders = 3

On the Replica Server:

Edit the postgresql.conf file:

hot_standby = on

Create a standby.signal file in the data directory to enable standby mode.

Starting Replication:

  1. Take a base backup of the primary server using pg_basebackup.
  2. Restore the backup on the replica server.
  3. Start the replica server; it will connect to the primary and begin synchronous replication.

Behavior:

Setting Up Asynchronous Replication

On the Primary Server:

Edit the postgresql.conf file:

wal_level = replica
synchronous_commit = off
max_wal_senders = 3

On the Replica Server:

Same as for synchronous replication.

Behavior:

Table of Contents

    Synchronous and Asynchronous Replication
    1. Understanding Replication Strategies
      1. Synchronous Replication
      2. Asynchronous Replication
    2. Choosing Between Synchronous and Asynchronous Replication
    3. Best Practices for Implementing Replication
    4. Example: Implementing Replication in PostgreSQL
      1. Setting Up Synchronous Replication
      2. Setting Up Asynchronous Replication