skip to Main Content

I am developing some SPA with a backend written in Java (Spring Boot). In relational DB that backend connects to, there is a table with some dictionary values. Values can edited by users of the app, but it’s done really, really rarely (almost never).
Those dictionary values are used in a lot of pages on UI and because of that I would like to "cache" them in a way. What I want to achieve is that I want to load dictionary values on startup to avoid asking DB for values during every request between UI and Backend.

Firstly, I thought about just loading it on the UI part of the app, when user enters the page for the first time. Then I ruled it out, since when one of the users changes the values, it should be reloaded.

What I think might work is just loading them on startup of Backend into some collection (that can be safely used in concurent environment, probably ConcurrentMap) and then during some GET requests asking that collection for the values (instead of DB). When the values are changed, that request just updates the DB table and reloads them into collection.

Then I thought that the collection solution won’t be enough, when my backend would be scaled up to more than one instance. In that case, only one of instances will be updated and the second one will provide outdated data. We can avoid it and force refreshes i.e. every 15 minutes (instead of on demand during values update).
But what I think is the best solution is to start some redis service on a side, load dictionary values into it and after every DB update of the values just update the redis instance with the new ones. Every instance of backend would use the same instance of redis, which seems quicker than executing query (select * from _ where _ = _) on DB.

What do you think? Is my thought process is correct? Do you have any ideas that can help solve my issue?

2

Answers


  1. I think your in the right path with your approach in terms of ‘caching’. I suggest you also check Memcached for it simplicity. Redis is a good choice but still it depends on your requirements and if you need that much feature. just my 2cent

    https://aws.amazon.com/elasticache/redis-vs-memcached/

    https://devcenter.heroku.com/articles/spring-boot-memcache#add-caching-to-spring-boot

    Thanks,

    Login or Signup to reply.
  2. If you are using Spring you could check out Spring Cache Abstraction. That way your cache will be up-to-date whenever some change occurs.
    Out of the box few implementations are supported by Spring:

    Spring provides a few implementations of that abstraction: JDK java.util.concurrent.ConcurrentMap based caches, Ehcache 2.x, Gemfire cache, Caffeine, and JSR-107 compliant caches (such as Ehcache 3.x). See Plugging-in Different Back-end Caches for more information on plugging in other cache stores and providers.

    If you decide to use Memcached implementation you can check out this library (uses Xmemcached under the hood) here.

    You could also check a small demo app of how to use Spring Cache Abstraction in your project (link).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search