How to Scale Hazelcast Cluster With Docker Compose

DZone 's Guide to

How to Scale Hazelcast Cluster With Docker Compose

During webinar about 'Distributed Caching for you next Node.js project' I was asked if we provide examples of running Hazelcast with Compose or Swarm. Now you have it!

· Cloud Zone ·
Free Resource


From the perspective of containerization and cloud deployment, Hazelcast IMDG is a perfect candidate.

Since it's Elastically Scalable, you can just add more servers, and they form the cluster automatically and contribute their memory to the 'shared memory.'

  • Performs at in-memory transaction speeds.
  • Highly Available can deploy in backup pairs or even WAN replicated.
  • Fault Tolerant no single point of failure.
  • Ready for Cloud.
  • I’ve been working with few clients who have embraced containerization of their IMDG deployments. And in this blog post, I explore how to use Docker Compose to scale Hazelcast IMDG cluster. Additionally, I will use Management Center to monitor IMDG cluster state.

    Getting Docker Images

    In this demonstration, I will be using official Hazelcast IMDG Docker images. My demo application will contain three components:

    • Hazelcast IMDG cluster.
    • Hazelcast Management Center.
    • Client Application.

    I will build a Docker image for my client app that will read and write to the Hazelcast IMDG cluster.

    Official Stuff

    You can find officially supported images in Docker Hub. I will be using:

    Client App

    A client app is a simple hazelcast client:

    public class MyClient {
        public static void main(String[] args) throws InterruptedException {
            ClientConfig clientConfig = new XmlClientConfigBuilder().build();   (1)
            final HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig); (2)
            IQueue<String> queue = client.getQueue("queue");
            queue.put("Hello!");    (3)
            System.out.println("Message sent by Hazelcast Client!");
    1. Configured with hazelcast-client.xml
    2. Connects to the cluster.
    3. Sends a message to the IQueue.

    Building Client App Docker Image

    In this example, to build a Docker image from 'fat' jar, I used Spotify’s docker-maven-plugin.

    Below is my Maven snippet for generating a fat jar and building a Docker image:

                    <id>make-assembly</id> <!-- this is used for inheritance merges -->
                    <phase>package</phase> <!-- bind to the packaging phase -->

    1. Builds a fat jar to be deployed as a Docker image.
    2. Builds a Docker image with the Hazelcast client app and dependencies.

    Enter Docker Compose

    Docker Compose allows you to define a multi-container application with all of its dependencies in a single file, then spin your application and dependencies up in a single command. Navigate to the directory, like so: hazelcast-code-samples/hazelcast-integration/docker-compose/src/main/docker. Then, run the following command to start the cluster, client, and management center containers:

    docker-compose -f hazelcast.yml up -d

    The docker-compose command will pull the images from Docker Hub and then link them together based on the information inside the docker-compose.yml (hazelcast.yml in our case) file. This will create ports, links between containers, and configure applications as required. After the command completes, we can now view the status of our cluster with the docker-compose ps command.


    The fun part comes with scaling. Let’s scale out our Hazelcast IMDG cluster to 2 nodes.

    Below is the docker-compose command I used for scaling my Hazelcast cluster.

    docker-compose scale hazelcast=5

    With the docker-compose ps command, you can see which containers are running.

    ❯ docker-compose -f src/main/docker/hazelcast.yml ps
               Name                         Command               State            Ports
    docker_hazelcast-client_1    /bin/sh -c echo "The appli ...   Exit 0
    docker_hazelcast_1           ./server.sh                      Up       5701/tcp
    docker_hazelcast_2           ./server.sh                      Up       5701/tcp
    docker_hazelcast_3           ./server.sh                      Up       5701/tcp
    docker_hazelcast_4           ./server.sh                      Up       5701/tcp
    docker_hazelcast_5           ./server.sh                      Up       5701/tcp
    docker_management-center_1   /bin/sh -c ./start.sh            Up>8080/tcp

    If you open the Management Center URL you should see that cluster consists of 5 nodes now.

    mancenter 1

    Figure 2. Management Center web app displays IMDG cluster of 5 nodes.

    If at this point you get a message from the Management Center to enter the license, you can request a trial key.

    Also, I can use the curl command to get the clusters' status and the list of members.

    Below is hope I got the query cluster status using Management Center:

    ❯ curl http://localhost:8080/mancenter/rest/clusters/hz-compose/members

    You can shut down those application stacks (IMDG cluster, Management Center, Hazelcast client Java application) with a single command:

    docker-compose -f src/main/docker/hazelcast.yml down

    What’s Next?

    In my future posts, I will explore other tools that allow you to deploy, scale, and orchestrate your Hazelcast IMDG cluster with different tools like Swarm, Kubernetes, and Mesos. If you have questions or suggestions, feel free to comment below.








    cloud, docker, docker-compose, hazelcast

    Published at DZone with permission of Vik Gamov , DZone MVB. See the original article here.

    Opinions expressed by DZone contributors are their own.

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}