This topic describes how to use the shard-level replication feature in Hologres.

Overview

In Hologres V1.1 and later, Hologres allows you to set the replica count of each shard for a table group to improve the concurrency and availability of queries for the table group. To enable the replication feature, you can set the replica count of each shard when you create a table group, or change the replica count of each shard for an existing table group. The new replicas are runtime replicas that occupy the memory. Therefore, they do not increase your storage costs.

If you want to set or change the replica count of each shard, take note of the following items:
  • In a Hologres instance, data is distributed by shard. Each shard manages different data. The data is not duplicated between different shards. A complete set of data consists of the data in all shards.
  • By default, each shard has only one replica. In this case, the value of the replica_count parameter is 1. This replica serves as the leader shard. You can adjust the replica count of each shard to replicate the data of each shard in more shard replicas. In this case, shard replicas other than the leader shard serve as follower shards.
  • The leader shard is responsible for all write requests. Read requests are distributed among the leader shards and follower shards. When you use a follower shard for query, the data may be queried at a latency of 10 ms to 20 ms.
  • The default value of the replica_count parameter is 1. A value of 1 indicates that the replication feature is disabled. A value greater than 1 indicates that the replication feature is enabled. We recommend that you set this parameter to 2. A greater value indicates that more resources are required. The maximum value is 4, which indicates three follower shards. Multiple replicas cannot be deployed on a compute node because of the anti-affinity feature. Therefore, the value of the replica_count parameter must be less than or equal to the number of compute nodes. For more information about the number of compute nodes supported by different instance types, see Instance types.
  • To balance the load of compute nodes, you must decrease the number of shards when you increase the number of replicas. To pursue the best performance, make sure that the value of the shard_count parameter multiplied by the value of the replica_count parameter equals the recommended shard count of your instance.

Limits

Only Hologres V1.1 and later support the shard-level replication feature. You can check the version of your Hologres instance on the instance details page in the Hologres console. If the version of your instance is earlier than V1.1, submit a ticket to upgrade your instance.

Syntax description

  • Query the table groups in a database
    To query the table groups in the current database, execute the following statement:
    select * from hologres.hg_table_group_properties ;
  • Query the replica count of each shard for a table group
    • Sample code
      select property_value from hologres.hg_table_group_properties where tablegroup_name = 'table_group_name' and property_key = 'replica_count';
    • Parameters
      Parameter Description
      table_group_name The name of the table group that you want to query.
      replica_count The property that you want to query. Set the value to replica_count.
  • Enable the replication feature
    In the following sample code, the second command is executed to read data from replicas. The second command allows you to enable the replication feature for a database or a session. The first command allows you to enable the replication feature for a table group. You must set the replica_count parameter in the first command. The command with the replica_count parameter must be run before the command that is used to read data from replicas. Otherwise, the system overhead during data replication is so high that read operations time out. We recommend that you create an empty table group and enable the replication feature for it to reduce changes to the existing business logic and data. The logic of queries needs no changes.
    -- Enable the replication feature for a table group.
    call hg_set_table_group_property ('table_group_name', 'replica_count', '2');
    
    -- Enable the feature of reading data from replicas for a database.
    alter database <database_name> set hg_experimental_enable_read_replica = on;
    The following table describes the parameters that are contained in the commands.
    Parameter Description
    database_name The name of the database.
    hg_experimental_enable_read_replica Specifies whether to enable the feature of reading data from replicas for a database. For each database, you need to enable the feature only once.
    • on: enables the feature.
    • off: disables the feature.
    hg_set_table_group_property The function that is used to set the replica count of each shard for a table group.
    • table_group_name: the name of the table group that you want to manage.
    • replica_count: the replica count of each shard for the table group. The replica count must be less than or equal to the number of compute nodes. In most cases, you can set the parameter to 2.
    • If you set the replica_count parameter to the default value 1, the replication feature is disabled. A value greater than 1 indicates that the replication feature is enabled.
  • Disable the replication feature
    • Sample code
      -- Modify the replica_count parameter to disable the replication feature.
      call hg_set_table_group_property ('table_group_name', 'replica_count', '1');
    • Parameters
      Parameter Description
      hg_set_table_group_property The function that is used to set the replica count of each shard for a table group.
      • table_group_name: the name of the table group that you want to manage.
      • replica_count: the replica count of each shard for the table group.
      • If you set the replica_count parameter to the default value 1, the replication feature is disabled. A value greater than 1 indicates that the replication feature is enabled.