Search code examples
javahibernatejpasecond-level-cache

Read only vs NonRestrict Read Write in Cache Concurrency in Hibernate


I am a beginner in Hibernate and came across these concepts in JPA second level cache concurrency strategy:

Read-Only: Used when the cache is never updated. Data like names of countries etc are suitable candidate

Non-Strict Read-Write: Data that is rarely updated.

I am confused as to what exactly is the difference between the two.


Solution

  • You use the read-only for cache entries that are queried once, usually during the startup of an application or upon the first request and it is certain that the result loaded will never change during the lifetime of the application. Like in the description the list of countries is a good example.

    For the Non-Strict Read-Write, you use this option when an update to the cached result may change occasionally.

    For example the days of the week that a shop is opened. This, in general, does not change but due to some renovation the next Sunday may be closed and that would cause an update to the cache.

    This enforces additional checks and synchronization on the persistence provider thus its performance is not the highest (like in read-only).

    You need to judge whether it is more relevant to use read-only whenever possible and restart the server when a rare change of the dictionaries occur or implement the Non-Strict Read-Write and deal with a bit slower performance but without the need of restarting the server every now and then.