Caching all entities in Cache Layer and Synchronizing with Database - caching

Is it possible, reliable and secure to cache all entities in distrubuted cache and notifies dao layer on update? My possible idea is;
Use JPA 2.1 and Hibernate implementation.
On creation persist it db
After persisting it, cache it to distrubuted cache.
Canalise all read actions to cache
on update notify dao layer to update entity .

yes you can design a system that will
On addition: persists data to db and adds to cache
On read: reads data from cache, and considers a cache miss as not
present in database as well.
On update: updates data in db and then updates in cache (or vice
versa)
On delete: deletes data from cache and then deletes from database
This approach will work fine if you have a single application using that database and if data is not that critical. However if data integrity is of more importance, you may face following problems in this approach:
You may face a cache miss when data is present in database(persisted
but not yet cached)
You may get stale data from cache (updated in db but not yet updated
in cache)
Also if data is removed from database by some other application, it
will still ramained cached in distributed cache(invalid data on
reads)
A better mothod my be if you use a rich featured distributed caching solution like NCache / Tayzgrid which provides Read Trough / Write behind features. This way your application will only need to use cache for all reads, writes or updates and cache will keep database updated using configured providers.
Another approach may be to use distributed cache as hibernate's second level cache and you will not need to add a caching layer by your self. See this article for details about hibernate's second level cache.
Distributed caching solutions like Tayzgrid provide caching provider for hibernate that can be easily configured. You can find hibernate providers for other solutions as well.

Related

Load data from Database once and available all the time using spring and show in JSP

I want to Load Dropdown data from Database at once and set inside java object and tie to my view (JSP page ) and available all the time for that particular controller or functionality using spring mvc AND jsp pages.
I dont want to load on application start up as ours is big one and and each functionality is independent.
It takes a lot of time to start the application if i load on application start up
Is there a way to it using spring mvc pattern and using JSP
Could someone please let me know how to do it
As you have not mentioned how frequently you are doing the database operation or how frequently you are fetching the data. Considering the average user.
Approach: Create your own local cache/ program cache implementation.
Instead of loading all the data from the database during startup, load only master data which will be common for all. If master data is also high then you can perform the lazy loading approach.
Load the data of a specific feature when it is requested for the first time. Keep the data in the local cache.
Whenever someone is making the changes then add the data in the cache and save the same to the database. so you will always have latest data in the cache.
Advantage:
Very useful for common or static master data
-If you need good business logic for some common data. This way only once you are processing the data and keeping cache.
-Fetching the data is very fast as it doesn't involve database request except for the first time
Disadvantage:
If you have a very high number of users and a very high update operation then the updating cache will delay the update process as you need to update it sequentially.
I suggest you can use a combination of approaches to improve the code quality and processing.
This sounds like a typical cache functionality.
Spring supports caching out of the box by #EnableCaching on application level and #Cacheable(“cachename”) on the repository method retrieving your dropdown data. In your simple use case you not even need an additional framework as there is a CacheManager based on ConcurrentHashMap which simply caches for ever.
With caching in place your controller can simply fetch the dropdown data from the repository. Caching will ensure only the first call will really fetch from database and keeps the result in memory for all upcoming calls.
If you ever need more sophisticated caching you only have to exchange the cache manager and configure the cache for your needs.

Refresh cache triggered by changes in database is it possible?

In my application I'm using Spring, Hibernate and EHCache. Problem is that database is being modifiy by another application.
Is it possible, is there is any implemented solution to refresh cache based on some database changes? I can use some additional column for versioning etc.
Any ideas?
What you are looking for is know as Read-Write Through Caching. EhCache supports it.
From EhCache Documentation:
Write-through caching is a caching pattern where writes to the cache
cause writes to an underlying resource. The cache acts as a facade to
the underlying resource. With this pattern, it often makes sense to
read through the cache too. Write-behind caching uses the same client
API; however, the write happens asynchronously. Ehcache-2.0 introduced
write-through and write-behind caching. While file systems or a
web-service clients can underlie the facade of a write-through cache,
the most common underlying resource is a database.
Here is a detailed article:
http://www.ehcache.org/documentation/2.8/apis/write-through-caching.html#write-through-and-write-behind-caching-with-the-cachewriter-
Ehcache does not support such a scenario out of the box. Write-through is when changes flow the other way: your application updates the cache and in the background the cache pushes the updates to the database.
In order to push database updates to your application, you will have to come up with your own solution. The ideal way is to not have two different applications hitting the same data but instead have one of the two applications go through the other, so that a single application owns this set of data.
Used below solution to refresh cache after each database save
#CacheEvict(cacheNames = {"my-cache"}, allEntries = true)
public MyObject save(MyObject object) {
MyObject myObject = myRepo.save(object);
CompletableFuture.runAsync(() -> config.loadData());
return myObject;
}

