Apache Ignite Read Through Database Caching

Introduction

In my previous post blog post about apache ignite, we learnt how to use a traditional RDBMS as persistent store with apache ignite. As part of that tutorial, we demonstrated write through caching mechanism using JCache API extension provided by ignite and also using sql.Β In this blog post, we will extend and build upon previous blog post. We will learn to implement apache ignite read through caching with RDBMS as persistent layer and ignite as caching layer.

Tech Stack

Database Table

As mentioned, we will extend the previous tutorial, hence we don’t need to create anything extra. Instead we’ll use the same tables here as well.

What are we implementing?

This project will have three parts to it –

  • Inserting to DB table through ignite cache using write-through mechanism (already implemented in previous blog)
  • Reading the already inserted records using Cache API (read-through). This will actually read the data off cache.
  • We’ll insert a record in db table manually without affecting cache. We’ll try to read this data using same cache API. However, as the data is not present in cache, the implementation will demonstrate a true read-through behavior. It’ll fetch the data from DB.

Modify the Model Class: Orders POJO

We’ll modify the POJO class Orders.java inside com.iteritory.ignite.model. We’ll add a new variable “List<OrderLines> ol” in this class and add the getter/setter method for this variable. We’ll also create a new constructor that uses it as an input argument. Post modification, the Orders.java file looks like –

Implement load (Long key) method inΒ OrdersCacheStoreAdapter.java

Previously we added code snippet in the write method as part of our write-through behavior demonstration. Now, we will implement the load method to demonstrate read-through behavior. This load method will be called each time you call cache.get(args) method and the data is not present in the cache. So, in our demonstration scenario, for the first two cache.get(Args) calls, orders will already be present in the cache; for the third call, it will not be present in the cache. So, it will make a call to the DB.

Post implementing the load method, the entire class looks –

Maven Dependency

Here we want to print the Order object as JSON, hence we will add few dependency for Jackson as well. Post modification, my pom.xml looks as –

ModifyΒ IgniteWriteThroughCache.java to implement read-through behavior

Here we’ll set the read through property in the main program. We’ll also write method for reading data. Post modification, the class looks like –

Insert an Order record in DB

In order to demonstrate that our implementation is indeed read-through, we’ll insert an order record using DBEaver (you can use sql client of your own choice) with order number 33. This record will not be in the cache initially as it was manually inserted in the DB. So, when we execute the program, for the first time, it will try to get it from cache; however, it won’t be available in cache. It will then try to fetch it from the DB and also store in the cache for anu subsequent call.

 

Execute the program

  • 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”]
  • This will create a jar file in theΒ targetΒ directory.
  • Important step:Β Copy the jar file created in above step to theΒ %IGNITE_HOME%/libsΒ folder. This is essential for our program to run.
  • 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.
  • Now run theΒ IgniteWriteThroughCache.javaΒ program. Upon successful execution, you will see execution result something similar to below on the eclipse console:-
  • Also check the ignite log in the terminal –

    ignite-console-log-read-through

    ignite-console-log-read-through

  • If you notice, the DB was hit only the first time cache.get((long) 33) was called. For the subsequent call to the same key does not hit DB. It hits the cache. So, in this case, when the data wasn’t available in the cache, it went to DB to fetch the data and then stored the same in cache as well. Hence, the next call to same key fetches the result from cache itself.

Conclusion

In this blog post, we implemented and also covered some concepts around read through cache mode. happy reading!! πŸ™‚

 

105

14 Responses

  1. Rob
    July 8, 2018
  2. Sadruddin Md
    July 9, 2018
  3. IGUser
    July 10, 2018
  4. IGUser
    July 10, 2018
  5. Sadruddin Md
    July 18, 2018
  6. Sadruddin Md
    July 18, 2018
  7. Nek
    July 27, 2018
  8. Nek
    July 27, 2018
  9. Ali
    July 28, 2018
  10. Sadruddin Md
    July 28, 2018
  11. Sadruddin Md
    July 28, 2018
  12. Ali
    July 28, 2018
  13. Ali
    July 28, 2018
  14. Sadruddin Md
    July 29, 2018

Write a response

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