Redis client for C# (.NET): StackExchange.Redis — connection and examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Redis client for C# (.NET): StackExchange.Redis



In modern development on the .NET platform, there is often a need to use high-performance caching systems and message brokers. One of the most popular solutions is Redis — an in-memory data store operating on the key-value principle. For interacting with Redis from C# (.NET), there is a proven and powerful library StackExchange.Redis. It was developed by Stack Exchange (the creators of Stack Overflow) and supports all major Redis features, including clustering, replication, and Pub/Sub.



What is StackExchange.Redis?



StackExchange.Redis is a high-performance, asynchronous client for Redis written in C#. The library is optimized for handling a large number of concurrent connections and supports multiplexing — a single connection can serve many parallel requests without blocking. This makes it an ideal choice for high-load applications on .NET Core, .NET 5/6/7/8, and .NET Framework.



Installing the library



Installing StackExchange.Redis is done via the NuGet Package Manager. The simplest way is to use the Package Manager Console in Visual Studio or the dotnet CLI.



dotnet add package StackExchange.Redis


Or via NuGet:



Install-Package StackExchange.Redis


After successful installation, a reference to the assembly appears in the project, and you can start working with Redis.



Connecting to a Redis server



The main class for interaction is ConnectionMultiplexer. It manages connections to Redis and automatically handles reconnections, network errors, and failover to replicas.



Example of creating a connection:



using StackExchange.Redis;

class Program{ static async Task Main(string[] args) { // Connection string to local Redis string connectionString = "localhost:6379"; // Creating the multiplexer using var redis = await ConnectionMultiplexer.ConnectAsync(connectionString); // Getting the database (default db=0) IDatabase db = redis.GetDatabase(); // Checking the connection bool isConnected = db.Ping().TotalSeconds < 2; Console.WriteLine($"Connected: {isConnected}"); }}


The connection string can include a password, port, SSL settings, and other options. For example, to connect to a Redis server remotely with a password:



"myredis.cloud.com:6380,password=secret123,ssl=true"


Basic data operations



After obtaining an instance of IDatabase, you can perform all standard Redis operations.



Working with strings



// Writing a stringawait db.StringSetAsync("user:1:name", "Alexey");

// Reading a stringstring name = await db.StringGetAsync("user:1:name");Console.WriteLine(name); // Outputs: Alexey

// Incrementing a counterawait db.StringIncrementAsync("page:visits", 1);


Working with hashes



// Setting hash fieldsHashEntry[] userData = {    new HashEntry("name", "Maria"),    new HashEntry("age", 28),    new HashEntry("city", "Moscow")};await db.HashSetAsync("user:2", userData);

// Getting a single fieldstring city = await db.HashGetAsync("user:2", "city");Console.WriteLine(city); // Moscow

// Getting all hash fieldsvar allFields = await db.HashGetAllAsync("user:2");foreach (var field in allFields){ Console.WriteLine($"{field.Name}: {field.Value}");}


Working with lists



// Adding elements to the list (from the left)await db.ListLeftPushAsync("notifications", "First notification");await db.ListLeftPushAsync("notifications", "Second notification");

// Getting all list elementsvar

Recommendations