Session externalisation
Keep HTTP sessions in Data Grid so a JBoss EAP, Spring Boot or Tomcat pod can die without logging the user out.
An HTTP session stored in the pod that handled the login is a session stored on one node. The next request, or a reschedule, lands elsewhere and the user is logged out. Session externalisation writes the session to a Data Grid cache. Any pod that can reach the cache can serve the next click. The cache outlives the pod.
The cluster is example-infinispan. Create a cache that is only for sessions, distributed, synchronous, two owners, with a lifespan equal to the session timeout. Do not reuse catalog. A flush of the product cache must not log people out.
apiVersion: infinispan.org/v2alpha1
kind: Cache
metadata:
name: sessions
namespace: datagrid
spec:
clusterName: example-infinispan
name: sessions
template: |
distributedCache:
mode: "SYNC"
owners: "2"
statistics: "true"
encoding:
mediaType: "application/x-protostream"
expiration:
lifespan: "1800000"
Thirty minutes, in milliseconds. The application's session timeout and this lifespan should match. ProtoStream is what EAP 8 expects from a remote store. The connection details are the same as Connecting applications: Service DNS, BASIC intelligence, SCRAM, the service CA.
JBoss EAP
EAP keeps a local invalidation cache and a Hot Rod store behind it. The local cache is a near-cache. The entries that matter are on Data Grid. Mark the application <distributable/> in web.xml, or EAP will keep using the local session manager and ignore all of the following.
On the server, point a remote cache container at the Service and attach a Hot Rod store. The shape from the EAP 8 configuration guide:
/subsystem=infinispan/remote-cache-container=datagrid:add( \
default-remote-cluster=dg, marshaller=PROTOSTREAM)
/subsystem=infinispan/remote-cache-container=datagrid/remote-cluster=dg:add( \
socket-bindings=[dg-binding])
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=dg-binding:add( \
host=example-infinispan.datagrid.svc, port=11222)
/subsystem=infinispan/cache-container=web/invalidation-cache=sessions:add()
/subsystem=infinispan/cache-container=web/invalidation-cache=sessions/store=hotrod:add( \
remote-cache-container=datagrid, fetch-state=false, purge=false, \
passivation=false, shared=true)
/subsystem=infinispan/cache-container=web/invalidation-cache=sessions/component=transaction:add( \
mode=BATCH)
/subsystem=infinispan/cache-container=web/invalidation-cache=sessions/component=locking:add( \
isolation=REPEATABLE_READ)
marshaller=PROTOSTREAM is required on EAP 8. Without it, the first session write fails and the deployment can roll back. shared=true tells EAP that other nodes see the same store, which is the whole point. Put the Hot Rod username, password and truststore in the remote cache container's security, from a credential store or an environment variable the pod gets from a Secret. A password in standalone.xml in an image layer will leak.
On OpenShift, the EAP image reads this configuration from a ConfigMap mounted over the configuration directory, or from the Helm chart values that render it. After a rolling update, log in, note the session cookie, delete the pod that served you, and request again with the same cookie. You are still logged in. That is the test. A test that only reloads the page on one pod proves nothing.
Spring Boot
Spring Boot does not gain a distributed session because the Data Grid starter is on the classpath. You have to write the session through that client. With Spring Session, implement the repository against the remote cache, or store the attributes yourself under the session id:
RemoteCache<String, byte[]> sessions = manager.getCache("sessions");
sessions.put(sessionId, bytes, 30, TimeUnit.MINUTES);
The cookie then holds only the id. Whichever pod receives it loads bytes back. Set client intelligence to BASIC or each pod will try to open pod IPs and the session will flap. Use the same ProtoStream or the same JSON encoding on every replica. Two deployments with two marshallers will corrupt the entry and the failure looks like a random logout.
If you already run Spring Session on Redis from the Redis caching lab, do not point the same application at both. One store. Redis remains a good session store for polyglot services. Data Grid is the one that matches EAP beside it.
Tomcat and JBoss Web Server
JBoss Web Server uses the Tomcat session client shipped with Data Grid. Copy the client jars into the server's lib directory (the archive is the Tomcat session client on the Data Grid download, for the server version you run). In context.xml:
<Manager className="org.wildfly.clustering.tomcat.hotrod.HotRodManager"
configurationName="sessions"
server_list="example-infinispan.datagrid.svc:11222"
auth_username="developer"
auth_password="${DG_PASSWORD}"
sasl_mechanism="SCRAM-SHA-512"
sasl_properties.javax.security.sasl.qop="auth"
use_ssl="true"/>
configurationName is the cache. Properties are Hot Rod client properties with the infinispan.client.hotrod. prefix removed, which is what the session manager documents. Trust the service CA the same way as the other clients. A manager that falls back to the standard Tomcat manager will silently keep sessions in the pod. Check the log for the Hot Rod manager at startup, then run the delete-the-pod test.
Sticky routes and what they do not do
A Route with a session cookie still helps: it keeps a user on one pod so the near-cache is warm. It is not the durability. The moment that pod is gone, only the Data Grid copy remains. Size owners at 2. Set the lifespan. Alert on cache size so a leak of session ids cannot fill the cluster. And do not put secrets you would be sorry to see in a heap dump into the session "because it is convenient". The session is now in a second system, which is what you wanted, and that system needs the controls in the security lab.
What's next
Persistence is the store behind the cache, for data that has to survive the whole cluster restarting. Sessions usually should not. A user can log in again. A basket sometimes cannot, and that is a persistence decision, made on purpose.