How to configure replicated cache in Hazelcast cluster?

Viewed 2767

My Spring application consists of dozen microservices. Each microservice provides data, which does not change very often. In the order to reduce communication between microservices I am considering to start using Hazelcast.

My idea is that every microservice would have embedded Hazelcast. Microservices are running in same network and I suppose that Hazelcasts would form a cluster. Every microservice will put its data into local Hazelcast on startup and data will be copied to every other Hazelcast in the cluster. When a microservice would need to load data from other microservice, it would first look into local Hazelcast and only if data are missed from local cache, it would make network call.

Is it possible to configure something like this with Hazelcast? I already made a try, but data from a microservice happened to be distributed across all Hazelcast nodes in cluster.

I used very trivial configuration:

@Configuration
@EnableCaching
@Profile("hazelcast")
public class HazelcastCacheConfiguration {
    @Bean
    public Config hazelcastConfig() {
        return new Config()
                .setInstanceName("routes-cache")
                .addMapConfig(
                        new MapConfig()
                                .setName("ports-cache")
                                .setEvictionPolicy(EvictionPolicy.LRU)
                ).addMapConfig(
                        new MapConfig()
                                .setName("routes-cache")
                                .setEvictionPolicy(EvictionPolicy.LRU)
                ).setProperty("hazelcast.logging.type", "slf4j");
    }
}

I checked data replication across the cluster in Hazelcast Management Center. My sample set of data has only 13 records. The microservice has pushed 13 records on startup into local Hazelcast and in Management Center I have seen that there are 2 nodes in the cluster with 9 records on one node and 4 records on the node of other microservice.

Thank you in advance!

1 Answers
Related