Using the Redis NoSql database with .NET Part 2: the Redis Windows service and some basic string commands


In the previous post we introduced the topic of this series: Redis with .NET. We first went through some basic characteristics of Redis. It is a NoSql key-value store with in-memory storage and access by default. The records are stored by unique keys where each unique key has a value attached. It has many good features like fast data access, data replication, scripting, high-availability clusters, various data types and much more. Normally Redis is installed on Linux servers but we’ll go with the officially unsupported Windows 64-bit port maintained by Microsoft. That will fit most .NET developers out there who are the main target group of this series. We started the Redis server and client and tried three of the most basic Redis commands: GET, SET and DEL that vaguely correspond to SELECT, INSERT/UPDATE and DELETE statements in SQL.

In this post we will first install the Redis server as a Windows service and then test a couple of Redis commands related to strings.

The server process as a Windows service

Before we continue let’s install the Redis server as a Windows service so that we don’t need to start the redis-server.exe file every time we want to communicate with it. We mentioned in the previous post that Redis can be installed with an MSI package which will also set up the Redis server as a Windows service. We took a different route with the NuGet package but it’s still easy to install and start the service. We’ll follow the documentation in Windows Service Documentation.docx that’s included in the downloaded NuGet package.

Open a command prompt and navigate to the folder where you saved redis-server.exe, e.g. c:\Redis. Run the following command:

redis-server –service-install –loglevel verbose

This will install – but not start – a service called Redis which you can find in the Services list:

Redis available as a service after service install on Windows

Next run the following command:

redis-server –service-start

Note that this process may fail with no further hints like it did in my case. Reading through this thread on StackOverflow I found a hint saying that the Logs folder must be created first. So go ahead and create a folder named Logs in the Redis installation folder:

Create a Logs folder for Redis

Run the service-start command again and hopefully now the response will be similar to the following:

Redis service successfully started.

You can refresh the Services window and you’ll see that Redis is in Running status.

Let’s test this with the client. Double-click redis-cli.exe and run the following command:

SET name “elvis.presley”

…and then retrieve the name:

GET name

It should go fine. You can check the Logs folder. It will contain a text file called redis_log.txt with some log messages like “Accepted” and “DB 0: 1 keys (0 volatile) in 4 slots HT”. This file will be frequently updated by the Redis server process with the “verbose” logging level.


Strings are the most flexible data type supported in Redis. We can store pretty much anything using strings. If we want to store an object such as a customer we can serialise its properties into a JSON string. The Redis documentation provides examples for its commands and they can be filtered according to the data type they operate on:

Redis documentation listing the available commands by data type

Note that the filtered commands may not only operate on the selected data type. E.g. the filtered commands for strings include GET and SET which can be used for other data types. The string related commands are all quite easy to follow. Feel free to explore them on the referenced Redis documentation page. Here we’ll try some of them.

The following example shows how a customer with id 643 could be stored in Redis:

SET customer:643 “{id: 643, name: ‘Great customer LTD’, address: ‘Tokyo’}”

We can use the string data type for integers:

SET customer-count “1534”

We can then use INCR and DECR to increment or decrement that number by one:

INCR customer-count
DECR customer-count

INCRBY and DECRBY accept an integer for the increase or decrease:

INCRBY customer-count 23
DECRBY customer-count 65

Let’s add a greeting:

SET current-greeting “Hello world”

We can get the length of a value with the STRLEN command:

STRLEN current-greeting

…which returns 11.

We can append a new string to an existing one using the APPEND command:

APPEND current-greeting ” and goodbye”

…current-greeting will be “Hello world and goodbye”

GETRANGE helps us to retrieve a substring from and to an index:

GETRANGE current-greeting 1 6

..which gives “ello w”. If we provide an end index which is out of range like here…:

GETRANGE current-greeting 1 543

…then Redis won’t throw an ugly index out of bounds exception but instead kindly provide the maximum substring:

“ello world and goodbye”

An important universal command is EXISTS which checks for the existence of a key:

EXISTS current-greeting

It returns 0 for false and 1 for true.

MSET and MGET are used to set and get multiple keys. Here’s how to set multiple greetings:

MSET yesterdays-greeting “Goodbye” current-greeting “Hello” tomorrows-greeting “Seeyou”

…and here’s how to retrieve them at once:

MGET yesterdays-greeting current-greeting tomorrows-greeting

…which returns the values in the following format in the client CLI:

1) “Goodbye”
2) “Hello”
3) “Seeyou”

All right, that’s enough of strings for now. We’ll continue with some other data types of Redis 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: