.NET and Apache Ignite: Testing Cache and SQL API features — Part I
Tags: dotnet, apache, csharp, programming
Published: 2021-09-11 15:33:15

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tpo5fwn662mfri7ao763.png)

Some non-functional requirements from our applications are about performance and scalability. Then, we start thinking about a lot of strategies: review architecture, refactor features and algorithms, apply some caching strategy, etc.

Last days, I started using [**_Apache Ignite_**](https://ignite.apache.org/) as a cache strategy for some applications. Apache Ignite is an open-source In-Memory Data Grid, distributed database, caching, and high-performance computing platform.

Yeah! That’s it! All this in one solution. :D

> If you want to know more about Apache Ignite, take a look at its [documentation](https://ignite.apache.org/docs/latest/). Beyond .NET, there are samples for other programming languages as well.

This article will be split into two parts. In this first one, I will present the basic steps to work with the **Cache** feature and check results from a small test with that. In the second part, let’s see how can we work with **SQL API** feature in .NET applications.

#### Machine settings

For this test, I used the machine settings below:

*   Windows 10
*   Intel(R) Core(TM) i5–4440
*   16 GB RAM
*   .NET 5
*   Apache Ignite 2.10.0
*   Docker version 20.10.6

#### Run docker container

Before implementing our cache repository, let’s run Apache Ignite with its official [**_Docker_**](https://www.docker.com/) container.

Then, run the following command to start the Apache Ignite container:

{% gist https://gist.github.com/luis-alexandre/ba1a8edefa1189416577fa7fa1db0d1c %}

We can persist the data that will be used in the sections below in memory or on disk. For this article, the is being persisted in memory.

After running the command above, you should see the screen below:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0et6v5wrrnyv316pv0j.png)

Check if there are error messages when the Apache Ignite container has been started. If everything is OK, let’s test its Cache feature in the sections below.

#### Basic Settings for .NET and Apache Ignite

The steps to use Apache Ignite are very simple. You can follow the items below to allow your application to connect with Apache Ignite.

*   Add NuGet package below:

**Install-Package Apache.Ignite** \-Version 2.10.0

*   Set up a new connection with **IgniteClientConfiguration** class**.** in this part you need to provide at least one Apache Ignite server:

{% gist https://gist.github.com/luis-alexandre/ac4b25f317ea00ea7d4d20c15ab3d70c %}

*   Start a new connection with **_Ignition.StartClient method_**. This method returns an interface called **_IIgniteClient_**, which contains methods to work with cache clusters:

{% gist https://gist.github.com/luis-alexandre/602e5730cd599ea7547d24a6a1ef4767 %}

*   With **_IIgniteClient.GetOrCreateCache,_** get or create a new cache repository to a specific object type. This method returns the **_ICacheClient_** interface, which contains methods to work with the cache repository.

{% gist https://gist.github.com/luis-alexandre/db806151cac7adf207fea5de78e406c2 %}

Done! Now your application can perform operations in a cache repository from Apache Ignite. I recommend checking the Apache Ignite documentation to know more about available methods.

#### Testing the Cache system

Now that we have an Apache Ignite cluster ready and the application set up appropriately, let’s test its cache feature.

For this test, two classes were created to be inserted on the Apache Ignite Cache: _OrderCacheEntity (Order)_ and _ItemCacheEntity (OrderItem)_.

{% gist https://gist.github.com/luis-alexandre/316a17813e66e06250b735456b54d27d %}

Basically, one Order can have one or more OrderItem associated with it. Then, the next step is to generate as many Order instances as possible, adding and getting them to the cache, and finally check the performance with some counters.

The code below inserts 1 million Orders in the cache repository and checks its performance for “**_ICacheClient.Put_**” and “**_ICacheClient.TryGET_**” methods. As an attempt to create a small **_“workload”_** against the Apache Ignite container, those operations are executed with Parallel Task.

{% gist https://gist.github.com/luis-alexandre/c24549c36019c0afc2079fc14c91305e %}

As we can see in the code above, some KPIs were created to check the performance of each operation against Apache Ignite Cache:

**_KPIs Operation Time:_**

*   **_INPUT time (AVG seconds)_**_:_ the average in seconds to add an item to the cache
*   **_OUTPUT time (AVG seconds):_** the average in seconds to get an item from the cache
*   **_INPUT time (MAX seconds):_** the maximum time in seconds to add an item to the cache.
*   **_OUTPUT time (MAX seconds):_** the maximum time in seconds to get an item from the cache.
*   **_INPUT time (MIN seconds):_** the minimum time in seconds to add an item to the cache.
*   **_OUTPUT time (MIN seconds):_** the minimum time in seconds to get an item from the cache.

**_KPIs Exceptions:_**

*   **_INPUT ERROR:_** number of exceptions created when the application was adding an item to the cache.
*   **_OUTPUT ERROR:_** number of exceptions created when the application was getting an item from the cache.

**_KPIs Summary_**:

*   **_ITEMS CREATED_**: how many items were created in the cache.
*   **_ITEMS RECEIVED_**: how many items were got from the cache.
*   **_ITEMS MISSING_**: how many items were not in the cache.

#### Results from Application Test

After running the application test, the metrics below were collected:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1p62c9isnc36b4rngmx.png)

In general, the Apache Ignite presented a nice performance adding and getting objects. The average time to add items in the cache was **0.016** seconds and **0.025** seconds to get those items from the cache. Also, there were no exceptions and lost items in this test.

Checking the Apache Ignite container, the memory consumption increase by about 390 MB, which represents about 130% more than its initial state:

*   _Container stats (initial state)_

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvdn7105slqripoq39mi.png)

*   _Container stats (after running application test)_

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ufvu6ayoa2hwlycmvu62.png)

For now, that’s all. In the second part, let's see how can we implement the SQL API feature in our .NET applications. From a development perspective, I think that this feature from Apache Ignite is the most interesting so far.

I hope that this can be helpful for you!