Will Hazelcast IMap block when waiting for an entry in transit?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Hazelcast IMDG (In-Memory Data Grid) is a distributed data grid, which allows data to be stored across multiple servers while being accessible as a single consolidated cluster. Among its many distributed data structures, Hazelcast's IMap is a critical component. It provides a thread-safe, distributed, and scalable map implementation.
Understanding Hazelcast IMap
IMap extends the standard Java Map interface. Unlike a typical ConcurrentHashMap found in Java, IMap spreads its content across networked nodes. This allows IMap to hold much larger datasets than could fit into a single node's memory. Operations on the map, like reads, writes, and updates are distributed across the cluster according to the keys' partitioning.
Key Operations and Blocking Behavior
One of the common questions when working with distributed maps like IMap is about their blocking behavior especially when an entry is in transit; meaning, either being moved from one partition to another or being loaded from an external store.
Read and Write Operations
For read and write operations IMap generally does the following:
- Read: When a read operation is requested, it checks the local store first. If the entry is not available locally, it checks other nodes. If the entry is not in any node (and if a map store is configured), it then loads it from the external store.
- Write: When a write operation is performed, it is executed on the owner node of the key. This node is determined based on the key’s hashcode and the current partition layout.
Blocking Scenarios
- Write operations typically block until the operation can be successfully committed across the members of the cluster that store replicas of the partition that is affected.
- Read operations can block when the value is in transit or being fetched from a map store. This blocking is critical to ensure data consistency and to prevent dirty reads.
Handling Data in Transit
Data might be "in transit" during:
- Rebalancing: When nodes join or leave, partitions might be reassigned between nodes which means data is moved across the network.
- Lazy Loading: When data is fetched lazily from an external store into the map.
When data is in transit due to these activities, Hazelcast uses mechanisms like locks or temporary blocking to ensure that the operations on the data key do not yield inconsistent results.
Example Scenario
Consider a Hazelcast cluster with 3 nodes, and a partitioned IMap holding user session data. If a node joins or leaves the cluster, the partitions are redistributed. Let's look at how operations might be impacted:
- User requests data that is in a partition being transferred:
- Read: Hazelcast will block the read until the partition transfer is completed to ensure that the latest data is read.
- Write: Similar to read, write operations will also be blocked until the partition transfer completes.
- User requests data not in the cache but in an external database:
- Read: The read would block until the data is loaded into the node handling the read request.
Conclusion and Performance Considerations
While blocking may seem like a performance penalty, it is crucial for maintaining strong consistency within the cluster. Hazelcast provides various configurations to manage how data is loaded, which can be optimized based on specific use cases.
Table: Summary of Hazelcast IMap Behavior
| Operation | Blocking Behavior | Reason |
| Read | Blocks if entry is in transit or lazy-loading | Ensures data consistency and integrity |
| Write | Blocks until commit across affected partition replicas | Ensures atomicity and durability |
Advanced Topics
- Near Cache: Use near cache for frequently read data to reduce read latency.
- Map Listeners: Utilize continuous query caching or listeners to react to data changes in the cluster.
Hazelcast's IMap offers a powerful tool for handling distributed data with various mechanisms ensuring data consistency, making it ideal for scalable and resilient applications.

