Set up session caching for applications in containers

The WebSphere Liberty session caching feature builds on an existing technology called JCache (JSR 107), which provides an API for distributed in-memory caching. We can set up session caching by configuring a JCache provider such as Infinispan or Hazelcast.


Set up session caching with Infinispan

Infinispan is an open source project that is the basis for Red Hat Data Grid. When we enable Infinispan session caching, the Infinispan client libraries are retrieved from the Infinispan JCACHE (JSR 107) Remote Implementation Maven repository, and the necessary infinispan.client.hotrod.* properties and the WebSphere Liberty server feature sessionCache-1.0 are configured by including the infinispan-client-sessioncache.xml XML snippet.

  1. Set up the Infinispan service.

    Ensure that we have an Infinispan service available in our Kubernetes environment. We can create our Infinispan service using the Infinispan Operator. The Infinispan Operator Tutorial explains how to get started with Infinispan in Red Hat OpenShift®.

  2. Install the client JAR files and set the INFINISPAN_SERVICE_NAME environment variable.

    To enable Infinispan in WebSphere Liberty, the container image author can use the provided Dockerfile. This Dockerfile assumes an Infinispan service name of example-infinispan, which is the default used in the Infinispan Operator Tutorial. To customize our Infinispan service, see Create Infinispan Clusters. The INFINISPAN_SERVICE_NAME environment variable must be set at build time as shown in the example Dockerfile, or overridden at image deployment time.

    Tip: If our Infinispan deployment and WebSphere Liberty deployment are in different namespaces or projects, set the INFINISPAN_HOST, INFINISPAN_PORT, INFINISPAN_USER, and INFINISPAN_PASS environment variables in addition to the INFINISPAN_SERVICE_NAME environment variable. Set these environment variables ensures that the WebSphere Liberty deployment has access to the Infinispan service environment variables it requires.


Set up session caching with Hazelcast

Hazelcast In-Memory Data Grid is another JCache provider. When we enable Hazelcast session caching, the Hazelcast client libraries are retrieved from the hazelcast/hazelcast container image, and Hazelcast is configured by copying a sample hazelcast.xml. The WebSphere Liberty server feature sessionCache-1.0 is configured by including the XML snippet hazelcast-sessioncache.xml. By default, the Hazelcast Discovery Plugin for Kubernetes automatically discovers its peers within the same Kubernetes namespace. To enable session caching with Hazelcast, the container image author can include the following Dockerfile snippet, and choose from either client/server or embedded topology.

    ### Hazelcast Session Caching #### 
    Copy the Hazelcast libraries from the Hazelcast container image
    COPY --from=hazelcast/hazelcast --chown=1001:0 /opt/hazelcast/lib/*.jar /opt/ibm/wlp/usr/shared/resources/hazelcast/
    
    # Instruct configure.sh to copy the client topology hazelcast.xml
    ARG HZ_SESSION_CACHE=client
    
    # Default setting for the verbose option
    ARG VERBOSE=false
    
    # Instruct configure.sh to copy the embedded topology hazelcast.xml and set the required system property
    #ARG HZ_SESSION_CACHE=embedded
    #ENV JAVA_TOOL_OPTIONS="-Dhazelcast.jcache.provider.type=server ${JAVA_TOOL_OPTIONS}"
    
    ## This script will add the requested XML snippets and grow image to be fit-for-purpose
    RUN configure.sh