Search code examples
spring-boottomcathazelcast

How to share Hazelcast cache over multi-war Tomcats


We have multiple Tomcats, each with multiple .war files (= Spring Boot app) deployed in it. We now need some distributed caching between app1 on tomcat1 and app1 on tomcat2. It´s essential that app2 on tomcat1 (and app2 on tomcat2) cannot see the Hazelcast cache of the other deployed apps.

The following image shows this situation:

         Tomcat 1                               Tomcat 2
         +-----------------------------------+  +-----------------------------------+
         |                                   |  |                                   |
         |    app1.war       app2.war        |  |    app1.war       app2.war        |
         |    +----------+   +----------+    |  |    +----------+   +----------+    |
         |    |          |   |          |    |  |    |          |   |          |    |
         |    |          |   |          |    |  |    |          |   |          |    |
         |    |          |   |          |    |  |    |          |   |          |    |
         |    |          |   |          |    |  |    |          |   |          |    |
         |    |          |   |          |    |  |    |          |   |          |    |
         |    |          |   |          |    |  |    |          |   |          |    |
         |    |          |   |          |    |  |    |          |   |          |    |
         |    |          |   |          |    |  |    |          |   |          |    |
         |    +----+-----+   +----+-----+    |  |    +----+-----+   +-----+----+    |
         |         |              |          |  |         ^               ^         |
         +-----------------------------------+  +-----------------------------------+
                   |              |                       |               |
                   |              |                       |               |
                   |              |                       |               |
                   |              |                       |               |
                   +--------------------------------------+               |
Shared cache via Hazelcast        |                                       |
                                  |                                       |
                                  +---------------------------------------+
                                  Shared cache via Hazelcast

Is this possible with Hazelcast? And if so, how? Right now I only find solution talking about shared web sessions via Hazelcast. But this doesn´t seem to be a solution for me here, or am I wrong?


Solution

  • If your applications must be strictly isolated, then you probably need to use different cluster groups. Cluster groups make it possible for different clusters to coexist on the same network, while being completely unreachable to one another (assuming correct configuration).

    If, however, you just need application data to be separate, then you can just make sure that app1 instances use caches with names that do not clash with app2 cache names. This is the simplest implementation.

    If you are deploying a sort of multitenant environment where you have security boundaries between the two groups of applications, then going for the cluster group option is better as you can protect clusters with passwords, and applications will be using distinct ports to talk to one another in those groups.