Distributed Caching with Hazelcast + Eureka: Smarter Microservices in Action published: true

Distributed Caching with Hazelcast + Eureka: Smarter Microservices in Action published: true


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!



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *