Redis Notes
Described as an 'in-memory data store' that can be used as a database, cache or message broker. It supports various types of persistent storage also - dumping data to a disk or writing transaction logs. On a Linux system, the data is written to /var/redis by default. The configuration files can be created in /etc/redis. An instance of Redis might also require an file in /etc/init.d.
Related components:
- Redis Sentinel
- Redis Cluster
Redis CLI
Check if the Redis server is running:
redis-cli ping
Start the Redis command line interface:
redis-cli
Set a row, or key-value:
set mykey somevalue
Read a value for a key:
get mykey
Write dataset and changes to persistent storage:
redis-cli save
redis-cli shutdown
Security
Redis has no authentication or access control by default.
- If deploying to an internal network, configure the firewall to block ports 6379 and 16379 for external IP addresses.
- Use bind to ensure Redis only listens on the specific port for that service.
- Use requirepass to apply authentication, so only authorised applications are able to query or write to the data. They will need to use the AUTH command for this.
- Use spiped to create an encrypted tunnel for Redis traffic.
Core Data Types
- Strings: The basic data type.
- Lists: A list is a list of strings, sorted in the order they were inserted into the data set.
- Sets: A set is an unordered collection of strings. This could be useful for applications that need to perform a lookup to determine whether a given value exists in a dataset.
- Sorted Sets: A Sorted Set is an ordered collection of strings.
- Hashes: A hash is stored as a key-value pair.
- Streams: This is an append-only data structure, used ideally for recording events in the order they were appended.
- Geospatial Index: For applications that require geospatial data.
- Bitmaps: Not sure why this is included, but it facilitates bitwise operations on strings, which are stored as byte sequences.
- Bitfields: These can be strings that are used as bit counters. Supported operations are get, set and increment.
Using Redis with .NET
Import assembly references:
using NRedisStack;
using NRedisStack.RedisStackCommands;
using StackExchange.Redis;
Instantiate Redis client:
ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("[server address]");
IDatabase db = redis.GetDatabase();
To instantiate a Redis client that requires authentication:
ConfigurationOptions options = new ConfigurationOptions
{
EndPoints = { { "[redis server address]", 6379 } },
User = "[user]",
Password = "[password]",
Ssl = true,
SslProtocols = System.Security.Authentication.SslProtocols.Tls12
};
options.CertificateSelection += delegate
{
return new X509Certificate2("[PFX file location]", "[password]");
};
ConnectionMultiplexer muxer = ConnectionMultiplexer.Connect(options);
IDatabase conn = muxer.GetDatabase();
The User and Password values should be read from appsettings/configuration file, rather than hard-coded.
Resources
Redis Site Redis @ GitHub Redis: Getting Started# Redis and .NET