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

Posted in In-Memory Data Grid, OracleDependency | Tagged , , | Leave a comment

How to Scale Java Applications with In-Memory Data Grid?

Java is very popular for developing high traffic enterprise level applications. These may be web applications developed in JSP, web services, or other server type applications. A wide range of these applications belong to Online Transaction Processing (OLTP) category involving millions of transactions.

You can handle this high transaction load by adding more web/app servers at the application tier but you cannot do the same at the database tier. The most you can do is buy more expensive hardware for your database server but that only takes you so far. Therefore, database always becomes a bottleneck and slows down your Java applications and may even grind it to halt if too much load is put on it.

Database being the bottleneck for web server farms

Database Scalability Problem in High Traffic Applications

Figure 1: Database Scalability Problem in High Traffic Applications

So, what to do about this? Well, in-memory data grid is becoming more popular for handling such situations. It lets you store application data in memory and reduce those expensive database trips that are overwhelming your database server. And, unlike a database, an in-memory data grid can scale by letting you add more servers at runtime as you increase your transaction load. This ensures that the in-memory data grid never becomes a scalability bottleneck.

An in-memory data grid also lets you persist you JSP Servlet sessions in it. These sessions are then replicated in a multi-server environment so you don’t lose any session data if a server ever goes down. And, an in-memory data grid is a much faster and more scalable session persistence store than your JSP Servlet container.

TayzGrid is an in-memory data grid for Java applications. It lets you store application data and also persist your JSP Servlet sessions in it. TayzGrid is extremely fast and scalable and provides linear scalability to you. Linear scalability means that as you add more grid servers in TayzGrid, your transaction capacity increases in a linear fashion.

Below is an example of using TayzGrid from Java Application:


 import com.alachisoft.TayzGrid.web.caching.Cache;
 import com.alachisoft. TayzGrid.runtime.*;
 ...
 public class MyCachingSample {
     private String url = "dbc:msql://20.200.20.1:1114/MyDatabase";
     public Employee GetEmployee(String empId) throws Exception
     {
          Employee emp = null;
          try {
               Cache cache = TayzGrid.initializeCache("MyDistributedCache");
                 emp = (Employee) cache.get(empId);
                 //If key not found in cache, Load from the database
                 if(emp == null)  {
                     ...
                         ResultSet rs = stmt.executeQuery("SELECT * FROM Employee WHERE EmpId ='"+ empId + "'");
                         if (rs.next()) {
                               emp = new Employee();
                               emp.EmpId = rs.getString("EmpId");
                               ...
                               //Cache key for future use
                               cache.insert(empId, emp, null, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default);
                         }
                 }
          }
          catch (Exception exp) {
            throw exp;
          }
          //return the required object
          return emp;
 }
 

TayzGrid provides a powerful API for the developers to use it as their in-memory data grid and powerful GUI based and command-line administration and monitoring tools for IT folks. This makes TayzGrid management very simple.
Please download a fully working copy of TayzGrid Enterprise and try it out for yourself.

Tweet

Posted in In-Memory Data Grid, Scaling Java Applications | Tagged , , | Leave a comment