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. Install AlloyDB Omni onto the system you want to host the replica.

    When you run the alloydb database-server install command, include the flags that are specific to creating a replica:

    sudo alloydb database-server install \
      --data-dir=DATA_DIR \
      --pg-port=PORT \
      --replica-source-ip=SOURCE_IP \
      --replica-source-port=SOURCE_PORT

    Replace the following:

    • DATA_DIR: the file system path you want this replica to use for its data directory.

    • PORT: the TCP port that you want AlloyDB Omni to accept connections on. If you don't provide this value, then AlloyDB Omni applies a default value of 5432.

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

    • SOURCE_PORT: the TCP port of the primary AlloyDB Omni instance to replicate from. If you don't provide this value, then AlloyDB Omni applies a default value of 5432.

  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.