Apache Ignite write through database caching

Introduction

In this tutorial, we will understand Apache Ignite write through database caching. First of all, let’s understand what write thorough cache is actually! Write-through caching ensures that data is updated to cache and the persistent store (disk, database etc) in same transaction. Therefore, if the transaction is successful then the change will be committed to both cache and persistent store. However, if it’s failed, the transaction will be rolled back and any change to database or cache is reverted to earlier state. There are pros and cons to this approach that can be covered in another post later.

In this blog post, we’ll implement and demonstrate an end-to-end solution for “write-through” cache. Following are used as part of this implementation –

Technology Stack

Create Database Tables

  1. To start with, we’ll create two tables in mysql database. The tables are self-explanatory. With each write-through insertion, we will create one order record with one or many order lines  –
  2. In order to create these tables and relationship constraint, let’s execute following statements –

JAVA program: Model Classes

  1. Next, we’ll start with our program that will insert records in database tables using write-through caching mechanism of apache ignite.
  2. In the previous blog post, we have already created a maven project. We’ll build this program in the same project.
  3. Right click on src/main/java and create a new package with the name com.iteritory.ignite.model. This package will hold the an Orders class that will in turn refer to OrderLines class. Orders and OrderLines calsses are exact mirror image of the underlying database.
  4. Right click on this newly created package and select New –> Class. Provide the class name as OrderLines.
  5. Content of OrderLines class is as below –

     
  6. We will now create Orders class with following content –

     

Maven Dependency

  1. In order to run this sample program, we need to resolve few dependency jars. We’ll use maven tool for that. Following is the pom.xml content used –

     

JAVA Program: CacheStoreAdapter / CacheStore

  1. Concept: –To implement write-through caching behavior, we’ll need to implement CacheStore interface and also set the CacheStoreFactory. Additionally, we will need to set writeThrough property of ignite CacheConfiguration. Apache ignite provides a convenience adapter in the form of CacheStoreAdapter that implement CacheStore interface. Here in this example, we will use CacheStoreAdapter to implement Write-Through behavior.
  2. Right click on src/main/java folder and create a new package com.iteritory.ignite.adapter. Create a class OrdersCacheStoreAdapter.java inside this package.
  3. Modify the class to extend CacheStoreAdapter<Key, Value>.  The class will look like after fixing the necessary imports -ckage com.iteritory.ignite.adapter;

     
  4. However, the class will still show errors as there are some unimplemented methods. To fix this, hover the mouse over OrdersCacheStoreAdapter; a pop-up will open; select “Add unimplemented methods”. This will auto-populate load, delete and write methods. The class will look like below –

     
  5. As part of this tutorial, we will implement write method only. In subsequent few blog posts, we’ll cover others.
  6. Concept:- Here we’ll use CacheStoreSession interface. CacheStoreSession interface is used to maintain the cache store operations. In this example, we’ll use JDBC. More on this concept can be found in the ignite official documentation. We’ll use @CacheStoreSessionResource annotation to inject CacheStoreSession.
  7. Next, we’ll create openConnection method responsible for creating connection to Database and returning the connection object. This method expects a Boolean value to set the connection’s auto commit mode.
  8. We’ll also create endConnection method that will close allocated connection resource (if any) depending on the status of ongoing transaction.
  9. We’ll create connection method that will be responsible for creating a new connection or reuse one depending on the current status.
  10. We’ll also then override the write method with our implementation. Here, we’ll use up-sert operation using prepared statement.
  11. We’ll implement sessionEnd method to either commit or rollback the transaction.
  12. Let’s check how the entire OrdersCacheStoreAdapter class look like after putting together all of the above –

     

JAVA Program: Implementation Class

  1. Now, we will create com.iteritory.ignite.impl package and inside that we’ll create a class IgniteWriteThroughCache.java – 

     
  2. Now, we’ll do a maven update [Right Click ignite-tutorial –> Maven –> Update Project …] and then will do a maven build [Right Click ignite-tutorial –>Run As –> Maven Build… ; set Goals as “clean install”]
  3. This will create a jar file in the target directory.
  4. Important step: Copy the jar file created in above step to the %IGNITE_HOME%/libs folder. This is essential for our program to run.

JAVA Project Structure

Apache Ignite Sample Program Project Structure

Apache Ignite Sample Program Project Structure

Execute the JAVA program

  1. Let’s start a single node apache ignite server by executing following command –>ignite.bat F:\apache-ignite-fabric-2.0.0-bin\config\itc-poc-config.xml from %IGNITE_HOME%/bin directory.
  2. If you are reading this blog afresh and haven’t gone through earlier posts, you can get the content of itc-poc-config.xml from this link; check step 7.
  3. Now run the IgniteWriteThroughCache.java program. Upon successful execution, you will see execution result something similar to below:

    I hope this blog post helps you with some key understanding, concepts of Apache Ignite along with how to implement these concepts using JAVA. Happy reading!!
135

13 Responses

  1. Naseer
    May 16, 2018
    • Sadruddin Md
      May 17, 2018
      • Naseer
        May 18, 2018
        • Sadruddin Md
          May 18, 2018
          • Naseer
            May 23, 2018
  2. Sadruddin Md
    May 23, 2018
    • Naseer
      May 23, 2018
  3. Sadruddin Md
    May 23, 2018
  4. Sadruddin Md
    May 23, 2018
    • Naseer
      May 23, 2018
  5. Nek
    July 24, 2018
  6. Anand
    November 6, 2018
  7. Dev
    March 24, 2020

Write a response

This site uses Akismet to reduce spam. Learn how your comment data is processed.