0
votes

I want to deploy a servlet on a two-nodes tomcat cluster, which use hazelcast caching. The setup is as follow:

  • two VM. each with tomcat7 setup.
  • a java program to instantiate the hazelcast on each VM. distributed cache is created across the two VM independent of the tomcat context

The servlet creates hazelcast client connection, and access the IMAP in hazelcast.

I noted, in some forum, saying to instantiate the hazelcast in contextInitialized() function in the tomcat. So, whenever the tomcat context starts, hazelcast will start; the tomcat context is destroyed, hazelcast will be destroyed too.

I am in doubt whether instantiating the hazelcast in contextInitialized() function will be more efficient and appropriate for tomcat-hazelcast integration? Or there is actually no much different if use my current java program to create the hazelcast, independent of the tomcat context lifecycle?

I am currently facing a problem of long response time of the servlet when multiple users access the serlvet webpage. I also use IMAP lock to make sure appropriate concurrency process. So, want to know how to tune it for better performance. Appreciate for any advice.

Thanks! Ryan

1

1 Answers

1
votes

My suggestion would be to make use of NearCache feature of Hazelcast clients so you can de-couple the Hazelcast node from tomcat.

Hazelcast Cluster: Setup a Hazelcast cluster with the IMap configuration and implement a MapStore which should take care of loading the IMap with the cache eligible segments. Also make use of Event Handlers to take care of refreshing the cache.

Tomcat Servlet Layer: Startup the Hazelcast clients on each of the tomcat servers and enable the NearCache on those clients. Once the Hazelcast client is up, the NearCache settings will take care of sync'ing the contents from its cluster. Both the tomcat instances would be up-to-date with the cache contents.

Things to remember:

  1. The number of nodes in the HZ Cluster depends on the size of the cache. Recommendation is to keep the Max Heap size to be around 4GB to avoid GC Overheads and spin up as many nodes as required.
  2. The NearCache will be part of the tomcat container itself, so allocate proper heap size when starting up tomcat.
  3. Partition the map contents properly and keep it small size to avoid I/O issues, as there will be data transfer between cluster and tomcat container during sync.

Let me know if this suits your architecture and have any questions.