Search Tutorials


Hazelcast Tutorial- Understanding Replicated Maps using example | JavaInUse



Hazelcast Tutorial- Understanding Replicated Maps using example

A replicated map is a weakly consistent, distributed key-value data structure provided by Hazelcast. All other data structures are partitioned in design. A replicated map does not partition data (it does not spread data to different cluster members); instead, it replicates the data to all nodes.

hazel_3-1
This leads to higher memory consumption. However, a replicated map has faster read and write access since the data are available on all nodes and writes take place on local nodes, eventually being replicated to all other nodes. Weak consistency compared to eventually consistency means that replication is done on a best efforts basis. Lost or missing updates are neither tracked nor resent.
This kind of data structure is suitable for immutable objects, catalogue data, or idempotent calculable data (like HTML pages). Used in cases of immutable slow movin data like config. ReplicatedMap interface supports EntryListeners.
Replicated map nearly fully implements the java.util.Map interface, but it lacks the methods from java.util.concurrent.ConcurrentMap since there are no atomic guarantees to writes or reads.
package com.javainuse.main;

import java.util.Map;

import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

public class FirstMapExample {
	public static void main(String[] args) {
		HazelcastInstance hz = Hazelcast.newHazelcastInstance();
		Map<String, String> employees = hz.getReplicatedMap("employees");
	    employees.put("1", "emp1");
	    employees.put("2", "emp2");
	    employees.put("3", "emp3");
	    employees.put("4", "emp4");
	    employees.put("5", "emp5");
	    
	    System.out.println("Total number of employees " + employees.size());
	}
}

Download Source Code

Download it - Hazelcast Collections- Map, Set and Queue

	

See Also

Hazelcast Hello World Example- Getting started with Hazelcast. Hazelcast Tutorial- Working with Hazelcast collections- Distributed Map,List,Set,Queues. Hazelcast - Main Menu