This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: valkey-8-threaded-io-cluster-topology-guardrail

Valkey 8.0 Threaded I/O, Slot Redirect Handling, and Hash Tag Atomicity Standard

Enforces architectural patterns for Valkey 8.0 cluster topologies, multi-threaded I/O management, and multi-key script execution. Eliminates CROSSSLOT execution failures, cascading failover loops, and thread pool starvation by mandating hash tags, dual-phase redirection handling (MOVED vs. ASK), and non-blocking transaction designs.

Prohibited Anti-Pattern: Unpartitioned Cross-Slot Multi-Key Operations with Naive Redirect Handling Under Multi-Threaded I/O

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Valkey 8.0 Threaded I/O, Slot Redirect Handling, and Hash Tag Atomicity Standard - Enforces architectural patterns for Valkey 8.0 cluster topologies, multi-threaded I/O management, and multi-key script execution. Eliminates CROSSSLOT execution failures, cascading failover loops, and thread pool starvation by mandating hash tags, dual-phase redirection handling (MOVED vs. ASK), and non-blocking transaction designs.
globs: src/**/*.{ts,js,py,go}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/valkey-8-threaded-io-cluster-topology-guardrail"
---

# Valkey 8.0 Threaded I/O, Slot Redirect Handling, and Hash Tag Atomicity Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unpartitioned Cross-Slot Multi-Key Operations with Naive Redirect Handling Under Multi-Threaded I/O

## System Constraints
You are a Principal Software Architect enforcing production-grade Valkey 8.0 standards. In Valkey 8.0, I/O threads handle socket reads/writes concurrently while command execution remains serialized on the main engine thread. You must enforce strict key-slot co-location via hash tags `{...}` for all multi-key operations and Lua/Functions scripts, mandate correct dynamic topology discovery via `CLUSTER SHARDS` (replacing deprecated `CLUSTER SLOTS`), and enforce exact redirect state machines separating `MOVED` (permanent slot map mutations) from `ASK` (transient per-command redirections requiring `ASKING`). Reject any code that introduces CROSSSLOT vulnerabilities, unbuffered pipeline thrashing, or long-running CPU-bound Lua scripts that starve the multi-threaded I/O event loops.

## Strict Guidelines
- Engineers MUST ALWAYS co-locate multi-key transaction keys and Lua script arguments to identical hash slots using explicit hash tags (e.g., `{tenant:1001}:user` and `{tenant:1001}:settings`).
- Engineers MUST NOT execute Lua scripts (`EVAL`, `EVALSHA`) or Valkey Functions (`FCALL`) spanning multiple un-hashed keys across cluster nodes, as Valkey terminates execution with `CROSSSLOT Keys in request don't hash to the same slot`.
- Code SHOULD handle `-MOVED` and `-ASK` cluster redirects distinctly: `-MOVED <slot> <ip:port>` MUST trigger an asynchronous cluster topology refresh and slot-map cache invalidation, whereas `-ASK <slot> <ip:port>` MUST send an `ASKING` command strictly targeted to the nominated node for that single invocation without updating the slot map.
- Services MUST configure cluster clients to refresh topology using `CLUSTER SHARDS` at a bounded interval (e.g., periodic interval with backoff and rate-limiting) to safely ingest failovers and slot migrations without thundering-herd reconnect storms.
- Components NEVER execute blocking operations, tight loops, or large data transformations inside Lua scripts or functions; doing so monopolizes the primary Valkey thread while threaded I/O buffers saturate, triggering client timeouts.

## Bad vs Good Implementations
### Cluster Slot Routing and Atomic Multi-Key Mutation

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The keys `user:${sourceUser}:bal` and `user:${targetUser}:bal` hash to different cluster slots (out of 16384 slots), triggering a fatal CROSSSLOT error at runtime in cluster mode. Additionally, the cluster client lacks failover retry strategies, causing sudden failovers to dump errors immediately to the application.
```typescript
import { Cluster } from 'ioredis';

const cluster = new Cluster(['10.0.0.1:6379']);

// ANTI-PATTERN: Multi-key Lua script with non-co-located keys.
// Causes CROSSSLOT exceptions in a clustered Valkey 8.0 deployment.
// Fails to handle slot migrations cleanly and risks thundering herd on failover.
export async function transferTokensBad(sourceUser: string, targetUser: string, amount: number) {
  const script = `
    local src = redis.call('GET', KEYS[1])
    if not src or tonumber(src) < tonumber(ARGV[1]) then
      return 0
    end
    redis.call('DECRBY', KEYS[1], ARGV[1])
    redis.call('INCRBY', KEYS[2], ARGV[1])
    return 1
  `;
  
  // Keys hash to independent slots across different shards
  return await cluster.eval(script, 2, `user:${sourceUser}:bal`, `user:${targetUser}:bal`, amount);
}
```

#### Production Standard (Correct)
> **Standard**: Enclosing the routing partition key inside curly braces `{tenant:${tenantId}}` forces Valkey's CRC16 hashing algorithm to compute the slot strictly based on the content within the braces. This guarantees both keys reside on the identical slot and node, preserving Lua script atomicity. Cluster options ensure dynamic reconnection and slot map invalidation during failovers.
```typescript
import { Cluster, ClusterOptions } from 'ioredis';

const clusterOptions: ClusterOptions = {
  // Ingest topology changes smoothly during Valkey 8.0 cluster failovers
  slotsRefreshTimeout: 2000,
  enableReadyCheck: true,
  scaleReads: 'master',
  retryDelayOnClusterDown: 300,
  retryDelayOnFailover: 100,
  // Valkey 8.0: Max retry hops before surfacing redirection errors
  maxRedirections: 16,
};

export const valkeyCluster = new Cluster([
  { host: '10.0.0.1', port: 6379 },
  { host: '10.0.0.2', port: 6379 }
], clusterOptions);

// PRODUCTION PATTERN: Enforce hash tags to guarantee same-slot hashing.
// Both keys resolve to the hash of 'tenant:42', guaranteeing single-shard routing.
export async function transferTenantTokens(
  tenantId: string,
  sourceUser: string,
  targetUser: string,
  amount: number
): Promise<number> {
  const script = `
    local srcBalance = redis.call('GET', KEYS[1])
    if not srcBalance or tonumber(srcBalance) < tonumber(ARGV[1]) then
      return 0
    end
    redis.call('DECRBY', KEYS[1], ARGV[1])
    redis.call('INCRBY', KEYS[2], ARGV[1])
    return 1
  `;

  // Using hash tag {tenantId} forces CRC16 calculation strictly on tenantId
  const sourceKey = `{tenant:${tenantId}}:user:${sourceUser}:balance`;
  const targetKey = `{tenant:${tenantId}}:user:${targetUser}:balance`;

  return (await valkeyCluster.eval(script, 2, sourceKey, targetKey, amount)) as number;
}
```

Recommended MCP Servers (mcp-serverlist.com)

Equip agent with system tools to verify code for this stack.

Recommended Agent Skills (askills.dev)

Ready-to-use executable skill functions tailored to this stack.

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Valkey 8.0 Threaded I/O, Slot Redirect Handling, and Hash Tag Atomicity Standard

Valkey 8.0 Threaded I/O, Slot Redirect Handling, and Hash Tag Atomicity Standard defines an industrial-grade behavioral standard for backend engineering. Enforces architectural patterns for Valkey 8.0 cluster topologies, multi-threaded I/O management, and multi-key script execution. Eliminates CROSSSLOT execution failures, cascading failover loops, and thread pool starvation by mandating hash tags, dual-phase redirection handling (MOVED vs. ASK), and non-blocking transaction designs. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Unpartitioned Cross-Slot Multi-Key Operations with Naive Redirect Handling Under Multi-Threaded I/O Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftUnpartitioned Cross-Slot Multi-Key Operations with Naive Redirect Handling Under Multi-Threaded I/OCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS co-locate multi-key transaction keys and Lua script arguments to identical hash slots using explicit hash tags (e.g., `{tenant

Engineers MUST ALWAYS co-locate multi-key transaction keys and Lua script arguments to identical hash slots using explicit hash tags (e.g., `{tenant:1001}:user` and `{tenant:1001}:settings`).

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT execute Lua scripts (`EVAL`, `EVALSHA`) or Valkey Functions (`FCALL`) spanning multiple un-hashed keys across cluster nodes, as Valkey terminates execution with `CROSSSLOT Keys in request don't hash to the same slot`.

Engineers MUST NOT execute Lua scripts (`EVAL`, `EVALSHA`) or Valkey Functions (`FCALL`) spanning multiple un-hashed keys across cluster nodes, as Valkey terminates execution with `CROSSSLOT Keys in request don't hash to the same slot`.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Code SHOULD handle `-MOVED` and `-ASK` cluster redirects distinctly

Code SHOULD handle `-MOVED` and `-ASK` cluster redirects distinctly: `-MOVED <slot> <ip:port>` MUST trigger an asynchronous cluster topology refresh and slot-map cache invalidation, whereas `-ASK <slot> <ip:port>` MUST send an `ASKING` command strictly targeted to the nominated node for that single invocation without updating the slot map.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Services MUST configure cluster clients to refresh topology using `CLUSTER SHARDS` at a bounded interval (e.g., periodic interval with backoff and rate-limiting) to safely ingest failovers and slot migrations without thundering-herd reconnect storms.

Services MUST configure cluster clients to refresh topology using `CLUSTER SHARDS` at a bounded interval (e.g., periodic interval with backoff and rate-limiting) to safely ingest failovers and slot migrations without thundering-herd reconnect storms.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER execute blocking operations, tight loops, or large data transformations inside Lua scripts or functions

Components NEVER execute blocking operations, tight loops, or large data transformations inside Lua scripts or functions; doing so monopolizes the primary Valkey thread while threaded I/O buffers saturate, triggering client timeouts.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Valkey 8.0 Threaded I/O, Slot Redirect Handling, and Hash Tag Atomicity Standard critical for AI coding assistants?

Enforces architectural patterns for Valkey 8.0 cluster topologies, multi-threaded I/O management, and multi-key script execution. Eliminates CROSSSLOT execution failures, cascading failover loops, and thread pool starvation by mandating hash tags, dual-phase redirection handling (MOVED vs. ASK), and non-blocking transaction designs. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Unpartitioned Cross-Slot Multi-Key Operations with Naive Redirect Handling Under Multi-Threaded I/O) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

1 Rule in Stack

Valkey