2

I am developing a web application in which I need to store session, user messages etc. I am thinking of using HashMap or H2 database.

Please let me know which is better approach in terms of performance and memory utilization. The web site has to support 10,000 users.

Thanks.

jaks
  • 4,407
  • 9
  • 53
  • 68

5 Answers5

3

As usual with these questions, I would worry about performance as/when you know it's an issue.

10000 users is not a lot of data to hold in memory. I would likely start off with a standard Java collection, and look at performance when you predict it's going to cause you grief.

Abstract out the access to this Java collection such that when you substitute it, the refactoring required is localised (and perhaps make it configurable, such that you can easily perform before/after performance tests with your different solutions -H2, Derby, Oracle, etc. etc.)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

If your session objects aren't too big (which should be the case), there is no need to persist them in a database.

Using a database for this would add a lot of complexity in a case when you can start with a few lines of code. So don't use a database, simply store them in a ligth memory structure (HashMap for example).

You may need to implement a way to clean your HashMap if you don't want to keep sessions in memory when the user left from a long time. Many solutions are available (the easiest is simply to have a background thread removing from time to time the too old sessions). Note that it's usually easier to clean a hashmap than a database.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

Both H2 and Hash Map are gonna keep the data in memory (So from space point of view they are almost the same).

If look ups are simple like KEY VALUE then looking up in the Hash Map will be quicker.

If you have to do comparisons like KEY < 100 etc use H2.

In fact 10K user info is not that high a number.

Vivek
  • 1,316
  • 1
  • 13
  • 23
0

If you don't need to save user messages - use the collections. But if the message is should be saved, be sure to use a database. Because after restart you lost all data.

native1989
  • 95
  • 3
  • 10
0

The problem with using a HashMap for storing objects is that you would run into issues when your site becomes too big for one server and would need to be clustered in order to scale with demand. Then you would face problems with how to synchronise the HashMap instances on different servers.

A possible alternative would be to use a key-value store like Redis as you won't need the structure of a database or even use the distributed cache abilities of something like EHCache

beny23
  • 34,390
  • 5
  • 82
  • 85