Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FlashAttributes are not working properly when application deployed on a clustered environment

I am using redirectAttributes to pass success or failure messages to redirected url. So that I can show the success or failure message on the redirected page only once. If the same page is refreshed again then the message will not come up again. This is ok and working fine in normal deployment on tomcat.

Now we have setup a clustered environment where we have deployed the web application. But in this case the redirectAttributes are working weirdly. Sometimes it works and sometimes not.

Following is the line of code I am using to add flashAttribute to the redirect attributes.

redirectAttributes.addFlashAttribute("successMsg", message);

I am using Spring 3.1.0.RELEASE version and Tomcat 7 for clustered environment.

I want to know whether there is any workaround for this problem. Does any newer Spring version supports the use of redirectAttributes in clustered environment? Also you can let me know if there is another way to perform this kind of stuff.

Thanks in advance.

like image 316
Japan Trivedi Avatar asked Dec 26 '22 17:12

Japan Trivedi


1 Answers

It sounds like your HTTP sessions for a client may not be shared between the Tomcat servers. Since the Spring Flash attributes are stored in the session, you may be experiencing the following:

  1. Initial request goes to serverA, and a flash attribute is set in the session on serverA
  2. A redirect occurs, and the request gets sent to serverB. serverA and serverB have different HTTP sessions for the user (assuming you have no mechanism to share them), so serverB does not see the flash attribute (it has it's own separate HTTP session)

You could experience this problem intermittently if the server to which a client request gets sent is non-deterministic. For example, if both of the above described requests happen to go to serverA, then the flash attribute would work correctly, since the session would be the same.

If this is the case, then you need a mechanism to either:

  1. Provide a 'sticky' session -- guarantee that all requests for a given client get routed to the same Tomcat server. Usually this is accomplished via a load-balancer / routing mechanism (example: nginx ip hash routing)
  2. Implement session replication -- make sessions shared across all Tomcat servers, so that regardless of which Tomcat serves the request for a client, the HTTP session will be the same.
like image 177
Matt Avatar answered Dec 31 '22 11:12

Matt