Connecting to Redis with C#

Connecting to a Redis Server

Welcome to the first lesson of our Redis course! In this unit, we'll start with the very basics — connecting to a Redis server using C#. Understanding how to establish this connection is essential since it forms the foundation of all the operations you'll perform with Redis. By the end of this lesson, you'll be confident in setting up a connection to a Redis server and verifying that connection through simple operations.

What is Redis?

Redis, which stands for Remote Dictionary Server, is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. It supports a wide range of data structures such as strings, hashes, lists, sets, and sorted sets with range queries. Redis is known for its speed and efficiency as it keeps the entire dataset in memory, ensuring quick data access. Redis also provides features like persistence, data replication, and support for simple pub/sub functionalities, making it a versatile tool for developing high-performance applications.

What You'll Learn

In this lesson, you will learn how to:

  1. Connect to a Redis server using C#.
  2. Verify your connection by storing and retrieving a value.

Here's the simple code you'll be working with:

using StackExchange.Redis;
using System;

class Program
{
    static void Main(string[] args)
    {
        // Connect to the Redis server
        ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");

        // Get a Redis database instance
        IDatabase db = redis.GetDatabase();

        // Set a value in Redis
        db.StringSet("name", "Redis Learner");

        // Retrieve the value from Redis
        string value = db.StringGet("name");
        Console.WriteLine($"Stored string in Redis: {value}");

        // Dispose the connection when done
        redis.Dispose();
    }
}

// Output: Stored string in Redis: Redis Learner

Let's break down each part of the code:

  • Connecting to the Redis server:

    ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");

    This line establishes a connection to a Redis server running on localhost. The ConnectionMultiplexer class manages the connection to Redis, handling aspects like connection pooling and automatic reconnection.

  • Getting a database instance:

    IDatabase db = redis.GetDatabase();

    The GetDatabase() method returns an IDatabase interface that provides access to Redis commands. By default, it connects to database 0, but you can specify a different database number if needed.

  • Setting a value in Redis:

    db.StringSet("name", "Redis Learner");

    This command stores a string value in Redis using the key "name". The StringSet method is used for storing string values, which is one of the basic data types in Redis.

  • Retrieving the value from Redis:

    string value = db.StringGet("name");
    Console.WriteLine($"Stored string in Redis: {value}");

    The StringGet method retrieves the value associated with the key "name". If the key doesn't exist, it returns null.

  • Cleaning up:

    redis.Dispose();

    It's important to dispose of the ConnectionMultiplexer when you're done to free up resources properly.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal