Yon can configure the WAN replication for Hazelcast's IMap and ICache data structures. To enable WAN replication for an IMap or ICache instance, you can use the wan-replication-ref
element. Each IMap and ICache instance can have different WAN replication configurations.
Enabling WAN Replication for IMap:
Imagine you have different distributed maps, however only one of those maps should be replicated to a target cluster. To achieve this, configure the map that you want
replicated by adding the wan-replication-ref
element in the map configuration as shown below.
<hazelcast>
<wan-replication name="my-wan-cluster">
...
</wan-replication>
<map name="my-shared-map">
<wan-replication-ref name="my-wan-cluster">
<merge-policy>com.hazelcast.map.merge.PassThroughMergePolicy</merge-policy>
<republishing-enabled>false</republishing-enabled>
</wan-replication-ref>
</map>
...
</hazelcast>
The following is the equivalent programmatic configuration:
Config config = new Config();
WanReplicationConfig wrConfig = new WanReplicationConfig();
WanTargetClusterConfig wtcConfig = wrConfig.getWanTargetClusterConfig();
wrConfig.setName("my-wan-cluster");
...
config.addWanReplicationConfig(wrConfig);
WanReplicationRef wanRef = new WanReplicationRef();
wanRef.setName("my-wan-cluster");
wanRef.setMergePolicy(PassThroughMergePolicy.class.getName());
wanRef.setRepublishingEnabled(false);
config.getMapConfig("my-shared-map").setWanReplicationRef(wanRef);
You see that we have my-shared-map
configured to replicate itself to the cluster targets defined in the earlier
wan-replication
element.
wan-replication-ref
has the following elements;
-
name
: Name ofwan-replication
configuration. IMap or ICache instance uses thiswan-replication
configuration. -
merge-policy
: Resolve conflicts that are occurred when target cluster already has the replicated entry key. -
republishing-enabled
: When enabled, an incoming event to a member is forwarded to target cluster of that member. Enabling the event republishing is useful in a scenario where cluster A replicates to cluster B, and cluster B replicates to cluster C. You do not need to enable republishing when all your clusters replicate to each other.
When using Active-Active Replication, multiple clusters can simultaneously update the same entry in a distributed data structure.
You can configure a merge policy to resolve these potential conflicts, as shown in the above example configuration (using the merge-policy
sub-element under the wan-replication-ref
element).
Hazelcast provides the following merge policies for IMap:
-
com.hazelcast.map.merge.PutIfAbsentMapMergePolicy
: Incoming entry merges from the source map to the target map if it does not exist in the target map. -
com.hazelcast.map.merge.HigherHitsMapMergePolicy
: Incoming entry merges from the source map to the target map if the source entry has more hits than the target one. -
com.hazelcast.map.merge.PassThroughMergePolicy
: Incoming entry merges from the source map to the target map unless the incoming entry is not null. -
com.hazelcast.map.merge.LatestUpdateMapMergePolicy
: Incoming entry merges from the source map to the target map if the source entry has been updated more recently than the target entry. Please note that this merge policy can only be used when the clusters' clocks are in sync.
NOTE: When using WAN replication, please note that only key based events are replicated to the target cluster. Operations like clear
, destroy
and evictAll
are NOT replicated.
Enabling WAN Replication for ICache:
The following is a declarative configuration example for enabling WAN Replication for ICache:
<wan-replication name="my-wan-cluster">
...
</wan-replication>
<cache name="my-shared-cache">
<wan-replication-ref name="my-wan-cluster">
<merge-policy>com.hazelcast.cache.merge.PassThroughCacheMergePolicy</merge-policy>
<republishing-enabled>true</republishing-enabled>
</wan-replication-ref>
</cache>
The following is the equivalent programmatic configuration:
Config config = new Config();
WanReplicationConfig wrConfig = new WanReplicationConfig();
WanTargetClusterConfig wtcConfig = wrConfig.getWanTargetClusterConfig();
wrConfig.setName("my-wan-cluster");
...
config.addWanReplicationConfig(wrConfig);
WanReplicationRef cacheWanRef = new WanReplicationRef();
cacheWanRef.setName("my-wan-cluster");
cacheWanRef.setMergePolicy("com.hazelcast.cache.merge.PassThroughCacheMergePolicy");
cacheWanRef.setRepublishingEnabled(true);
config.getCacheConfig("my-shared-cache").setWanReplicationRef(cacheWanRef);
NOTE: Caches that are created dynamically do not support WAN replication functionality. Cache configurations should be defined either declaratively (by XML) or programmatically on both source and target clusters.
Hazelcast provides the following merge policies for ICache:
-
com.hazelcast.cache.merge.HigherHitsCacheMergePolicy
: Incoming entry merges from the source cache to the target cache if the source entry has more hits than the target one. -
com.hazelcast.cache.merge.PassThroughCacheMergePolicy
: Incoming entry merges from the source cache to the target cache unless the incoming entry is not null.