Hazelcast: Simple Scenario using Java
Simultaneous Read and Write with Hazelcast Data Store
To enable two applications to simultaneously read and write data using Hazelcast, both applications must:
- Connect to the same Hazelcast cluster.
- Use shared distributed data structures, such as
IMaporIQueue, to manage concurrent data access. - Utilize Hazelcast’s thread-safe operations to ensure consistency during simultaneous reads and writes.
Conceptual Explanation with Schema
Architecture Schema:
- Hazelcast Cluster: Central node(s) holding the distributed data.
- Application A: Writes and reads data to/from the Hazelcast
IMap. - Application B: Simultaneously writes and reads data from the same
IMap. - Data Flow:
- Both applications use Hazelcast client APIs or embedded nodes to interact with the cluster.
- Hazelcast ensures consistency, scalability, and fault tolerance.
+-------------------+ +-------------------+
| Application A | | Application B |
| | | |
| Write to IMap | <---->| Read from IMap |
| Read from IMap | | Write to IMap |
+-------------------+ +-------------------+
| |
+-----------+--------------+
|
+-----------------------+
| Hazelcast Cluster |
| (Distributed IMap) |
+-----------------------+Key Features for Simultaneous Access
- Thread-Safe Access: Hazelcast’s data structures like
IMapare designed for concurrent access. - Atomic Operations: Use atomic operations like
putIfAbsentorcomputefor safe updates. - Listeners: Register listeners for real-time notifications of changes in the data.
Get Aymen FARHANI’s stories in your inbox
Join Medium for free to get updates from this writer.
Simple Java Code Example
Step 1: Hazelcast Configuration
Create a hazelcast.xml configuration file:
<hazelcast xmlns="http://www.hazelcast.com/schema/config">
<network>
<join>
<multicast enabled="false"/>
<tcp-ip enabled="true">
<member>127.0.0.1</member> <!-- Add other nodes if needed -->
</tcp-ip>
</join>
</network>
</hazelcast>Step 2: Application Code
Application A: Writing and Reading Data
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
public class ApplicationA {
public static void main(String[] args) {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
IMap<String, String> map = hazelcastInstance.getMap("sharedMap");
// Write data to the map
map.put("key1", "Value from Application A");
System.out.println("Application A wrote: " + map.get("key1"));
// Simulate read from the map
String value = map.get("key2");
System.out.println("Application A read: " + value);
}
}Application B: Writing and Reading Data
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
public class ApplicationB {
public static void main(String[] args) {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
IMap<String, String> map = hazelcastInstance.getMap("sharedMap");
// Write data to the map
map.put("key2", "Value from Application B");
System.out.println("Application B wrote: " + map.get("key2"));
// Simulate read from the map
String value = map.get("key1");
System.out.println("Application B read: " + value);
}
}Execution Steps
- Start Application A and Application B. Both applications will connect to the same Hazelcast cluster.
- Observe the console output:
- Application A writes
"Value from Application A"tokey1and readskey2. - Application B writes
"Value from Application B"tokey2and readskey1.
Hazelcast Features Ensuring Simultaneity
- Distributed IMap: The
IMapis thread-safe and supports simultaneous access. - Atomic Operations: Updates to the map are consistent even when multiple applications interact simultaneously.
- Replication and Fault Tolerance: Data is replicated across the cluster, ensuring reliability.
This setup demonstrates how Hazelcast allows multiple applications to concurrently read and write data while maintaining consistency and reliability.










