[C#] Redis introduction
Redis is an in-memory key/value database that runs as a service and can be accessed by network. In other words, it's like a online Dictionary (or HashMap) structure, lightweight and very fast, but unlike it's competitor memcached, it does have extra database features to make it more versatile.
Basic use case
The Redis company does not maintain any official C# library, but it does endorse StackExchange.Redis.
dotnet add package StackExchange.Redis
using StackExchange.Redis;
public class RedisRepository
{
// ConnectionMultiplexer should be used as a Singleton
// Recommended to inject as IConnectionMultiplexer and dispose when the application shuts down
private readonly static ConnectionMultiplexer Connection = ConnectionMultiplexer.Connect("localhost");
public async Task<int?> GetValueAsync(string key)
{
var db = Connection.GetDatabase();
var value = await db.StringGetAsync(key);
return (int?)value;
}
public async Task SetValueAsync(string key, int value)
{
var db = Connection.GetDatabase();
await db.StringSetAsync(key, value);
}
public async Task RemoveValueAsync(string key)
{
var db = Connection.GetDatabase();
await db.KeyDeleteAsync(key);
}
}
By default, all values are permanent until manually removed, but Redis implements an expiry parameter, so each value can have a specific time to live and be removed automatically after the set time.
// Keep the value for only 5 seconds
await db.StringSetAsync(key, value, expiry: TimeSpan.FromSeconds(5));
Redis does not implement sliding expiration natively, but it's possible to reset the expiry timer using KeyExpireAsync. Use it after every successful get operation to simulate the sliding feature.
// Reset the expiry to live for 5 seconds starting from now
await db.KeyExpireAsync(key, expiry: TimeSpan.FromSeconds(5));
About Redis
Redis can be installed on a local machine, but there's no native support for Windows. However, the company maintains an official docker image on Docker HUB and is supported by the biggest cloud platforms, so it's fairly easy to launch an instance on a cloud environment.
Redis is designed for performance, not security, so it should be used on trusted networks with trusted clients and avoid external access. Still, it does offer authentication and a few configuration options, it does care a lot about vulnerabilities and overall is pretty stable.
Redis persists data as key/value pairs and supports a few data types. Key accepts only string and byte[]. Value accepts most of the primitives (string, int, float, byte[], etc) and built-in has some complex types, like bitmaps, hashmaps, sets, geospatial data and others.
Some of the extra features Redis provides are master/slave data replication for high availability, data persistence strategies and snapshots so the Redis instance can recover from a crash with pre-populated data and batch command execution, which Redis call "transaction" but does not support rollbacks. It can also run Lua scripts, which can be used similarly as SQL's stored procedures.
When sharing a single Redis instance on a system with parallel processing, whether a multi-thread or multi-instance system, it is recommended to use a lock strategy to avoid redundant processes. This article has an example of a distributed lock implementation, appropriate for containerized or multi-instance applications.