返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
数据库

RedisStack Deployment/Persistence/Security/Integration with C# Projects

Docker deployment of RedisStack? Huh, just a basic operation. ACL permissions? Just a standard feature in Redis 6.0. C# integration? Using the official NRedisStack package, it's just one line of code.

曦远 (DealiAxy)约 10 分钟
目录8 个章节

Preface

Redis is really great—fast speed, supports many data types, and most importantly, it can now be used for vector search.

This article documents the deployment and configuration process of the officially provided redis-stack.

About redis-stack

redis-stack installs a Redis server with additional database capabilities and the RedisInsight.

Redis provides two images:

  • redis/redis-stack contains both Redis Stack server and RedisInsight. This container is best for local development because you can use RedisInsight to visualize your data.
  • redis/redis-stack-server provides Redis Stack but excludes RedisInsight. This container is best for production deployment.

The difference is whether RedisInsight, the web management interface, is included.

Since I need to use the web management interface, this article chooses the redis-stack image.

Deployment

As usual, deploy using docker-compose.

Map out Redis's port 6379 and RedisInsight's port 8001.

version: '3.4'

services:
  redis:
    image: redis/redis-stack:latest
    container_name: redis-stack
    restart: always
    ports:
      - "6379:6379"
      - "8001:8001"
    volumes:
      - ./data:/data

networks:
  default:
    name: redis-stack

It is recommended to use Portainer for deploying and managing images.

Persistence here is achieved by mapping the dump.rdb file.

Security and ACL Commands

In versions of Redis prior to 6.0, logging into the Redis Server required only a password (assuming the requirepass configuration was set), without needing a username. Moreover, the password was stored in plaintext in the configuration file, offering limited security. Applications also used this same password to connect, granting them full permissions to manipulate data, which posed significant risks.

With the introduction of ACL (Access Control List) in Redis 6.0, these security concerns have been effectively addressed. It allows for the creation of different users with tailored permissions based on specific requirements.

Redis ACL is backward-compatible, meaning that by default, the user is default and uses the password configured with requirepass. If ACL functionality is not utilized, the behavior remains identical to older clients.

In RedisInsight, the Workbench interface provides a convenient way to execute Redis commands, and the command tutorial on the left side is incredibly cool!

A Few ACL Commands

ACL commands are straightforward, and they even have auto-completion in RedisInsight. Here's a brief introduction to a few of them:

  • acl help - Help
  • acl list - List all users
  • acl whoami - View the current user
  • acl setuser - Set a user; this command is also used for setting passwords as needed

Set Password

Assuming you want to set the password for the default user to abc123

ACL SETUSER default on >abc123 sanitize-payload ~* &* +@all

If you no longer want the password, you can clear it with the following command

ACL SETUSER default  on nopass sanitize-payload ~* &* +@all

Connect in the Program

Take C# as an example.

First, install the official nuget package.

dotnet add package NRedisStack

Demo code

using NRedisStack.RedisStackCommands;
using StackExchange.Redis;

var redis = await ConnectionMultiplexer.ConnectAsync("localhost", options => {
    options.User = "default";
    options.Password = "abc123";
});
var db = redis.GetDatabase();

var json = db.JSON();

// Write
json.Set("ext:1", "$", new { Age = 35, Name = "Alice" });

// Read
Console.WriteLine($"Read: {json.Get(key: "ext:1", path: "$", indent: "\t", newLine: "\n")}");

References

_EOF_

RedisStack Deployment/Persistence/Security/Integration with C# Projects

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…