0

I have an application in spring boot that handles custom requests. And there exists a scheduled job running every 30 sec and it fetches a list of objects from db and performs certain operations. I get freaked out thinking that I need to query the database for every 30 seconds, and instead I could keep these objects in a data structure. There are rest APIs that adds or deletes or edits the properties of these objects and therefore, I would need to handle update of these objects In the data structure as well. Which approach should I go for ?

The size of the objects can be anything between 100 - 10000. I use Postgres as my db.

Dalu
  • 172
  • 3
  • 18
  • Try looking this and find some good in memory db: https://stackoverflow.com/questions/439958/highest-performance-database-in-java – Harry Joy Oct 09 '19 at 12:03
  • If you integrate an ORM to your project this would be a simple task. then you can use 2nd level cache. – Hadi Moloodi Oct 09 '19 at 12:06
  • How large is your query? Worrying about a database hit every 30 seconds sounds exaggerated, when you consider that databases perform thousands of transactions per second. – Kayaman Oct 09 '19 at 12:11
  • It is a simple select statement something like 'SELECT distinct(name, id) from table'. May be I am just being paranoid here – Dalu Oct 09 '19 at 12:14

2 Answers2

1

I don't think that you will need to save the data to a custom data structure, instead you could take a look at Hibernate's 2nd level cache for both entities and queries.

With proper settings the Hibernate will cache the data and the query you are using for you.

There is good article on Baeldung about 2nd level cache that can get you started.

toucheqt
  • 725
  • 2
  • 8
  • 15
  • Hibernate is not used in my application, rather I use JDBCTemplate. – Dalu Oct 09 '19 at 12:04
  • Sorry, my bad that I assumed the use of Hibernate with Spring Boot, however I think there is an implementation of JDBC Caching in the EHCache dependency anyway - you can look at that, it should work in a similar fashion. – toucheqt Oct 09 '19 at 12:07
0

i believe you should write a database listener that listens on DB records change event then fetches the records in the data structure of your choice then caches the records to be used in case the DB change event isn't triggered

  • this is a good article on how to implement db listener in java
Ahmed Maher
  • 1,521
  • 17
  • 22