Search code examples
javajspservletshttpsession

Amount of data stored in session


What technique should we use to make the httpsession object not loaded heavily with data.

Example :

Request 1 ---- > httpSession loaded with an arraylist of 50,000 different objects. session.setAttribute("data",arraylist);

Request 2 ---- > httpSession loaded with an arraylist of 40,000 different objects. session.setAttribute("data",arraylist);

Assume the server is loaded heavily with multiple sessions and huge data in them. Let us say from my above example request1..1000 at a time. Which means 1000 session objects with huge data.

What is the alternate way to solve instead storing them in session like this?


Solution

  • Some ideas:

    1. Be more selective about which data you really need to serve to your client, instead of plonking everything in the Session on the first request you could only load data when the client really needs it (c.q. when a tab is clicked by the user). You can also use specialized lightweight classes instead of your full backend domain classes to pass the data to the frontend.

    2. Check your domain model, and see if you can split out any 'static data'. By this I mean data which is commonly shared within your application and does not change a lot, like zipcodes. This type of data is a good candidate for caching and passing by reference, instead of duplicating it.

    3. As mentioned, use a caching framework like Ehcache. It reduces the need for plumbing code in your application and allows shared caching across all sessions instead of duplicating the data. Of course if you are only storing user-specific data in your session, then the sharing won't be of big benefit. A framework like that also allows you to configure the caching strategy, for instance so that it will start using the database if necessary.