java - Refreshing Caches while under load with Spring/EHCache -
I have a caching problem on Spring Multi-Threaded Web Services with database backend and earch-based caching Many customers in the service All repeat requests to the same object, with dozens of requests per second. There are only a few things that are requested that often a large number of other objects are requested with no time, the objects can change every two minutes, so the TTL of the cash is set to one minute. Loading objects from the database is slow and takes at least several seconds.
At first I used a simple implementation to get the object:
- Check that the object is in the cache.
- If yes, then return it to the cache.
- If not, then load it from the database, keep it in the cache and return it.
It was working well while testing it locally but performance tests on fast servers are shown very poorly loaded spikes a few times, each time a more frequently requested object Ends in the cache. When this happens, for the next 10 seconds, all requests for that object will result in database load, unless the first thread ends up loading the database and insert the new object into the cache. The result was a small but very heavy load on the database, and many users needed to wait to complete.
My current implementation improves the repair of database loads whether the object is currently being loaded or not loaded:
- Check that the object is cached or not.
- If yes, then return to the cache.
- If not, the object or not is currently being loaded.
- If so, wait for the second thread to complete the load, get a new object from the cache and return it.
- If not, then put the object in the list after loading the objects, then keep it in the cache when finished and return it.
With this implementation, when the object expires, there is only one database operation and, due to low database load, it will end soon, but it It still means that all the users requesting objects during the object load need to wait.
What I really want is that only the first thread waits for the database load, and all others are only 'time-consuming' objects while the object is being loaded. Response time is more important to me than the fact that the object is a few seconds old.
Alternatively, I can refresh the cache asynchronous when I saw that the object will end in a few seconds. This is close to the eTechTech's single TTL model and it will mean that no one is required to wait for the database load
My real question is that before I invent the wheel again Is there any existing structure that already applies in some way (in a spring / eachchal environment)? Or maybe support for this is already present in spring / eacrech and I can not find the right option? "post-text" itemprop = "text">
There are two types of structures provided that can help you:
- Scheduled refresh
Both you need to change the paths you interact with your cache because they require
CacheLoader to configure them. .
Unfortunately, I can not find an online document that shows an example for the second option. It allows to reserve cache entries by using quartz to schedule it. This key can only refresh a subset of keys based on the generator. See classes in package
net.sf.ehcache.constructs.scheduledrefresh
Comments
Post a Comment