Using the Redis NoSql database with .NET Part 1: introduction and setup


Traditional relational databases have received a couple of strong competitors in recent years. According to the db-engines website the 10 most widely used data stores are still dominated by relational databases and it will probably be the case for long years to come. However, there are many alternatives out there that can take their place in an application as its backing store. They can either act as the main data store of the application, i.e. where all the records are persisted for later retrieval. Alternatively they serve a different but very specific storage role that is not well suited for relational databases. In other words the application is backed up by e.g. SQL Server or MongoDb as its main data storage but delegates other storage needs, such as caching to say Memcached.

NoSql data stores

These alternative data stores are often called NoSql databases since they don’t require SQL for queries. The most prominent and widely used NoSql database nowadays is the document based MongoDb which we explored in two different series here and here and which is queried using JavaScript and JSON.

Document databases form one of many groups within the NoSql world. Apart from MongoDb there’s Amazon DynamoDb, which we explored here, CouchDb, RavenDb and others. Then there’s another group in the NoSql category called key-value stores that store their data by keys. Altogether they are a very different beast compared to document stores.

Document datastores store their records in documents that are indexed for efficient queries. Key-value databases on the other hand store their records by unique kwys. In that sense they behave like dictionaries in .NET or maps in Java where a single unique key holds a value which can be of any type that .NET/Java supports: integers, strings, lists, custom objects etc. Key-value data stores also have their data types which can be used as the value of a given key.

So working with document and key-value stores requires a very different way of thinking and an open mindset if you’re a staunch believer and user of relational databases. Forget things like tables, schemas, primary and secondary keys, SQL statements and many other elements you take for granted. You may think you’ll never use anything other than Oracle/MySql/Sql Server/etc. for your data storage solution but it’s still beneficial to know that there are other tools that can serve as a full database or help you solve specific needs. Also, you can have have a mixed data storage strategy where you store some objects in a relational DBMS and some others in a document store depending on the objects’ nature and the advantages and disadvantages of each storage mechanism. The mindset of “we’ve always done it this way” is a dangerous one that hinders progress at work. Studying document and key-value stores can widen your skills set and offer a different point of view to solving data storage challenges.


Redis is currently the most popular key-value store at the time of writing this post. It is an open-source data structure store which stores its data in memory by default. That’s right, it’s a database whose primary data storage is the memory of the server it is installed on. Don’t worry, it also offers persistent storage so the data is not lost if the server is rebooted. Since the records are stored in memory Redis provides extremely fast read and write operations. Therefore Redis is a good candidate for distributed caching. Other features of Redis include:

  • 5 data types are supported for values: strings, sets, sorted sets, lists and hashes
  • Key-values with a time-to-live attached
  • Clusters with data replication to build a high-availability environment
  • Transactions
  • A publish-subscribe messaging model
  • Scripting through the Lua programming language

You can find out more about the features and the details on the Redis intro page here.

In this series we’ll first look at the Redis server and client as standalone applications so that we get used to the Redis commands. We’ll then see how to use it with .NET at a later stage.

Installation on Windows

Note that Redis is normally installed on Linux. However, since this series is aimed at .NET developers, chances are that most readers will be more familiar with Windows – including myself of course. There’s a Windows port for 64-bit systems maintained by Microsoft and it is stored on GitHub here. Scroll down to the Readme file and the first section is called “Redis on Windows”. You’ll see that one way to install Redis is through NuGet. We’ll go for that option since it will download the latest signed version. It will also download the necessary exe files for starting the Redis server and communicating with it through a command line client.

There are other versions available through the releases page here. You’ll be able to download an MSI installer and a ZIP file from there. The ZIP file will contain the binaries. You can download Redis for Windows that way if you want but here we’ll continue with the NuGet way.

So create a new console application in Visual Studio, give it some name like RedisConsoleTests and open the package manager console: View, Other Windows, Package Manager Console. Run the following command:

Install-Package Redis-64

When the installation has finished go to the folder where you saved the console application and open the packages folder. It should contain a sub-folder called redis-64.[the version number]. The most current signed version number at the time of writing this post is 3.0.503:

Redis subfolder in .NET console application

Open that folder, then open the tools sub-folder. The tools folder will contain a lot of documentation for Windows including how to install Redis as a Windows Service. Let’s move all these files to another location. I’ve created a folder called Redis on my C drive and copied all the files from the NuGet package there:

Redis executables copied to another location on C drive

The redis-server.exe file will start the server. Go ahead and double click it. If everything went fine then you should see the Redis server console window up and running:

Redis server up and running on Windows

You’ll see that the Redis server is listening on port 6379 by default.

The redis-cli.exe file offers a tool to interact with the server using Redis commands. Double-click on that file and it will open another command prompt connecting to port 6379 on localhost:

Redis client connecting to Redis server on Windows

The two most common commands in Redis are GET and SET which get and set a certain key and its value. They are vaguely like INSERT and SELECT statements in SQL. Issue the following command in the client:

SET username “jack.nicholson”

This will set a key called “username” to the string value “jack.nicholson”.

GET will retrieve the value for a key:

GET username

…which will return jack.nicholson on the client. If we try to retrieve a non-existent key, like “GET blah” then the client will respond with “(nil)”, meaning a null.

The command to delete a key-value is DEL:

DEL username

Great, that’s enough for now. We’ll continue with a lot more fun in the next post.

You can view all posts related to data storage on this blog here.


About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: