Search code examples
nginxtomcatjsfredisload-balancing

JSF with redis for session management


I'm trying to use redis to store http sessions, because I want to use a load balancer to distribute the request between two servers (without sticky sessions). Currently, I'm using redisson (https://redisson.org/) with the tomcat configuration (https://redisson.org/articles/redis-based-tomcat-session-management.html) and nginx for load balancing.

Redisson configuration in both tomcats:

<Manager className="org.redisson.tomcat.RedissonSessionManager"
        configPath="${catalina.base}/conf/redisson.yaml" 
        readMode="MEMORY" updateMode="AFTER_REQUEST" broadcastSessionEvents="false"
        broadcastSessionUpdates="true"
        keyPrefix=""/>

In jsf:

<context-param>
  <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
  <param-value>client</param-value>
</context-param>

NGINX:

upstream samplecluster {
  server localhost:9090;
  server localhost:8080;
}

server {
 listen 4040;       
 server_name localhost;
 
  location / {
    proxy_pass http://samplecluster;
  }
}

I use, viewscoped beans. The problem is that when the different actions of the bean are distributed, sometimes data get lost. For example, from the main screen I open a modal to select specific data. That data, after being selected, should be shown in them main screen. That's not happening, the data get lost.

What could be wrong?


Solution

  • If you're going to do session replication, you should probably be using server side state saving.

    <context-param>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>server</param-value>
    </context-param>
    

    The reason being is MyFaces and other JSF implementations often generate encryption keys for encrypting/signing the state on the client side. You would have to manage and synchronize these keys across your servers in order for this to work perfectly.

    server works better in my opinion, and since you have a session anyway, you might as well use it!