Beginners Journey To Docker Redis Image In .NET

What is Redis ?

 
Redis stands for (RE)mote (DI)ctionary (S)erver. It is a caching server. It is a key-value based in-memory caching server. It is enterprise grade and can be used in many use cases such as database, message broker, session storage, etc. It is very popular for Key-Value storage. It supports a large number of data types such as string, list, sets,etc. We use a caching server to improve the application performance. It supports a good number of memory management techniques such as eviction policies and Time to Live (TTL) for Keys.
 
For more details refer to here.
 
It is available in the cloud. Cloud offerings are very much scalable. It is available in the form of the following variants:
  1. Azure Cache for Redis
  2. Amazon ElastiCache for Redis
  3. In GCP, Memorystore for Redis

Why Redis in Docker Image?

 
Docker is used in a Microservices based architecture.This Docker Image can be deployed in one go that will be used by other Microservices, we can use the docker image of Redis along with other Microservices. The deployment can be done using Docker composer or Kubernetes.
 
When developing/testing an application, docker image can get your redis server up and running with a few commands. If you wish to use the same in production scenarios you must understand the security section mentioned in here. Now, let’s get started with Hands-on from the next section.
 

Setting up Docker Image

 
Write the following commands below. This will pull the docker image from docker hub and expose the Redis server inside docker image available at 6379 to host on port number 1919. The name of the docker image we are creating is “redis-local”.
 
docker pull redis 
docker run --name redis-local -dp 1919:6379 redis
 

Connecting Redis through .NET Application

 
Now, create a .NET Application and install this nuget package “StackExchange.Redis”.
 
The code shown below will create the connection and set the key in Redis server. This code will be used to validate the connection if Redis Image is able to communicate with external services.
  1. ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("localhost:1919");  
  2. var db = connection.GetDatabase();  
  3. db.StringSet("AKey""AValue");   
Other common operations,
 
 var keyVal = db.StringGet("<KeyName>");
To Get Key
db.KeyExists("<KeyName>")
To Check if Key Exists
db.KeyExpire("<KeyName>",new TimeSpan(0,1,0));
Set Expiry for Key to 1 Min 0 Sec
 db.KeyDelete("<KeyName>")
To Delete Key
 
More available at here.
 

Validating the Key in Server

 
Now, switch back to cli and run the following commands. This will connect to the bash shell inside the docker image that is already running with the name “redis-local”. After this we will execute the command “redis-cli” to connect to redis server. Then, we will execute the “get <keyname>” command to get the redis key value.
 
docker exec -it redis-local /bin/bash 
redis-cli
get AKey
 
Output will be “AValue”
 
Screenshot for reference,
 
Now, we have verified that Docker image of Redis is working correctly. This covers the scope of this tutorial.
 
Beginners Journey To Docker Redis Image In .NET 
 
Other common commands,
 
set <KeyName> <KeyValue>
Used to set key in Redis Server
TTL <KeyName>
Used to get Time To Live of particular key
FLUSHALL
To Clean the Redis Server Data
Ping
To Check of server is responding to events
DEL <KeyName>
To Delete Key from Redis Server
 
More commands are available at here.
 

Summary

 
We have covered the following things,
  1. Setting up Redis Docker image in the local system.
  2.  Adding Redis Client package in .NET Application and sending test data.
  3. Verifying test data in Redis Server inside docker image.


Recommended Ebook

.NET Interview Questions and Answer: Practical Implementation

Download Now!
Similar Articles