StackExchange.Redis for C# (.NET): Complete Guide with Examples
\n\n1. What It Is and Why You Need It
\n\nStackExchange.Redis is a high-performance, asynchronous client for Redis, developed by the StackOverflow team. It is the de facto standard library for working with Redis in the .NET ecosystem. It is designed for .NET Framework 4.6+ and .NET Core/.NET 5+.
\n\nWhy do you need it? Redis is an in-memory data store often used as a cache, message broker (Pub/Sub), task queue, or high-performance NoSQL database. StackExchange.Redis provides a convenient API for all these scenarios: managing strings, hashes, lists, sets, transactions, Lua scripts, streaming data, etc. Without this library, you would have to write low-level code for working with sockets and the Redis protocol (RESP).
\n\nThe key feature of the library is its asynchrony and multithreading. It uses ConnectionMultiplexer — a single, shared object for all connections to the Redis server. This allows efficient use of sockets and avoids creating multiple connections. The library fully supports Redis Cluster, Sentinel, and replication.
2. Installation
\n\nInstallation is done via NuGet Package Manager. Run one of the following commands in the Package Manager Console or in the .NET CLI terminal:
\n\n// Via .NET CLI\ndotnet add package StackExchange.Redis\n\n// Via Package Manager Console\nInstall-Package StackExchange.Redis\n\n\nAfter installation, add the using directive to your code:
\nusing StackExchange.Redis;\n\n\n3. Quick Start — Minimal Working Example
\n\nBelow is a minimal example that connects to a local Redis server, writes a string, and reads it back:
\n\nusing StackExchange.Redis;\n\n// Create a multiplexer (entry point for all operations)\n// Replace "localhost:6379" with your Redis server address\nConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost:6379");\n\n// Get the database (default db 0)\nIDatabase db = redis.GetDatabase();\n\n// Set a value\nbool setResult = db.StringSet("mykey", "Hello, Redis!");\nConsole.WriteLine($"Set result: {setResult}");\n\n// Get the value\nRedisValue value = db.StringGet("mykey");\nConsole.WriteLine($"Get result: {value}");\n\n// Close the connection (usually not required if using using)\nredis.Close();\n\n\nImportant: ConnectionMultiplexer is a long-lived object. It should be created once for the entire lifetime of the application (e.g., as a singleton in a DI container). Do not create a new multiplexer for each request!
4. Main Methods/Functions/Classes
\n\n4.1. ConnectionMultiplexer.Connect()
\nSignature: public static ConnectionMultiplexer Connect(string configuration)
What it does: Creates and returns a new instance of ConnectionMultiplexer, which manages all connections to Redis. Accepts a configuration string (address, port, password, timeouts, etc.).
Example:
\nvar redis = ConnectionMultiplexer.Connect("myredisserver.com:6379,password=secret,connectTimeout=5000");\n\n\n4.2. IDatabase.StringSet()
\nSignature: bool StringSet(RedisKey key, RedisValue value, TimeSpan? expiry = null, When when = When.Always)
What it does: Sets the value for the specified key. Can accept an optional expiry time and a condition (only if key exists, does not exist, etc.).
\nExample:
\n
Also in library
Recommendations