Choosing Between read-write and nonstrict-read-write Caching for Grails Domain Classes
0 reputation · 03 Feb 2022, 01:46 UTC
0 reputation · 03 Feb 2022, 01:46 UTC
Determine the best second‑level cache strategy to minimize stale reads when domain objects are updated concurrently.
Grails maps domain classes to Hibernate entities. The cache configuration is declared in the domain class’s static mapping block, e.g. cache usage: 'read-write' or cache usage: 'nonstrict-read-write'. The choice affects soft‑lock overhead, write‑side latency, and the likelihood of readers seeing outdated data.
Should the application use read-write caching, which enforces soft locks on updates, or nonstrict-read-write, which foregoes locking and accepts occasional stale reads?
read-write caching?read-write compared to the lock‑free nonstrict-read-write strategy when write traffic is high?cacheable: true) interact with entity cache invalidation for each strategy, and can it exacerbate stale‑data visibility?If your workload has high write contention on a few hot entities and you can tolerate a few milliseconds of stale reads, choose nonstrict-read-write. If you need guaranteed visibility of committed updates for every reader and write contention is moderate, choose read-write.
When a transaction updates an entity, Hibernate places a soft‑lock token in the second‑level cache for that key. Readers in other transactions block on that lock until the updating transaction commits. After commit the lock is released and the refreshed value is visible. The latency equals lock hold time plus cache propagation (typically a few milliseconds on a single node).
Updates evict or invalidate the cache entry asynchronously without locking. A reader that already holds a cached copy or races the invalidation can return stale data until the next cache miss forces a database reload. The stale‑read window depends on the cache provider’s invalidation latency (often milliseconds in clustered caches, but can be longer under replication lag).
read-write serializes access to hot keys because each update acquires the soft lock. This adds write‑side latency and reduces throughput proportionally to contention intensity. nonstrict-read-write avoids locking entirely, so write throughput scales better, at the cost of occasional stale reads.
Enabling cacheable: true stores result‑set identifiers in the query cache. If the entity cache is invalidated but the query cache retains stale identifiers, subsequent query executions may return identifiers for entities that are no longer current, worsening stale‑data visibility for both strategies. Query cache regions must be evicted in sync with entity cache regions.
nonstrict-read-write stale‑read probability rises with replication lag, while read-write locks are usually local to the node acquiring them, so cross‑node readers may still see brief staleness until invalidation arrives.hibernate.generate_statistics=true and monitor second_level_cache_put_count, second_level_cache_hit_count, second_level_cache_miss_count, and any lock‑contention metrics under realistic load.replicateUpdatesViaCopy).hibernate.cache.use_query_cache=true and that eviction/expiration policies match the entity cache policies.What cache provider and cluster topology (standalone, replicated, distributed) are you using? The answer changes the recommendation because soft‑lock scope and invalidation latency differ markedly between providers.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.