DEV Community

Ali Farooq
Ali Farooq

Posted on

Distributed Caching with Hazelcast + Eureka: Smarter Microservices in Action

๐Ÿง  Distributed Caching with Hazelcast + Eureka: Smarter Microservices in Action

Hey devs! ๐Ÿ‘‹
If you're working with microservices, youโ€™ve probably hit the classic challenge: how do we implement an efficient distributed caching solution without overloading the database or dealing with complex network setups?

Let me introduce you to a simple but powerful combo Iโ€™ve been playing with โ€” Hazelcast + Eureka โ€” and show you how I integrated them in my project:

๐Ÿ‘‰ HazelcastEurekaDiscovery on GitHub


๐Ÿ’ก Why Hazelcast?

Hazelcast is a distributed in-memory cache, and itโ€™s super handy when:

  • You want to avoid expensive DB hits
  • You need fast access to session/state data
  • Youโ€™re building real-time, low-latency apps

It supports automatic clustering (nodes discovering each other and sharing data), but out of the box, it uses multicast for discovery โ€” which doesnโ€™t work well in modern cloud environments (like Docker, Kubernetes, AWS, etc.).


๐Ÿ” Enter Eureka (Service Discovery)

Eureka is a service registry from Netflix (supported in Spring Cloud). Services can register themselves, and other services can discover them dynamically. This is perfect for solving the โ€œhow do services find each other?โ€ problem โ€” which Hazelcast needs for clustering.

Soโ€ฆ what if we let Hazelcast use Eureka to discover peer nodes?

Spoiler: It works great. And that's what my HazelcastEurekaDiscovery project is all about.


โš™๏ธ How It Works

Hereโ€™s a quick breakdown of what the repo includes:

๐Ÿงฑ Components

  1. Eureka Server

    Hosts the registry at http://localhost:8761. Services register themselves here.

  2. Cache Service

    A Spring Boot app with Hazelcast embedded.

    It:

    • Registers with Eureka
    • Uses Eureka to find other cache service instances
    • Forms a Hazelcast cluster dynamically

๐Ÿงช Run It Yourself

Clone the repo

git clone https://github.com/alif2165/HazelcastEurekaDiscovery
cd HazelcastEurekaDiscovery
Enter fullscreen mode Exit fullscreen mode

Start Eureka Server

cd eurekaserver
mvn spring-boot:run
Enter fullscreen mode Exit fullscreen mode

Start multiple cache service instances

cd ../cacheservice
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8081
mvn spring-boot:run -Dspring-boot.run.arguments=--server.port=8082
Enter fullscreen mode Exit fullscreen mode

Then go to: http://localhost:8761/eureka to see all registered instances.

๐Ÿงฐ Under the Hood: Hazelcast + Eureka Config

hazelcast.yaml example

network:
  join:
    multicast:
      enabled: false
    tcp-ip:
      enabled: false
    discovery-strategies:
      discovery-strategies:
        - class-name: com.hazelcast.discovery.EurekaDiscoveryStrategy
Enter fullscreen mode Exit fullscreen mode

Spring Boot application.properties

spring.application.name=cacheservice
eureka.client.service-url.defaultZone=http://localhost:8761/eureka
Enter fullscreen mode Exit fullscreen mode

๐Ÿš€ Why This Rocks

โœ… No static IP configs
โœ… Scales easily โ€“ just start new instances
โœ… Cluster auto-formation using Eureka
โœ… Cache stays in sync across services

Itโ€™s a clean way to build distributed systems that are scalable, discoverable, and efficient.


๐ŸŽฏ Use Cases

Shared caching across microservices

Lightweight alternative to Redis in some use cases

In-memory session management

Cluster coordination without Zookeeper/Consul


๐Ÿ™Œ Final Thoughts

This setup might be simple, but it solves a real pain point in distributed systems โ€” dynamic, self-forming clusters for shared state.

If youโ€™re exploring Hazelcast, Spring Cloud, or microservice discovery patterns, give this a try!

Top comments (0)