Question: Is Hazelcast a distributed cache?

Answer

Yes, Hazelcast is indeed a distributed cache.

Hazelcast IMDG (In-Memory Data Grid) is an open-source in-memory data grid based on Java. It provides distributed implementations for core data structures such as java.util.{Map, Queue, Set, List}, and additional data structures like MultiMap, Lock, Topic etc.

Here are key features that make Hazelcast a potent distributed cache:

  1. Distributed and Replicated In-Memory Store: Hazelcast stores data in memory across all nodes in the cluster. This makes it lightning fast as there's no disk I/O overhead. Additionally, to handle failures, it replicates data across multiple nodes.

  2. Elastic Scalability: Hazelcast can scale out to hundreds of nodes, and scales back in when nodes are removed. It can do this while your application is running, with no downtime.

  3. Data Sharding: Data in Hazelcast is automatically partitioned across nodes, ensuring balanced data and workload distribution.

  4. Transparent Networking: Hazelcast has automatic discovery and handling of network partitions. The latter is especially important in maintaining data consistency and availability during network disruptions.

  5. Thread-Safe Operations: Hazelcast is entirely thread-safe. It uses locks to maintain data integrity during concurrent access.

  6. Transactional Capability: It supports JTA (Java Transaction API) and XA transactions helping you control data manipulation and ensure data integrity.

  7. Persistence: You can persist the cached data to a database, making it suitable for caching database queries.

Here's an example of how to create a Hazelcast instance and use it as a distributed cache:

import com.hazelcast.core.Hazelcast; import java.util.Map; HazelcastInstance hz = Hazelcast.newHazelcastInstance(); Map<Integer, String> map = hz.getMap("my-distributed-map"); map.put(1, "Hello Hazelcast"); System.out.println("Value for key 1 is " + map.get(1)); hz.shutdown();

This example creates a Hazelcast instance and adds an entry into a distributed map. This map is not local; it's distributed across all Hazelcast nodes.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.