1

We have a java based product which keeps Calculation object in database as blob. During runtime we keep this in memory for fast performance. Now there is another process which updates this Calculation object in database at regular interval. Now, what could be the best strategy to implement so that when this object get updated in database, the cache removes the stored object and fetch it again from database.

I won't prefer any caching framework until it is must to use.

I appreciate response on this.

Roman C
  • 49,761
  • 33
  • 66
  • 176
user1986187
  • 21
  • 1
  • 3

3 Answers3

2

It is very difficult to give you good answer to your question without any knowledge of your system architecture, design constraints, your IT strategy etc.

Personally I would use Messaging pattern to solve this issue. A few advantages of that pattern are as follows:

  • Your system components (Calculation process, update process) can be loosely coupled
  • Depending on implementation of Messaging pattern you can "connect" many Calculation processes (out-scaling) and many update processes (with master-slave approach).

However, implementing Messaging pattern might be very challenging task and I would recommend taking one of the existing frameworks or products.

I hope that will help at least a bit.

Tom
  • 26,212
  • 21
  • 100
  • 111
0

I did some work similar to your scenario before, generally there are 2 ways. One, the cache holder poll the database regularly, fetch the data it needs and keep it in the memory. The data can be stored in a HashMap or some other collections. This approach is simple and easy to implement, no extra framework or library needed. But users will have to endure dirty data from time to time. Besides, polling will cause a lot of pressure on DB if the number of pollers is huge or the query is not fast enough. However, it is generally not a bad one if your requirement for real-time is not that high and the scale of your system is relatively small.

The other approach is that the cache holder subscribes the notification of the data updater and update its data after being notified. It provides better user experience, but this will bring more complexity to your system because you have to get some MS infrastructure, such as JMS, involved. Developing and tuning is more time-consuming.

Gavin Xiong
  • 957
  • 6
  • 9
0

I know I am quite late resonding this but it might help somebody searching for the same issue. Here was my problem, I was storing requestPerMinute information in a Hashmap in a Java filter which gets loaded during the start of the application. The problem if somebody updates the DB with new information ,the map doesn't know about this. Solution: I took one variable updateTime in my Java filter which just stored when was my hashmap last got updated and with every request it checks if the current time is time more than 24 hours , if yes then it updates the hashmap from the database.So every 24 hours it just refreshes the whole hashmap. Although my usecase was not to update at real time so it fits the use case.

mishi
  • 33
  • 1
  • 4