my dog learned polymorphism*
The moose likes Servlets and the fly likes Storing large chunks of data in sessions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Storing large chunks of data in sessions" Watch "Storing large chunks of data in sessions" New topic
Author

Storing large chunks of data in sessions

shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Hi Guys

I am in need of some suggestions. The app I am working on is going to really strain the DB server, so to lower the load on the server I decided to use sessions. What happens is the servlet gets the data from the server, it is placed into a session and the customer will access the sessions rather than data constantly being requested from the DB. Is this a good method of dealing with the problem ? Or are there better ways of handling such a problem ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60758
    
  65

Do the math.

How many simultaneous users do you expect at peak? How much data are you storing in the session? Multiply.

Can your server handle the memory load?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Potentially hundreds, the requests can be large too. The best comparison I can give is like an email webapp. When the user wants to view his inbox, it'll load around 50 emails, with title, 20 words from beginning of email, date sent, receipient etc. So its either between memory (session) or hdd (DB). Sessions are quicker than DB too so thats another reason. I don't think hardware will be too much of an issue. We'll get more servers if needed, but considering you can get servers now with 128GB ram.. What do you guys think ?
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
Will there be any overlap in the data you're considering storing in the session? In other words, will users A and B both end up storing some of the same data in the session? If so, you might consider storing some common data types at the application level.


Fun programming etcetera!
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Not that I can think of. Im really stuck between the two. If I do use sessions then its going to be alot more work too.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

hi shaf,


if the data is need to the everypage of the particular user(example, like user id,role id,name,..),then you can put into session,otherwise dont use session,use request.

commonly, if you dump the datas into session, then it make your application slow according to the number of user
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60758
    
  65

seetharaman venkatasamy wrote:otherwise dont use session,use request.
He's talking about caching data so that he doesn't have to hit the DB on every request. Request scope is not an option here.
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

While saving the data in the session may work, it's hard to manage, especially if someone just closes their browser without signing off and you have to wait for the session to time out.

Have you looked at various Java caching libraries to reduce the database load? A distributed cache like memcached (search for java memcached) would make things better if you need to run on multiple boxes in the future, too.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12759
    
    5
Consider serializing the data to a disk file with a unique name that can be associated with a user.

I do this with on-line test servlets using a custom object to hold everything associated with a test session.

Serialization can be surprisingly fast, and with a little effort you can permit a user to resume a session after losing the connection and re-entering the application. Naturally you will need a mechanism to remove old serialized data to keep from filling up your disk.

Bill
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

I would stick to what Dave said. I didn't understand your reply shaf to Deve's question. Is your date user specific?? If not then just put the data in the ServletContext and then you can use it everytime you want. Also store the storing time with the data so that you can update it after a fixed period of time...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
 
subject: Storing large chunks of data in sessions
 
Similar Threads
can we streaming the A/V on the same machine?
Applet - Database Connectivity
Session Persistence in DB
Hibernate on Multiple Servers.
passing values of Select box from JSP to Action class thru form Bean