# How Chat2DB's Redis Key Management Service Handles Key Operations

> Discover how Chat2DB's Redis key management service uses a six-layer architecture to efficiently manage your Redis keys. Learn about create, update, delete, query, and scan operations.

- Repository: [OtterMind/Chat2DB](https://github.com/OtterMind/Chat2DB)
- Tags: internals
- Published: 2026-07-26

---

**Chat2DB's Redis key management service implements a six-layer controller-converter-service-plugin architecture that processes create, update, delete, query, and scan operations through REST endpoints, domain converters, and Lua script executors.**

Chat2DB is an open-source database management tool that provides comprehensive Redis support through its dedicated key management service. This service abstracts complex Redis commands into a clean Java API, enabling developers to perform key operations via HTTP endpoints or direct service calls. The implementation follows a strict separation of concerns, moving from web controllers through domain converters to plugin-based Lua script execution.

## Architecture of the Chat2DB Redis Key Management Service

The service follows a **controller → converter → service → plugin** pattern that isolates web concerns from domain logic and Redis-specific implementation details.

### The Six-Layer Processing Pipeline

| Layer | Component | Responsibility |
|-------|-----------|----------------|
| **1** | **`DbRedisKeyController`** | REST entry point exposing HTTP endpoints at `/api/redis/*`. Receives request DTOs and delegates to the service layer. |
| **2** | **`RedisKeyConverter`** | Translates between external request/response models (`KeyCreateRequest`, `KeyUpdateRequest`) and internal domain objects (`RedisKey`, `KeyEntry`). |
| **3** | **`IDbRedisKeyService`** | Interface declaring the service contract: `create`, `update`, `delete`, `query`, `scan`, and `keyDetail`. |
| **4** | **`DbRedisKeyServiceImpl`** | Domain service implementation that builds command objects and forwards them to the plugin layer. |
| **5** | **`RedisKeyOperations`** | Plugin façade that encapsulates concrete Redis actions and calls the low-level executor. |
| **6** | **`RedisScriptExecutor`** | Executes actual Redis commands using Lua scripts via methods like `createRedisKey` and `scanRedisKey`. |

Source files for these components are located in `chat2db-community-server/chat2db-community-web`, `chat2db-community-domain`, and `chat2db-community-plugins` modules respectively.

## End-to-End Key Operation Flow

When creating a Redis key, the request traverses all six layers in a standardized workflow:

1. **HTTP Request** → `POST /api/redis/create` receives a `KeyCreateRequest` payload
2. **Controller** → `DbRedisKeyController` calls `redisKeyConverter.request2create(request)` to convert to a domain `RedisKey` object
3. **Service** → `DbRedisKeyServiceImpl.create` builds a `RedisKeyCreateCommand` and invokes `redisKeyOperations.create(command)`
4. **Plugin** → `RedisKeyOperations.create` transforms the command into a Lua script call via `RedisScriptExecutor.getInstance().createRedisKey(redisKey)`
5. **Execution** → `RedisScriptExecutor` runs the Lua script on the connected Redis instance and returns an `ExecuteResponse`
6. **Response** → The service wraps the result into a `KeyEntry` and the controller returns `DataResult<KeyEntry>` to the client

The same pattern applies to update, delete, query, and scan operations—each step swaps the specific command and executor method while maintaining the identical architectural flow.

## Core Components and Implementation Details

### REST Controller Layer

`DbRedisKeyController` serves as the entry point in [`chat2db-community-server/chat2db-community-web/src/main/java/ai/chat2db/community/web/api/controller/DbRedisKeyController.java`](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-web/src/main/java/ai/chat2db/community/web/api/controller/DbRedisKeyController.java). It exposes six primary endpoints:

- `POST /api/redis/create` – Create new keys
- `POST /api/redis/update` – Modify existing keys
- `POST /api/redis/delete` – Remove keys
- `POST /api/redis/query` – Search keys
- `POST /api/redis/keys` – Scan keyspace
- `POST /api/redis/key_detail` – Retrieve metadata

Each method receives a request DTO, delegates conversion, and calls the appropriate method on `IDbRedisKeyService`.

### Data Conversion Layer

Located at [`chat2db-community-server/chat2db-community-web/src/main/java/ai/chat2db/community/web/api/converter/redis/RedisKeyConverter.java`](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-web/src/main/java/ai/chat2db/community/web/api/converter/redis/RedisKeyConverter.java), the converter handles translation between web-facing DTOs and internal domain models. This keeps the controller thin and isolates transformation logic for requests like `KeyCreateRequest`, `KeyUpdateRequest`, and `KeyDeleteRequest`.

### Service Layer Interface

`IDbRedisKeyService` in [`chat2db-community-server/chat2db-community-domain/chat2db-community-domain-api/src/main/java/ai/chat2db/community/domain/api/service/db/IDbRedisKeyService.java`](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-api/src/main/java/ai/chat2db/community/domain/api/service/db/IDbRedisKeyService.java) declares the contract:

```java
KeyEntry create(RedisKey redisKey);
KeyEntry update(RedisKey oldKey, RedisKey newKey);
void delete(String key);
List<KeyEntry> query(String pattern);
List<KeyEntry> scan(String cursor, int count);
KeyEntry keyDetail(String key);

```

### Service Implementation

`DbRedisKeyServiceImpl` in [`chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/db/DbRedisKeyServiceImpl.java`](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-domain/chat2db-community-domain-core/src/main/java/ai/chat2db/community/domain/core/impl/db/DbRedisKeyServiceImpl.java) implements the interface. Each method constructs a command object (e.g., `RedisKeyCreateCommand`) and passes it to `RedisKeyOperations`.

### Plugin Facade and Script Execution

`RedisKeyOperations` in [`chat2db-community-server/chat2db-community-plugins/chat2db-community-redis/src/main/java/ai/chat2db/plugin/redis/RedisKeyOperations.java`](https://github.com/OtterMind/Chat2DB/blob/main/chat2db-community-server/chat2db-community-plugins/chat2db-community-redis/src/main/java/ai/chat2db/plugin/redis/RedisKeyOperations.java) acts as the plugin façade. It delegates to `RedisScriptExecutor` in the same module, which provides methods like `createRedisKey`, `updateRedisKey`, `deleteRedisKey`, `scanRedisKey`, and `getRedisKeyDetail` to execute Lua scripts against the Redis instance.

## Practical Usage Examples

### REST API Examples (cURL)

Create a string key with TTL:

```bash
curl -X POST http://localhost:10825/api/redis/create \
     -H "Content-Type: application/json" \
     -d '{
           "key": "myKey",
           "type": "STRING",
           "value": "hello",
           "expire": 3600
         }'

```

Update an existing key:

```bash
curl -X POST http://localhost:10825/api/redis/update \
     -H "Content-Type: application/json" \
     -d '{
           "oldRedisKey": {"key":"myKey"},
           "newRedisKey": {"key":"myKey","value":"world"}
         }'

```

Delete a key:

```bash
curl -X POST http://localhost:10825/api/redis/delete \
     -H "Content-Type: application/json" \
     -d '{"key":"myKey"}'

```

### Direct Java Service Integration

Autowire the service interface for programmatic access:

```java
@Autowired
private IDbRedisKeyService redisKeyService;

public KeyEntry createStringKey(String name, String value, long ttlSeconds) {
    RedisKey redisKey = RedisKey.builder()
                               .name(name)
                               .type("STRING")
                               .value(value)
                               .expire(ttlSeconds)
                               .build();
    return redisKeyService.create(redisKey);
}

```

Rename a key using the converter:

```java
public void renameKey(String oldName, String newName) {
    KeyUpdateRequest request = new KeyUpdateRequest();
    request.setOldRedisKey(new KeyEntry(oldName));
    request.setNewRedisKey(new KeyEntry(newName));
    redisKeyService.update(redisKeyConverter.request2update(request));
}

```

## Summary

- **Chat2DB's Redis key management service** uses a strict six-layer architecture separating web, domain, and plugin concerns
- **REST endpoints** in `DbRedisKeyController` handle HTTP requests at `/api/redis/*` routes
- **Domain conversion** occurs in `RedisKeyConverter`, translating between DTOs and `RedisKey` domain objects
- **Service implementation** in `DbRedisKeyServiceImpl` orchestrates command creation and delegates to the plugin layer
- **Lua script execution** via `RedisScriptExecutor` performs actual Redis operations atomically
- **Dual access patterns** support both REST API consumption and direct Java service injection for backend integration

## Frequently Asked Questions

### What operations does Chat2DB's Redis key management service support?

The service supports six core operations: **create**, **update**, **delete**, **query** (pattern matching), **scan** (cursor-based iteration), and **key detail** retrieval. Each operation follows the same layered architecture from controller through to Lua script execution.

### How does Chat2DB execute Redis commands internally?

According to the Chat2DB source code, commands execute through **`RedisScriptExecutor`**, which runs Lua scripts via methods like `createRedisKey`, `updateRedisKey`, and `deleteRedisKey`. This approach ensures atomic operations and consistent error handling across different Redis deployments.

### Can I use the Redis key management service programmatically without the REST API?

Yes. You can autowire the **`IDbRedisKeyService`** interface in your Spring components and call methods like `create(RedisKey)` or `update(oldKey, newKey)` directly. This bypasses the controller layer and is useful for backend integrations or custom automation scripts.

### Where is the Redis plugin logic located in the Chat2DB repository?

The Redis-specific implementation resides in the **`chat2db-community-plugins/chat2db-community-redis`** module. Key files include [`RedisKeyOperations.java`](https://github.com/OtterMind/Chat2DB/blob/main/RedisKeyOperations.java) for the plugin façade and [`RedisScriptExecutor.java`](https://github.com/OtterMind/Chat2DB/blob/main/RedisScriptExecutor.java) for command execution, both under `src/main/java/ai/chat2db/plugin/redis/`.