Skip to main content
Version: 5.17

Tutorial: Synchronize data between two Hazelcast clusters using WAN synchronization

Learn how to synchronize data across two Hazelcast clusters using WAN synchronization.

Overview​

In this tutorial, you'll do the following:

  • Deploy two Hazelcast clusters.
  • Create a Hazelcast map configuration on one of the clusters.
  • Synchronize map data between the two Hazelcast clusters using both full and delta WAN synchronization.

The tutorial should take approximately 10 minutes to complete.

Prerequisites​

Before you begin, make sure that you have:

Start the Hazelcast clusters​

In this step, you'll start two Hazelcast clusters, one called hazelcast-first and one called hazelcast-second.

  1. Create a secret with your Hazelcast Enterprise License.

    shell
    kubectl create secret generic hazelcast-license-key --from-literal=license-key=<hz-license-key>
  2. Start the first Hazelcast cluster.

    shell
    kubectl apply -f - <<EOF
    apiVersion: hazelcast.com/v1alpha1
    kind: Hazelcast
    metadata:
    name: hazelcast-first
    spec:
    licenseKeySecretName: hazelcast-license-key
    exposeExternally:
    type: Unisocket
    discoveryServiceType: LoadBalancer
    EOF
  3. Check the status of the cluster to ensure it is running.

    shell
    kubectl get hazelcast
    shell
    NAME STATUS MEMBERS
    hazelcast-first Running 3/3
  4. Start the second cluster.

    shell
    kubectl apply -f - <<EOF
    apiVersion: hazelcast.com/v1alpha1
    kind: Hazelcast
    metadata:
    name: hazelcast-second
    spec:
    licenseKeySecretName: hazelcast-license-key
    exposeExternally:
    type: Unisocket
    discoveryServiceType: LoadBalancer
    EOF
  5. Check the status to ensure that both clusters are running.

    shell
    kubectl get hazelcast
    shell
    NAME STATUS MEMBERS
    hazelcast-first Running 3/3
    hazelcast-second Running 3/3

Create and populate a map for WAN replication​

In this step, you will create and populate a map on the hazelcast-first cluster. This map will be the source for WAN replication.

  1. Create the configuration for a map called map-sync on the hazelcast-first cluster.

    If you need full WAN synchronization, use the following command.

    shell
    kubectl apply -f - <<EOF
    apiVersion: hazelcast.com/v1alpha1
    kind: Map
    metadata:
    name: map-sync
    spec:
    hazelcastResourceName: hazelcast-first
    EOF

    If you need delta WAN synchronization, use the following command.

    shell
    kubectl apply -f - <<EOF
    apiVersion: hazelcast.com/v1alpha1
    kind: Map
    metadata:
    name: map-sync
    spec:
    hazelcastResourceName: hazelcast-first
    merkleTree:
    depth: 10
    EOF
    tip

    Unsure of whether you need full or delta? See Full WAN synchronization and Delta WAN synchronization for details.

  2. Find the address of the first cluster.

    shell
    kubectl get hazelcastendpoint --selector="app.kubernetes.io/instance in (hazelcast-first)"
    shell
    NAME TYPE ADDRESS
    hazelcast-first Discovery 34.123.9.149:5701
    hazelcast-first-wan WAN 34.123.9.149:5710

    The ADDRESS column displays the external address of the first Hazelcast cluster.

  3. Open the examples folder to access all sample clients:

    shell
    cd hazelcast-platform-operator-wan-sync

    The sample code for this tutorial is in the docs/modules/ROOT/examples/operator-wan-sync directory.

  4. Configure the Hazelcast client to connect to the first cluster using its address.

    note

    CLC must be installed in your system. See the CLC installation instructions.

    Add the first cluster configuration to the CLC:

    bash
    clc config add hz-1 cluster.name=dev cluster.address=<FIRST-CLUSTER-EXTERNAL-IP>
  5. Add entries to the map.

    Populate the map with entries.

    bash
    for i in {1..10};
    do
    clc -c hz-1 map set --name map-sync key-$i value-$i;
    done

    Verify that the map has the expected number of entries.

    bash
    clc -c hz-1 map size --name map-sync

Enable WAN replication and replicate entries​

In this step, you'll first verify that the second cluster does not contain a map-sync structure. You'll then enable WAN replication and verify that the map and all entries have been copied to the second cluster.

  1. Find the address of the second cluster.

    shell
    kubectl get hazelcastendpoint --selector="app.kubernetes.io/instance in (hazelcast-second)"
    shell
    NAME TYPE ADDRESS
    hazelcast-second Discovery 198.51.100.16:5701
    hazelcast-second-wan WAN 198.51.100.16:5710

    The ADDRESS column displays the external address of the second Hazelcast cluster.

  2. Repeat steps 2.3 to 2.4, using the address of the second cluster to enable the client to connect.

  3. Connect to the second cluster and verify that the map named map-sync contains no data.

    bash
    clc -c hz-2 map size --name map-sync
  4. Modify the configuration of the first cluster to add the address of the second cluster as the WAN replication event target.

    If you need a full WAN synchronization, run the following command to apply the configuration:

    shell
    kubectl apply -f - <<EOF
    apiVersion: hazelcast.com/v1alpha1
    kind: WanReplication
    metadata:
    name: wan-replication
    spec:
    resources:
    - name: hazelcast-first
    kind: Hazelcast
    targetClusterName: dev
    endpoints: "<SECOND-CLUSTER-EXTERNAL-IP>"
    EOF

    If you need a delta WAN synchronization, run the following to apply the configuration:

    shell
    kubectl apply -f - <<EOF
    apiVersion: hazelcast.com/v1alpha1
    kind: WanReplication
    metadata:
    name: wan-replication
    spec:
    resources:
    - name: hazelcast-first
    kind: Hazelcast
    targetClusterName: dev
    syncConsistencyCheckStrategy: "MERKLE_TREES"
    endpoints: "<SECOND-CLUSTER-EXTERNAL-IP>"
    EOF
  5. Create a WAN synchronization resource within the Kubernetes cluster, using the existing WanReplication CR.

    shell
    kubectl apply -f - <<EOF
    apiVersion: hazelcast.com/v1alpha1
    kind: WanSync
    metadata:
    name: wan-sync
    spec:
    wanReplicationResourceName: wan-replication
    EOF
    note

    WAN synchronization ensures data consistency between the two Hazelcast clusters. Full WAN synchronization transmits all data from the source cluster to the target cluster, aligning the state of the target IMap with the source IMap. This method is particularly beneficial when the synchronization between two remote clusters is lost due to WAN queue overflows or cluster restarts.

  6. Check the WAN synchronization status:

    shell
    kubectl get wansync wan-sync

    The output should be similar to the following:

    shell
    NAME STATUS
    wan-sync Completed
  7. Repeat step 3.3 to verify that the map-sync structure on the hazelcast-second cluster now contains data.

Clean up​

To remove all custom resources, run the following commands:

shell
kubectl delete secret hazelcast-license-key
kubectl delete $(kubectl get wansync,wanreplications,map,hazelcast -o name)

Summary​

In this tutorial, you learned how to:

  • Deploy two Hazelcast clusters.
  • Create a Hazelcast map configuration on one of the clusters.
  • Synchronize map data between the two Hazelcast clusters using full and delta WAN synchronization.

Next steps​

See also: