Help Center/ Distributed Message Service for Kafka/ Best Practices/ Using MirrorMaker to Synchronize Data Across Clusters
Updated on 2024-10-24 GMT+08:00

Using MirrorMaker to Synchronize Data Across Clusters

Overview

Scenario

In the following scenarios, MirrorMaker can be used to synchronize data between different Kafka clusters to ensure the availability and reliability of the clusters:

  • Backup and disaster recovery: An enterprise has multiple data centers. To prevent service unavailability caused by a fault in one data center, cluster data is synchronously backed up in multiple data centers.
  • Cluster migration: As enterprises migrate services to the cloud, data in on-premises clusters must be synchronized with that in cloud clusters to ensure service continuity.

Principle

MirrorMaker can be used to mirror data from the source cluster to the target cluster. As shown in Figure 1, in essence, MirrorMaker first consumes data from the source cluster and then produces the consumed data to the target cluster. For more information about MirrorMaker, see Mirroring data between clusters.

Figure 1 How MirrorMaker works

Restrictions

  • The IP addresses and port numbers of the nodes in the source cluster cannot be the same as those of the nodes in the target cluster. Otherwise, data will be replicated infinitely in a topic.
  • Use MirrorMaker to synchronize data between at least two clusters. If there is only one cluster, data will be replicated infinitely in a topic.

Procedure

  1. Buy an ECS that can communicate with the source and target clusters. For details, see the Purchasing a Custom ECS.
  2. Log in to the ECS, install JDK, and add the following contents to .bash_profile in the home directory to configure the environment variables JAVA_HOME and PATH. In this command, /opt/java/jdk1.8.0_151 is the JDK installation path. Change it to the path where you install JDK.

    export JAVA_HOME=/opt/java/jdk1.8.0_151 
    export PATH=$JAVA_HOME/bin:$PATH

    Run the source .bash_profile command for the modification to take effect.

    Use Oracle JDK instead of ECS's default JDK (for example, OpenJDK), because ECS's default JDK may not be suitable. Obtain Oracle JDK 1.8.111 or later from Oracle's official website.

  3. Download the binary software package of Kafka 3.3.1.

    wget https://archive.apache.org/dist/kafka/3.3.1/kafka_2.12-3.3.1.tgz

  4. Decompress the binary software package.

    tar -zxvf kafka_2.12-3.3.1.tgz

  5. Go to the binary software package directory and specify the IP addresses and ports of the source and target clusters and other parameters in the connect-mirror-maker.properties configuration file in the config directory.

    # Specify two clusters.
    clusters = A, B
    A.bootstrap.servers = A_host1:A_port, A_host2:A_port, A_host3:A_port
    B.bootstrap.servers = B_host1:B_port, B_host2:B_port, B_host3:B_port
    
    # Specify the data synchronization direction. The data can be synchronized unidirectionally or bidirectionally.
    A->B.enabled = true
    
    # Specify the topics to be synchronized. Regular expressions are supported. By default, all topics are replicated, for example, foo-.*.
    A->B.topics = .*
    
    # If the following two configurations are enabled, clusters A and B replicate data with each other.
    #B->A.enabled = true
    #B->A.topics = .*
    
    # Specify the number of replicas. If multiple topics need to be synchronized and their replica quantities are different, create topics with the same name and replica quantity before starting MirrorMaker.
    replication.factor=3
    
    # Specify the consumer offset synchronization direction (unidirectionally or bidirectionally).
    A->B.sync.group.offsets.enabled=true
    
    ############################# Internal Topic Settings  #############################
    # The replication factor for mm2 internal topics "heartbeats", "B.checkpoints.internal" and
    # "mm2-offset-syncs.B.internal"
    # In the test environment, the value can be 1. In the production environment, it is recommended that the value be greater than 1, for example, 3.
    checkpoints.topic.replication.factor=3
    heartbeats.topic.replication.factor=3
    offset-syncs.topic.replication.factor=3
    
    # The replication factor for connect internal topics "mm2-configs.B.internal", "mm2-offsets.B.internal" and
    # "mm2-status.B.internal"
    # In the test environment, the value can be 1. In the production environment, it is recommended that the value be greater than 1, for example, 3.
    offset.storage.replication.factor=3
    status.storage.replication.factor=3
    config.storage.replication.factor=3
    
    # customize as needed
    # replication.policy.separator = _
    # sync.topic.acls.enabled = false
    # emit.heartbeats.interval.seconds = 5

  6. In the binary software package directory, start MirrorMaker to synchronize data.

    ./bin/connect-mirror-maker.sh config/connect-mirror-maker.properties

  7. (Optional) If a topic is created in the source cluster after MirrorMaker has been started, and the topic data needs to be synchronized, restart MirrorMaker. For details about how to restart MirrorMaker, see 6. You can also add configurations listed in Table 1 to periodically synchronize new topics without restarting MirrorMaker. refresh.topics.interval.seconds is mandatory. Other parameters are optional.

    Table 1 MirrorMaker configurations

    Parameter

    Default Value

    Description

    sync.topic.configs.enabled

    true

    Whether to monitor the source cluster for configuration changes.

    sync.topic.acls.enabled

    true

    Whether to monitor the source cluster for ACL changes.

    emit.heartbeats.enabled

    true

    Whether to let the connector send heartbeats periodically.

    emit.heartbeats.interval.seconds

    5 seconds

    Heartbeat frequency.

    emit.checkpoints.enabled

    true

    Whether to let the connector periodically send the consumer offset information.

    emit.checkpoints.interval.seconds

    5 seconds

    Checkpoint frequency.

    refresh.topics.enabled

    true

    Whether to let the connector periodically check for new topics.

    refresh.topics.interval.seconds

    5 seconds

    Frequency of checking for new topics in the source cluster.

    refresh.groups.enabled

    true

    Whether to let the connector periodically check for new consumer groups.

    refresh.groups.interval.seconds

    5 seconds

    Frequency of checking for new consumer groups in the source cluster.

    replication.policy.class

    org.apache.kafka.connect.mirror.DefaultReplicationPolicy

    Use LegacyReplicationPolicy to imitate MirrorMaker of an earlier version.

    heartbeats.topic.retention.ms

    One day

    Used when heartbeat topics are created for the first time.

    checkpoints.topic.retention.ms

    One day

    Used when checkpoint topics are created for the first time.

    offset.syncs.topic.retention.ms

    max long

    Used when offset sync topics are created for the first time.

Verifying Data Synchronization

  1. View the topic list in the target cluster to check whether there are source topics.

    Topic names in the target cluster have a prefix (for example, A.) added to the source topic name. This is a MirrorMaker configuration for preventing cyclic topic backup.

  2. Produce and consume messages in the source cluster, view the consumption progress in the target cluster, and check whether data has been synchronized from the source cluster to the target cluster.

    If the target cluster is a Huawei Cloud Kafka instance, view the consumption progress on the Consumer Groups page.