Create a read-only replica

To increase read throughput and availability, you can create an AlloyDB Omni replica server for an existing AlloyDB Omni server. A replica server provides a read-only clone of your primary database server. It continuously updates its own data to reflect changes to your primary server's data.

The following sections provide steps to configure your AlloyDB Omni primary server for replication, configure the replica server, and verify replication status on the primary server.

Configure your primary server for replication

  1. Update the network or firewall settings of your primary server's host so that it allows ingress traffic from the replica server's host through TCP port 5432.

  2. Add the following lines in the /var/alloydb/config/pg_hba.conf file:

    host all                alloydbreplica     IP_RANGE trust
    host replication        alloydbreplica     IP_RANGE trust
    host alloydbmetadata    alloydbmetadata    IP_RANGE trust
    

    Replace IP_RANGE with the IP range, in CIDR notation, of the subnet where your replica machine is located. For example, 203.0.113.0/24.

  3. Restart your primary server:

    sudo alloydb database-server stop
    sudo alloydb database-server start
    

Create and configure the replica server

To create and configure an AlloyDB Omni replica server, complete the following steps:

  1. Log in to the system that hosts your replica server and install AlloyDB Omni.

    As the final step in the procedure, make sure to run the sudo alloydb database-server install command with the --replica-source-ip flag:

    sudo alloydb database-server install \
      --data-dir=DATA_DIR \
      --replica-source-ip=SOURCE_IP
    

    Replace the following:

    • DATA_DIR: the filesystem path you want this replica to use for its data directory

    • SOURCE_IP: the IP address of the primary AlloyDB Omni instance to replicate from

  2. Ensure the replica has network connectivity to the primary server:

    ping SOURCE_IP
    
  3. Start your replica:

    sudo alloydb database-server start
    

Verify replication status on the primary server

To verify replication is configured properly, run the following command on the primary server's host:

  docker exec -it pg-service psql -h localhost -U alloydbadmin -c "select * from pg_stat_replication"

The output table contains one row for every replica connected to your primary database server.

After you set up replication, all inserts, updates, and deletions to rows in your primary database server become readable on your replica within seconds.