How to Synchronize In-Memory Data Grid with Oracle Database?

Java applications are very popular in high traffic environments. Many web applications, web services and other server-side applications are developed in Java. And, these applications typically process a lot of data coming from a relational database.

But databases quickly become a scalability bottleneck as transaction load increases. And, in-memory data grids (IMDGs) are becoming increasingly popular for caching data in memory and reducing those expensive database trips so as to remove those bottlenecks. These in-memory data grids can easily scale out and do not become scalability bottleneck.

But, using an in-memory data grid means that you are storing data in the grid coming from the database and hence there are two copies of this data. This can cause data integrity problem where data in the database is updated but in-memory data grid still has the older version of the data. To avoid this issue, you have to synchronize your in-memory data grid data with the database to ensure that it is always correct.

This is an area that is typically neglected by most in-memory data grids. As a result, developers are hesitant to store a lot of transactional data in an IMDG because they’re afraid it’ll become stale. And, without caching transactional data, your Java application cannot truly take advantage of an in-memory data grid because most of the data in a typical Java application is transactional.

To handle this problem, TayzGrid provides database synchronization of the in-memory data grid with Oracle databases. This ensures that even if a third party application modifies data in the database, the in-memory data grid is always kept synchronized with the database.

To do this synchronization, TayzGrid becomes an Oracle database client and uses Oracle notifications to register interest with the database against a given data set. This data set is basically the data that corresponds to the in-memory data grid item. The database then triggers a notification whenever data in this data set changes. TayzGrid catches this notification and either invalidates the corresponding grid item or reloads it from the database by calling a Read-through handler.

TayzGrid encapsulates all that database synchronization capability inside an Oracle Cache Dependency feature. This way, you don’t have to implement anything to achieve this synchronization.

Oracle Cache Dependency is a great feature provided by TayzGrid to ensure that your data in an in-memory data grid is always kept synchronized with your database. All you need to do is enable change notifications on your database and after that add your data in TayzGrid with OracleCacheDependency having information of database connection string and query.

TayzGrid creates a connection with your database by providing a connection string and registering your query with the Oracle database. So when any change happens to the related data set of the registered query Oracle database triggers change notification to TayzGrid. Later on TayzGrid removes the stored item with OracleCacheDependency so that next time that item should be reloaded in TayzGrid with updated values.

Here are the simple steps to use OracleCacheDependency in your Java application:

1. Enable database change notifications to your user by executing this query. You just have to execute it once.

[SQL]
 "grant change notification to scott" 

2. Import the following jars in your application:

import com.alachisoft.TayzGrid.web.caching.*;
import com.alachisoft.TayzGrid.runtime.dependencies.*;

3. Now initialize your in-memory data grid, create OracleCacheDependency and add it with your item.

 Cache cache = TayzGrid.initializeCache("oracleDependencyCache");
try {
     //Adding cache item "Product:1002" with KeyDependency on "Product:1"
     cache.insert(key2, product, new KeyDependency(key1), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);

     //Removing/Deleting/Updating key: "Product:1001" will automatically make all of its dependant keys removed, here it's "Product:2"
     cache.delete(key1);

     if (cache.contains(key2)) {
     // Error: Product:2 must have been removed from the cache
     }
} catch (OperationFailedException ex) {
 // handle exception
}

You can use TayzGrid and try it yourself to see how easily TayzGrid solves your database synchronization issues.

Tweet

About Iqbal Khan

Iqbal Khan is a Technology Evangelist at Alachisoft (http://www.alachisoft.com). Iqbal received his MS in Computer Science from Indiana University, Bloomington, in 1990. You can reach him at iqbal@alachisoft.com.
This entry was posted in In-Memory Data Grid, OracleDependency and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


two + = 3

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>