8

I'm working on distributed web application and we decided to separate web module from business services to make it more scalable.

Here is the situation: We have one server instance that keeps web application (Controllers, JSPs, etc) and lots of server instances with business services. If web application needs any data it asks any of the existing business server via Hessian about it, then got the response and displays data.

Currently we retrieving data from DB based on logged in user and this cannot be changed, so every server should know which user asked to do the job.

My question is: Do you know the solution to keep user session across several independent applications?

For example one of the solutions can be send username with every request but this is not a very good idea for us.

Thanks a lot

oyushche
  • 81
  • 1
  • 4
  • Thank you all. I've found the best solution. We should simply add username (or whatever) as http request header and retrieve it on the other side. It is very easy with Hessian you should only extend HessianProxy and implement addRequestHeaders(URLConnection) method. Take care – oyushche Sep 10 '10 at 10:27

3 Answers3

9

There are 2 approaches for this problem:

1) Store all session info in central memcached server

2) Use session-aware load-balancer, which will direct same users to same nodes.

BarsMonster
  • 6,483
  • 2
  • 34
  • 47
  • 1
    3) Store all sessions in a master-master replicated database that supports multiple replicas of every session. That way, you will have failover. Also, see my provious answer here that might give you some insight: http://stackoverflow.com/a/8443896/260805 – Ztyx Dec 09 '11 at 10:12
  • Isn't it better to store sessions in centralized database? Instead of storing in memory cache or in file systems? – Nah Aug 30 '18 at 16:37
  • @MuhammadHannan Surely central database session storage is the simplest architecture. It is also the slowest. Session data is fetched on each page load, so you add quite a bit of extra latency and server load... – BarsMonster Aug 30 '18 at 20:28
  • Then what are recommended approaches to improve the speed. In reference to PHP (Apache), Node.js. Just quick hints/link and I'll do further research on it myself. – Nah Aug 31 '18 at 03:45
4

Use distributed hashtable to store and retrieve your sessions from any server. Try Hazelcast for example. It is open source and super simple; see the sample below.

Map<String, Session> mapSessions  = Hazelcast.getMap("sessions");
// session is created or updated so put it into the sessions map
mapSessions.put(sessionId, session);
// any server needing to access a session should just retrieve
// it from the map. 
// Map is distributed/shared so any JVM running Hazelcast can
// read the sessions.
Session session = mapSessions.get(sessionId);

Hazelcast is peer-to-peer. Just include a single jar and start sharing your sessions.

Talip Ozturk
  • 1,709
  • 12
  • 14
0

A distributed Memcached server would be a better choice as it can manage the storage gracefully. Another alternative solution is to use Redis as it is quite faster than Memcached and it can store a different variety of data type and it could be used as a database as well

Kamal
  • 309
  • 2
  • 5