Sitemap
Aymen FARHANI

Aymen FARHANI

Senior Software Engineer

Hazelcast: Simple Scenario using Java

2 min readJan 11, 2025

--

Simultaneous Read and Write with Hazelcast Data Store

To enable two applications to simultaneously read and write data using Hazelcast, both applications must:

  1. Connect to the same Hazelcast cluster.
  2. Use shared distributed data structures, such as IMap or IQueue, to manage concurrent data access.
  3. Utilize Hazelcast’s thread-safe operations to ensure consistency during simultaneous reads and writes.

Conceptual Explanation with Schema

Architecture Schema:

  1. Hazelcast Cluster: Central node(s) holding the distributed data.
  2. Application A: Writes and reads data to/from the Hazelcast IMap.
  3. Application B: Simultaneously writes and reads data from the same IMap.
  4. 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

  1. Thread-Safe Access: Hazelcast’s data structures like IMap are designed for concurrent access.
  2. Atomic Operations: Use atomic operations like putIfAbsent or compute for safe updates.
  3. 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

  1. Start Application A and Application B. Both applications will connect to the same Hazelcast cluster.
  2. Observe the console output:
  • Application A writes "Value from Application A" to key1 and reads key2.
  • Application B writes "Value from Application B" to key2 and reads key1.

Hazelcast Features Ensuring Simultaneity

  1. Distributed IMap: The IMap is thread-safe and supports simultaneous access.
  2. Atomic Operations: Updates to the map are consistent even when multiple applications interact simultaneously.
  3. 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.

--

--