what are some caches that are responsible for fetching the data on miss?

The book 'architecture of open source software' says that the most common type of global cache in a web application is responsible for fetching the data itself, in case it is missing, as shown on this fixure. This seems different than what I've encountered so far. Most applications I have encountered make the application server responsible for fetching data from the db, and updating the server. At first, I thought the book might be talking about caching proxies, like Varnish, but they cover those in the next section, so that doesn't seem to be the case.
What cache systems actually fetch the data in case of a miss, and how do they know how to interact with the database?
Caching solutions provide read-through/write behind features which enable users to configure a read-through/write-behind provider be implementing some interface and deploying it with cache server. These providers contain logic about how cache server can interact with database to load/save data in database.
On a cache fetch operation if data is not present in cache server, cache loads data from database using configured provider thus avoiding a cache miss.
This way client applications deal cache as only data source and cache itself is responsible for interactions with database. You can read further details in this article by Iqbal Khan.
NCache and TayzGrid are enterprise solutions among many others that provide this feature.

How can couchbase be used as a caching layer on top of oracle?

I have Oracle as my main RDBMS for read and write, but I want to use couchbase as caching layer as it has map-reduce as can be used as memcache. Any idea as to how i can implement that, and how to transfer and update data in the caching layer, when Oracle is updated or inserted etc.
You are not telling anything about your current performance issues.
I have seen too many applications which do not really take advantage of RDBMS/SQL features, especially if an ORM sits in between.
The cure is to put another cache on top of a database, and to synchronize this in a cluster manually using IP multicasts (SwarmCache for example), message queues (JMS) or nightly import jobs. It could create more problems in the end. And it increases system complexity.
So my answer to your question is: I would not do it, as long as there is room for improvement regarding your data model and/or queries.
I believe your question is about Database synchronization. This can be done through a combination of using DB dependencies and "right-thru" features that I am not too sure about whether couchbase offers. So with DB dependency you have cached items dependent upon Db items and if the DB items are updated or deleted the corresponding dependent item in the cache is removed and at the same time you can write a "right-thru" handler executed at the server level; and the main purpose of this handler is loading fresh copies of the removed items in the cache. So, basically, you'll write the handler once and registerit with the cache server and the cache server will execute it when needed to sync. new items in the DB with the cache. This reading on Db synchronization can be useful . Its based on a product Ncache.
So your question is not directly related to Couchbase, but as other stated more about how you can be alerted when data are changing into your Oracle instance.
One thing that is not well known is the Oracle Database Change Notification feature that is quite cool for this:
http://docs.oracle.com/cd/E11882_01/java.112/e16548/dbchgnf.htm
So you can create an application that is listening to your changes and pushes the data into Couchbase.

Updating cache on update of database table

I am using AppFabric to handle the caching capabilities of my website. I would like the
products cache to be updated once there has been a change within the products table
within my database. I read about implementing a read-through but from reading about I
found:
Read: Called when a cache client requests a cached item that does not currently exist in the associated cache.
This doesn't seem like it will solve my problem as I'd like the products cache to be
updated once a change to the table has been discovered, it won't necessarily not exist
in the cache in the first place. Is there any way I can do this using AppFabric
capabilities?
Basically, you just want to have a SqlCacheDependency mechanism. In the past, it was very usefull for In Memory Cache.
However, there is no support within AppFabric caching for the SqlCacheDependency mechanism (or in fact for any kind of dependency). You have to build your own ...
In addition, With a read-through provider, the cache detects the missing item and calls the provider to perform the data load. If your add your item with an expiration of 30 minutes, the provider will never not try to reload it before its expiration.
Eventually, you can try to update the cache with a Write-Behind provider. It will update periodically you database.

Resources