What is an In-Memory Database?
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 different license. In this article we are going to discuss Rediswhich 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 forServiceStack.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.
///<summary>
///ToSaveKeyValuePairinRedisDB
///</summary>
///<paramname="host">RedisHostName</param>
///<paramname="key">Keyasstring</param>
///<paramname="value">Valueasstring</param>
///<returns></returns>
privatestaticboolSave(stringhost,stringkey,stringvalue)
{
using(varobjRedisClient=newRedisClient(host))
{
if(objRedisClient.Get<string>(key)==null)
{
returnobjRedisClient.Set<String>(key,value);
}
else
{
returnfalse;
}
}
}
///<summary>
///TogetvaluefromRedisDB
///</summary>
///<paramname="host">RedisHostName</param>
///<paramname="key">Keyasstring</param>
///<returns></returns>
privatestaticstringGet(stringhost,stringkey)
{
using(varobjRedisClient=newRedisClient(host))
{
returnobjRedisClient.Get<String>(key);
}
}
We will test this by making a call to these two functions:
staticvoidMain(string[]args)
{
Save("localhost","Key1","Value1");
varresult=Get("localhost","Key1");
}
Here is the result when you debug this code
Conclusion
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.