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)