3

Let say i have a table which has Master Data seeded 1 time, and rarely it gets updated. It has a flat structure entity, nothing sort of Lazy attributes.

Address{
     id,
     country,
     state,
     city,
     pincode,
     area
}

Case -1 : Table has 1000 records

Case-2 : Table has 1 million records

Use Case - Need to get full Address object by id. No updation in transaction in this table, just used for display purpose, LOV's, reference Id etc.

  1. Considering performance, memory usages, GC, search speed etc which one is better using Hibernate cache(id, object) for this or just using static HashMap (is as key, Address object as value).

  2. I know Hibernate 2nd level cache (ehcache,Infinispan etc) also uses Map structure deep inside libraries. So does it add any useful in above cases, to add extra layer of Hibernate.

Iamat8
  • 3,888
  • 9
  • 25
  • 35
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • are you referring to the first level of hibernate cache on your first point? – kucing_terbang Oct 08 '15 at 10:35
  • @kucing_terbang no it's purely 2nd level cache vs hashmap in topic, no Ist level – Ankur Singhal Oct 08 '15 at 10:39
  • then I will choose to go with hibernate cache (ehcache, etc). Unless you have a huge memory reserved for JVM. – kucing_terbang Oct 08 '15 at 10:52
  • @ kucing_terbang Yes, so i want to know how is Hibernate managing objects, isn't storing in-memory..??, how it stores id and object in this case and where, is it purely on disk. – Ankur Singhal Oct 08 '15 at 10:58
  • yes, hibernate will store it into the memory until the transaction has ended (which it will write the data into the database/disk and clean the cache/memory). the treatment should be the same on your case as in the end, the `Address` is just another entity for hibernate. – kucing_terbang Oct 08 '15 at 11:03
  • @kucing_terbang There will be no updates in this entity, no inserts, only read. If both `HashMap` and `2nd level cache` storing in memory, then what is the diff btw the two. – Ankur Singhal Oct 08 '15 at 11:05
  • Basicallly, 2nd level cache provide you with already made scalability. I mean, if you only use hashmap. You will need to also take care yourself if let say the size of data increase. And to make my last comment more clear. The data is cached in memory but you will need to open the transaction session when you get the data indefinitely then. – kucing_terbang Oct 08 '15 at 11:47

1 Answers1

1

Ehcache ,Hazelcast(for 2nd level cache) etc has more features than a static Hashmap.

1.limit the maximum number of elements in memory

2.overflow to disk (if the above number is exceeded)

3.set a time-to-live and time-to-idle for elements

4.allows replication within a cluster

If you want these features then you should go for this.Otherwise static hashmap can do your job. Make it refresh after some time if your records gets updated.

One more thing,in both the cases data will be stored in memory or you can say on Heap. But if you want to store data OFF Heap then read about MapDB.

I have worked on hazelcast as 2nd level cache provider.It gives distributed cache as well and work well in clustering environments.

Check these links Disk based HashMap Ehcache Vs Static map cache implementation

Community
  • 1
  • 1
Vikas Sharma
  • 745
  • 9
  • 19