How to configure Load Balancing Groups in WildFly

In this tutorial we will learn how to configure Load Balancing Groups with mod_cluster so that requests for your applications are not spread randomly across the full WildFly cluster. Application requests will be sent first to other nodes available in a given Balancing Group before it is sent to another Load Balancing Group.

Our starting point is a domain server configuration which is made up of 4 servers:

  • server-one and server-two in the “main-server-group
  • server-three and server-four in the “other-server-group
    <servers>
        <server name="server-one" group="main-server-group"/>
        <server name="server-two" group="main-server-group" auto-start="true">
            <socket-bindings port-offset="150"/>
        </server>
        <server name="server-three" group="other-server-group" auto-start="true">
            <socket-bindings port-offset="250"/>
        </server>
        <server name="server-four" group="other-server-group" auto-start="true">
            <socket-bindings port-offset="350"/>
        </server>
    </servers>

Our server groups are bound to the “ha” profile configuration:

    <server-groups>
        <server-group name="main-server-group" profile="ha">
            <jvm name="default">
                <heap size="64m" max-size="512m"/>
            </jvm>
            <socket-binding-group ref="full-sockets"/>
        </server-group>
        <server-group name="other-server-group" profile="ha">
            <jvm name="default">
                <heap size="64m" max-size="512m"/>
            </jvm>
            <socket-binding-group ref="full-ha-sockets"/>
        </server-group>
    </server-groups>

Now let’s set a System Property on each Server Group to specify the Load Balancing Group to whom they belong:

/server-group=main-server-group/system-property=modcluster-balancing-group:add(value=main-group)

/server-group=other-server-group/system-property=modcluster-balancing-group:add(value=other-group)

Now, for your profile, configure the load-balancing-group attribute for your mod_cluster proxy:

/profile=ha/subsystem=modcluster/proxy=default:write-attribute(name=load-balancing-group,value=${modcluster-balancing-group})

/host=master:reload

You should have the following configuration in the profile section of your domain.xml:

<subsystem xmlns="urn:jboss:domain:modcluster:5.0">
    <proxy name="default" advertise-socket="modcluster" listener="ajp" load-balancing-group="${modcluster-balancing-group}">
        <dynamic-load-provider>
            <load-metric type="cpu"/>
        </dynamic-load-provider>
    </proxy>
</subsystem>

Then, within the server-groups section of the domain.xml file:

    <server-groups>
        <server-group name="main-server-group" profile="full">
            <jvm name="default">
                <heap size="64m" max-size="512m"/>
            </jvm>
            <socket-binding-group ref="full-sockets"/>
            <system-properties>
                <property name="modcluster-balancing-group" value="main-group"/>
            </system-properties>
        </server-group>
        <server-group name="other-server-group" profile="full-ha">
            <jvm name="default">
                <heap size="64m" max-size="512m"/>
            </jvm>
            <socket-binding-group ref="full-ha-sockets"/>
            <system-properties>
                <property name="modcluster-balancing-group" value="other-group"/>
            </system-properties>
        </server-group>
    </server-groups>

What happens in practice? when a new request mod_cluster tries to intelligently determine the best node available for the request.

Let’s assume that the first request lands on the server-one, which belongs to the “main-server-group” and the Load Balancing Group named “main-group”. Now what happens if server-one fails or it’s shutdown? As mod_cluster is aware that there is another server in the same Load Balancing Group, then server-two will serve the next requests.

The advantage of using a Load Balancing Group is that we can make the load balancing of your application more deterministic. This can make your cluster more scalable, for example if you don’t want to replicate your HTTP Session across all 4 cluster members but just across your Load Balancing Group.