Configuring Open Liberty or WebSphere Liberty Session Persistence With JCache and Redis

The Java developers who create today's high-performance web applications must deliver seamless user experiences. User sessions must be interrupted, even when multiple application servers exist across the globe, or in the case of unexpected events like an app crash or a server failure. 

With Redis, WebSphere Liberty (or the open-source Open Liberty), and Java's standard caching API (JCache), developers can achieve session persistence—the key to perfectly seamless user sessions.

What Is Session Persistence?

Session persistence refers to the ability to store user session data beyond the lifecycle of a single HTTP request. This is essential for maintaining stateful user interactions, such as remembering login details, shopping cart contents, and user preferences. Traditionally, sessions might be stored in memory on the server, but this approach doesn't scale well in distributed environments or when unexpected server restarts occur.

Session persistence solves this by providing a reliable way to store session data externally. This could be in a database, a file system, or an in-memory data store like Redis. In these setups, Redis works with JCache to create a distributed cache.

What Is a Distributed Cache?

A distributed cache extends the traditional concept of a cache,   typically stored in a single location, to the RAM of multiple servers. Since it's an in-memory data store, Redis is ideally suited for the task.

Java developers using Open Liberty or WebSphere Liberty enjoy native integration with JCache. This provides a roadmap to session persistence, as Redis' data persistence options ensure that even if an entire cache cluster goes down, you can recover session data. You just need to select the right library to interface with JCache, and that's where Redisson comes in.

Open Liberty or WebSphere Liberty Integration With Redisson

Redisson users can configure distributed cache with Open Liberty or WebSphere Liberty with a simple configuration, like in this  example:

<library id="jCacheVendorLib">

    <file name="${shared.resource.dir}/redisson-all-3.30.0.jar"/>

</library>

<cache id="io.openliberty.cache.authentication" name="io.openliberty.cache.authentication"

    cacheManagerRef="CacheManager" />

<cacheManager id="CacheManager" uri="file:${server.config.dir}/redisson-jcache.yaml"> 

    <properties fallback= "true"/>

    <cachingProvider jCacheLibraryRef= "jCacheVendorLib"/>

</cacheManager>

And here is a distributed Session persistence configuration example:

<featureManager>

    <feature>servlet-6.0</feature>

    <feature>sessionCache-1.0</feature>

</featureManager>

<httpEndpoint httpPort="${http.port}" httpsPort="${https.port}"

        id="defaultHttpEndpoint" host="*" />

<library id="jCacheVendorLib">

    <file name="${shared.resource.dir}/redisson-all-3.30.0.jar"/>

</library>

<httpSessionCache cacheManagerRef="CacheManager"/>

<cacheManager id="CacheManager" uri="file:${server.config.dir}/redisson-jcache.yaml"> 

    <properties fallback= "true"/>

    <cachingProvider jCacheLibraryRef= "jCacheVendorLib"/>

</cacheManager>

Additional settings are available per JCache instance to Redisson PRO users:

fallback setting. Skip errors if Redis cache is unavailable. Default value: false

implementation setting. Cache implementation. Default value: cache. Available values:

cache - standard implementation, clustered-local-cache - data partitioning and local cache support, local-cache - local cache support, clustered-cache - data partitioning support.

localcache.store_cache_miss setting. Description Defines whether to store a cache miss into the local cache. Default value: false

The above examples are just a few of the additional settings and features available to Redisson PRO users. To see the complete list of Redisson PRO parameters available for Open Liberty or WebSphere Liberty session persistence with JCache, check out the Redisson documentation.

Learn More About Redisson and Redisson PRO

To learn more about Redisson and its Open Liberty or WebSphere Liberty integration for distributed session persistence with JCache, visit the Redisson website.

Similar articles