Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are DB hits costlier than accessing collection in java?

Just implemented a design where i had cached some data in hashmap and retrieved data from it instead querying the same data from DB.

Is my thinking correct ?

like image 780
ashwinsakthi Avatar asked Dec 05 '22 13:12

ashwinsakthi


1 Answers

Keeping a copy of the data in memory would almost certainly be faster than fetching it from the DB.

That said, there are further considerations to be taken into account:

  1. Can the data in the DB change while you're holding an in-memory copy? If so, how are you going to deal with that?
  2. Is memory consumption going to be an issue?
  3. Are you certain that you're optimizing a real, and not an imagined, bottleneck?
like image 116
NPE Avatar answered Dec 29 '22 04:12

NPE