Alachisoft.com

Contact Us

+1 (925) 236-3830 sales@alachisoft.com

support@alachisoft.com

Live Chat

Synchronize In-Memory Data Grid with Database

TayzGrid is an extremely fast and scalable in-memory data grid that lets you improve your application performance and scalability. Unlike a relational database that quickly becomes a bottleneck, TayzGrid scales linearly by letting you add more data grid servers to the data grid cluster in order to increase transaction capacity.

But when you store application data in the data grid, you are creating a copy of the data in the data grid that also exists in your relational database. And, if this data in the database changes, you want to make sure the data grid is also updated so it is always consistent with the database.

TayzGrid provides powerful database synchronization features where the data grid updates itself when data changes in the relational database. Each feature is geared toward a specific type of usage and collectively they handle a variety of scenarios for synchronization with the database. These features are:

  • OracleDependency: This allows you to synchronize the data grid with Oracle with the help of Oracle Notifications.
  • DbDependency: This allows you to synchronize the data grid with any OLEDB compliant database including DB2 through polling.
  • Read-through handler for auto reload: This features allows TayzGrid to automatically reload a fresh copy of the item in the data grid instead of removing it when data in the database changes.

Using OracleDependency

OracleDependency is used by TayzGrid in case your database is Oracle 10g or later and running on either Windows or Unix.

You can specify an OracleDependency for any item in the data grid when you’re adding or updating it in the data grid. And, this OracleDependency is a SQL statement or a stored procedure call corresponding to a dataset in the Oracle database. Oracle database server then monitors this dataset for any additions, updates, or removals and when that happens, Oracle server notifies TayzGrid in-memory data grid cluster. TayzGrid then either removes this item from the data grid or reloads a fresh copy of it from the database.

OracleDependency notifies TayzGrid through Oracle Notifications almost immediately after database update. And, these notifications are using Oracle’s client/server communication underneath and then talking to TayzGrid in-memory data grid cluster.

Here is how you can use OracleDependency in your code.


CacheDependency oracleDependency = new OracleCacheDependency(connectionString,
                                   "SELECT PRODUCTID, rowId,
                                   PRODUCTNAME FROM PRODUCTS WHERE PRODUCTID = 1");
CacheItem cacheItem = new CacheItem("OracleDependentValue");
cacheItem.setDependency(oracleDependency);            
_cache.add("Item:1", cacheItem);

Using DbDependency

DbDependency is used by TayzGrid in case your database is not Oracle but is some other OLEDB compliant database.  You can also use DbDependency with Oracle if you do not want the event notifications which could get chatty in case your data is changing very rapidly.

In DbDependency, you are asked to create a special table in the database called TayzGrid_db_sync which contains one row for each item in the data grid with DbDependency. And, you have to modify your database triggers to update rows in this table when the corresponding data in the database changes. Then, TayzGrid polls this TayzGrid_db_sync table looking for rows that were updated. And, in one poll, TayzGrid can fetch thousands of rows and then synchronize them with the database. Synchronization here means removing the corresponding item from the data grid.

Here is how you can use DbDependency in your code.


CacheDependency dependency = DBDependencyFactory.CreateOleDbCacheDependency(
                                 connectionString,
                                 Integer.toString(i) + ":oldRow.Products");
_cache.insert(i + ":oldRow.Products", i, dependency, Cache.NoAbsoluteExpiration,
              Cache.NoSlidingExpiration, CacheItemPriority.Default);

Using Read Through Handler for Auto Reload

The default behavior of database synchronization is to remove the corresponding data grid item from the database when the corresponding data in the database changes. However, there are situations where you simply want to update it with the latest version of the data.

To handle this need, TayzGrid allows you to combine database synchronization (OracleDependency, or DbDependency) with Read-through hander feature of TayzGrid. With this, TayzGrid simply calls the Read-through handler to reload the latest copy of the data grid item and then updates the data grid with it.


What to Do Next?