Hazelcast is a distributed in-memory data platform that Spring can use as the backing store of its cache abstraction. With Hazelcast as the cache provider, @Cacheable results are kept in a cluster-wide map, so every application instance sees the same cached data instead of keeping a private copy in local memory.
Use it when you run several instances of a service and need one consistent cache with replication, expiry and eviction. For a single instance, a local cache such as Caffeine is simpler and faster.
Spring's cache abstraction (@Cacheable, @CachePut, @CacheEvict) only defines how caching is applied. A CacheManager decides where the data lives. With a local provider such as Caffeine each instance has its own cache, so an update on instance A leaves a stale entry on instance B. Hazelcast gives all instances one shared cache:
The trade-off is a network hop for entries held by another member, plus serialization of keys and values.
| Mode | How it works | Good for |
|---|---|---|
| Embedded | Each application instance is itself a Hazelcast member | Small clusters, simple deployments, lowest latency |
| Client-server | A separate Hazelcast cluster, applications connect as clients | Larger data, independent scaling and upgrades of the cache |
Embedded mode is the quickest to start with and is what Spring Boot sets up by default when it finds a Hazelcast configuration. Client-server mode needs a HazelcastClient configuration and a separately managed cluster.
Spring Boot manages the Hazelcast version, so do not pin one yourself. You need the core library, the hazelcast-spring module that contains HazelcastCacheManager, and the cache starter.
Spring Boot auto-configures a HazelcastInstance when it finds hazelcast.yaml, hazelcast.xml or a spring.hazelcast.config property, and then a HazelcastCacheManager on top of it. The old tutorial pattern of building the CacheManager yourself, and pinning version 4.2.2, is no longer needed.
Create src/main/resources/hazelcast.yaml:
Each Spring cache name maps to a Hazelcast distributed map of the same name. Here the products cache expires entries ten minutes after writing, drops entries idle for five minutes, keeps one backup copy and evicts least recently used entries beyond 10,000 per node. On Kubernetes, use the Kubernetes discovery plugin instead of a fixed member list, and on cloud platforms prefer the provider's discovery mechanism over multicast, which is often blocked.
If no map configuration exists for a cache name, Hazelcast creates the map with default settings, which means no expiry. Always configure expiry for caches that hold changing data.
Enable caching once, then annotate the service methods.
The first call to getById(5L) runs the repository query and stores the result in the products map. The next call, on any instance, returns the cached value. update refreshes the entry and delete removes it from the whole cluster.
Keys and values travel between members, so they must be serializable. The simplest option is to implement java.io.Serializable:
Java serialization is easy but slow and fragile when classes change between versions. For production workloads consider Hazelcast's Portable, IdentifiedDataSerializable or compact serialization, which also helps with rolling upgrades. Never cache JPA entities with lazy associations; cache DTOs or records.
No. With hazelcast-spring on the classpath and a Hazelcast configuration present, Spring Boot creates the HazelcastInstance and HazelcastCacheManager for you.
Yes. Define a HazelcastInstance and a HazelcastCacheManager bean yourself and add @EnableCaching.
Neither is universally better. Hazelcast can run embedded in the JVM with no extra server, while Redis is a separate service many teams already operate. Choose by operational fit and data size.
The class does not implement Serializable or a Hazelcast serialization interface. Make cached types serializable or register a serializer.
Hazelcast turns Spring's cache abstraction into a shared, clustered cache. Add hazelcast and hazelcast-spring, describe the cluster and map expiry in hazelcast.yaml, enable caching and use @Cacheable, @CachePut and @CacheEvict as usual. Make cached values serializable, set a time to live per map, and pick embedded or client-server mode based on how you want to operate the cache.