2
votes

One downside to distributed caching is that every cache query (hit or miss) is a network request which will obviously never be as fast as a local in memory cache. Often this is a worthy tradeoff to avoid cache duplication, data inconsistencies, and cache size constraints. In my particular case, it's only data inconsistency that I'm concerned about. The size of the cached data is fairly small and the number of application servers is small enough that the additional load on the database to populate the duplicated caches wouldn't be a big deal. I'd really like to have the speed (and lower complexity) of a local cache, but my data set does get updated by the same application around 50 times per day. That means that each of these application servers would need to know to invalidate their local caches when these writes occurred.

A simple approach would be to have a database table with a column for a cache key and a timestamp for when the data was last updated. The application could query this table to determine if it needs to expire it's local cache. Yes, this is a network request as well, but it would be much faster than transporting the entire cached data set over the network. Before I go and build something custom, is there an existing caching product for Java/Spring that can be configured to work this way? Is there a gotcha I'm not thinking about? Note that this isn't data that has to be transactionally consistent. If the application servers were out of sync by a few seconds, it wouldn't be a problem.

1
AFIAK Infinispan also supports a replicated mode. We also built the database approach, like you describe it, in our applications with cache2k on top of it for super fast local access. I can share that, although I need a bit of time to make it generic first. - cruftex
Thanks, @cruftex. Looking at the docs for Infinispan, I see that it does have a near cache mode to support local access, but from what I can tell, it still requires a cache server which I'd like to avoid if possible. I'm going to look into your approach with cache2k. If I get stuck I may take up you up on the code sharing. Appreciate it! - Jason Nesbitt

1 Answers

0
votes

I don't know of any implementation that queries the database in the way you specify. What does exist are solutions where changes in local caches are distributed among the members in a group. JBossCache is an example where you also have the option to only distribute invalidation of objects. This might be the closest to what you are after.

https://access.redhat.com/documentation/en-us/jboss_enterprise_application_platform/4.3/html/cache_frequently_asked_questions/tree_cache#a19

JBossCache is not a spring component as such, but you create and use a cache as a spring bean should not be a problem.