Working With In-Memory Database - Redis

In this article we will see how to implement Redis in-memory database in C# .Net applications

Overview
 
An in-memory database is a database management system that primarily relies on main memory for computer data storage. In-memory databases are faster than disk-optimized databases because disk access is slower than memory access. Accessing data in memory eliminates seek time when querying the data, which provides faster and more predictable performance than a disk. There are many in-memory databases available for use with a different license. In this article we are going to discuss Redis which is one of the well-known and open source in-memory databases. Before starting with Redis first we will see some of the major advantages of using in-memory database.
 
Advantages of in-memory database
  • In-memory database helps in big data management
  • It allows real-time updates that enable businesses to gain access to their data as it happens.
  • It is used with applications that allow very fast data access, storage and manipulation.
  • In-memory database not only facilitates faster query response times, but at the same time, also reduces or totally eliminates the need of data indexing and storing pre-aggregated data in OLAP cubes or aggregate tables.
Redis
 
Redis is an open-source in-memory database project implementing a distributed, in-memory key-value store with optional durability. Redis supports different kinds of abstract data structures, such as strings, lists, maps, sets, sorted sets, hyperlog, bitmaps and spatial indexes. For more details please visit Redis In-Memory Database.
 
In order to use Redis in-memory database in our application we have to first install Redis database. You can download it from here Download latest version of Redis In-Memory Database. Once we are ready with installation let's create one simple console application to see how it works.
 
Step 1
 
Open Visual Studio -- here I am using Microsoft Visual Studio 2015 Community Edition. Click on File - New - Project. Here for demo purposes I am creating a console application.
 
 
Step 2
 
To connect with Redis we have to first download Redis Client from Nuget Package. In solution explorer right click on references and click on Manage NuGet Packages. It will open NuGet Package Manager, in that browse for ServiceStack.Redis.Complete and click on install.
 
 
 
Step 3
 
Now that we are ready with Redis environment let's do some code. Here we will create two static functions, one for inserting value into the Redis DB and the second one to read it from the DB.
 
To store data into the Redis database we are using the below code.
  1. /// <summary>  
  2. /// To Save Key Value Pair in Redis DB  
  3. /// </summary>  
  4. /// <param name="host">Redis Host Name</param>  
  5. /// <param name="key">Key as string</param>  
  6. /// <param name="value">Value as string</param>  
  7. /// <returns></returns>  
  8. private static bool Save(string host, string key, string value)  
  9. {  
  10.     using (var objRedisClient = new RedisClient(host))  
  11.     {  
  12.           if (objRedisClient.Get<string>(key) == null)  
  13.           {  
  14.                return objRedisClient.Set<String>(key, value);  
  15.           }  
  16.           else  
  17.           {  
  18.               return false;  
  19.           }  
  20.     }  
  21. }  
 To read details from Redis database:
  1. /// <summary>  
  2. /// To get value from Redis DB  
  3. /// </summary>  
  4. /// <param name="host">Redis Host Name</param>  
  5. /// <param name="key">Key as string</param>  
  6. /// <returns></returns>  
  7. private static string Get(string host, string key)  
  8. {  
  9.        using(var objRedisClient = new RedisClient(host))  
  10.        {  
  11.             return objRedisClient.Get<String>(key);  
  12.        }  
  13. }  
 We will test this by making a call to these two functions:
  1. static void Main(string[] args)  
  2. {  
  3.        Save("localhost""Key1""Value1");  
  4.        var result = Get("localhost""Key1");  
  5. }  
Here is the result when you debug this code
 
 
 
In this article we have seen basic implementation of Redis in-memory database. You can download sample application source code attached to this article. I hope you enjoyed reading this article. Keep reading and don't forget to post your valuable feedback.