0Pricing
C# Academy · Lesson

Connecting to Redis

Configure a Redis distributed cache.

Connecting to Redis is a free C# Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is Redis?

Redis is a fast in-memory key-value data store, widely used as a distributed cache. It runs as a separate server that multiple app instances connect to over the network.

Installing The Package

ASP.NET Core integrates with Redis through the Microsoft.Extensions.Caching.StackExchangeRedis package, which adapts the StackExchange.Redis client to IDistributedCache.

dotnet add package Microsoft.Extensions.Caching.StackExchangeRedis

Registering Redis With AddStackExchangeRedisCache

Call AddStackExchangeRedisCache in your service registration and provide the connection string. This wires Redis as the IDistributedCache implementation.

builder.Services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = "localhost:6379";
});

Using Configuration

Keep the connection string in appsettings.json and read it via IConfiguration so it differs per environment.

builder.Services.AddStackExchangeRedisCache(options =>
{
    options.Configuration =
        builder.Configuration.GetConnectionString("Redis");
});

The InstanceName Prefix

Set InstanceName to prefix every key Redis stores. This namespaces your app keys so multiple apps can share one Redis instance safely.

builder.Services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = "localhost:6379";
    options.InstanceName = "MyApp:";
});

Connection String Options

The StackExchange.Redis connection string supports many options: multiple endpoints, a password, SSL, and timeouts, all comma-separated.

options.Configuration =
    "redis-server:6380,password=secret,ssl=True,abortConnect=False";

Same Interface, Real Redis

Once registered, you keep using IDistributedCache exactly as before. The only change is the registration line, so your services need no edits to start hitting Redis.

public WeatherService(IDistributedCache cache) => _cache = cache;
// GetStringAsync / SetStringAsync now talk to Redis

Running Redis Locally

For development you can run Redis quickly with Docker, then point your connection string at it.

docker run -p 6379:6379 redis:7
# then Configuration = "localhost:6379"

Managed Redis Options

In production, managed services like Azure Cache for Redis or AWS ElastiCache handle scaling, persistence and failover. You typically connect with an SSL endpoint and access key.

options.Configuration =
    "myapp.redis.cache.windows.net:6380,password=KEY,ssl=True";

Handling Connection Resilience

Set abortConnect=False so the client keeps retrying if Redis is briefly unavailable at startup, rather than throwing immediately. Combine with Polly for call-level resilience.

Health Checks

Register a Redis health check so your app can report cache connectivity to orchestrators and load balancers.

builder.Services.AddHealthChecks()
    .AddRedis("localhost:6379");

Quick Check

Test connecting to Redis.

Recap

Redis is a fast external cache. Install Microsoft.Extensions.Caching.StackExchangeRedis and call AddStackExchangeRedisCache with a Configuration connection string, optionally an InstanceName prefix. Your services keep using IDistributedCache unchanged. Use config per environment, abortConnect=False for resilience, and add a Redis health check.

Frequently asked questions

Is the “Connecting to Redis” lesson free?

Yes — the full text of “Connecting to Redis” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Connecting to Redis”?

Configure a Redis distributed cache. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Connecting to Redis” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. IDistributedCache Abstraction
  2. Connecting to Redis
  3. Cache Patterns and Expiration
  4. Caching Serialized Objects
← Back to C# Academy