B4J Question WEB APP Sessions with two servers

juventino883

Member
Licensed User
Longtime User
Hi! I have a doubt about WebApp User session, lets say that I have a web app running in two servers and I have a load balancer that distributes the traffic depending on the load, in this scheme could happen that a user makes a request and the load balancer send the user to server 1, but when this user makes another request, the seccond request could go to server 2 (because there is less load, or server 1 is not available) my question is how can we manage the user session between the two servers?

load balancer.png

I'm asking because currently I'm learning about kubernetes and this kind of things happend frequently and I was thinking about B4J webapps in this environment, and also, this is something that you have to solve if you need horizontal scaling for your app.
 

alwaysbusy

Expert
Licensed User
Longtime User
This should be done with sticky sessions by your load balancer I think. Could be a cookie injected by the load balancer so once you are redirected to a server, it should always go to this server for this session.

In HAProxy, this should be something like this:
B4X:
frontend haproxynode
        bind *:80
        default_backend backendnodes

backend backendnodes
        balance roundrobin
        cookie SRVNAME insert
        server server1 18.216.160.202:80 cookie S01  check
        server server2 18.218.98.36:80 cookie S02  check
 
Upvote 0

juventino883

Member
Licensed User
Longtime User
Hi @alwaysbusy thanks for your reply!, you are rigth it could be done with sticky sessions, the only con that I can see is that sticky sessions also make it more difficult to keep servers in balance. A server can become overloaded if it accumulates too many sessions, or if specific sticky sessions require a high number of resources. This could result in your load balancer having to shift a client to a different server mid-session, resulting in data loss.

As far as I understand, B4J webserver will create a cookie for the new user where it saves the session data, it could be possible to read this cookie if the client is shifted to a different server?
 
Upvote 0

juventino883

Member
Licensed User
Longtime User
yes, probably it's not possible, I was thinking in some alternatives like creating handlers to share the data or send requests like with microservices but, all the "solutions" that I think seems to be too complex, finding a way to share or reuse the cookie would be great.
 
Upvote 0

Chipbeard

New Member
Session tracking enables you to track a user's progress over multiple servlets or HTML pages, which, by nature, are stateless. A session is defined as a series of related browser requests that come from the same client during a certain time period. Session tracking ties together a series of browser requests think of these requests as pages that may have some meaning as a whole, such as a shopping cart application. For more detail also see https://www.b4x.com/android/forum/threads/web-app-sessions-with-bik-two-servers.140065/
 
Upvote 0

juventino883

Member
Licensed User
Longtime User
Hi @Chipbeard and @Erel thank you for your answers,

@Chipbeard the link that you have posted is a link to this thread, maybe you wanted to post another link?

@Erel currently I'm using Nginx as reverse proxy to point to the servers but how could I read the cookie created by server 1 in server 2?
 
Upvote 0
Top