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

Valkey 8.0 Cluster Topology Routing, Hash Tagging, and Threaded I/O Guardrails

Enforces strict hash tag partition alignment ({tag}) for atomic Lua scripts and multi-key operations in Valkey 8.0 clusters, alongside deterministic cluster redirect handling (MOVED/ASK) with exponential backoff. Precludes CROSSSLOT exceptions, uncoordinated failover reconnect loops, and unbuffered pipeline degradation in threaded I/O environments.

Prohibited Anti-Pattern: Executing un-tagged multi-key Lua scripts and naive reconnect handling during Valkey cluster failovers.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Valkey 8.0 Cluster Topology Routing, Hash Tagging, and Threaded I/O Guardrails - Enforces strict hash tag partition alignment ({tag}) for atomic Lua scripts and multi-key operations in Valkey 8.0 clusters, alongside deterministic cluster redirect handling (MOVED/ASK) with exponential backoff. Precludes CROSSSLOT exceptions, uncoordinated failover reconnect loops, and unbuffered pipeline degradation in threaded I/O environments.
globs: src/**/*.ts, src/**/*.js, lib/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/valkey-8-cluster-threaded-io-failover"
---

# Valkey 8.0 Cluster Topology Routing, Hash Tagging, and Threaded I/O Guardrails

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Executing un-tagged multi-key Lua scripts and naive reconnect handling during Valkey cluster failovers.

## System Constraints
You are a Principal Engineer enforcing Valkey 8.0 Threaded I/O & Cluster Failover standards. Mandate strict hash slot isolation with hash tags for all atomic operations and robust cluster redirect handling.

## Strict Guidelines
- Engineers MUST ALWAYS wrap multi-key parameters and Lua script keys in hash tags (e.g., `{tenant:123}:session`) to guarantee co-location in the same cluster slot (0-16383).
- Engineers MUST NOT execute multi-key commands (MGET, MSET, EVAL, FCALL) across distinct hash slots without client-side scatter-gather or explicit key partitioning.
- Code SHOULD configure Valkey cluster client drivers with dynamic topology refresh intervals and bounded exponential backoff to handle MOVED and ASK redirects during node failovers.
- Services MUST enable offline queuing during failover slot migration windows rather than immediately dropping in-flight pipeline commands.
- Components NEVER assume multi-threaded I/O alters command execution atomicity; Valkey 8.0 executes commands sequentially in the main thread while parallelizing socket read/write operations.

## Bad vs Good Implementations
### Valkey Cluster Multi-Key Lua Execution & Failover Configuration

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The script uses separate key prefixes (`rate:...` and `profile:...`) without hash tags, causing Valkey Cluster to route them to different hash slots and throw a runtime CROSSSLOT error. Additionally, the cluster client lacks failover backoff, slot refresh strategies, and connection throttling during failovers.
```typescript
import { Cluster } from 'ioredis';

// ANTI-PATTERN: Default naive cluster config without failover handling
const valkey = new Cluster(['10.0.0.1:6379', '10.0.0.2:6379']);

export async function updateRateLimit(userId: string, action: string) {
  // FAILS in Valkey Cluster: keys map to disparate slots -> CROSSSLOT Keys error
  const script = `
    local current = redis.call('INCR', KEYS[1])
    if current == 1 then
      redis.call('EXPIRE', KEYS[1], ARGV[1])
    end
    redis.call('HSET', KEYS[2], 'lastAction', ARGV[2])
    return current
  `;
  
  const key1 = `rate:${userId}:${action}`;
  const key2 = `profile:${userId}`;
  
  return await valkey.eval(script, 2, key1, key2, 60, Date.now());
}
```

#### Production Standard (Correct)
> **Standard**: Encapsulating the routing identity within `{user:${userId}}` guarantees both keys map to identical hash slots, preserving atomic Lua execution. The cluster client is hardened with exponential backoff, auto-pipelining for Valkey 8.0 threaded I/O, and proactive slot refresh during cluster node failover.
```typescript
import { Cluster } from 'ioredis';

// PRODUCTION FIX: Configured for Valkey 8.0 cluster failovers and threaded I/O pipelines
const valkey = new Cluster(
  [{ host: '10.0.0.1', port: 6379 }, { host: '10.0.0.2', port: 6379 }],
  {
    enableOfflineQueue: true,
    slotsRefreshTimeout: 5000,
    retryDelayOnFailover: 100,
    maxRedirections: 5,
    clusterRetryStrategy: (times) => {
      const delay = Math.min(100 * Math.pow(2, times), 2000);
      return delay + Math.floor(Math.random() * 50); // Jittered backoff
    },
    redisOptions: {
      enableAutoPipelining: true // Optimal for Valkey 8.0 threaded I/O
    }
  }
);

export async function updateRateLimit(userId: string, action: string): Promise<number> {
  // Invariant: Keys bound to identical hash slot via explicit '{tag}'
  const script = `
    local current = redis.call('INCR', KEYS[1])
    if current == 1 then
      redis.call('EXPIRE', KEYS[1], ARGV[1])
    end
    redis.call('HSET', KEYS[2], 'lastAction', ARGV[2])
    return current
  `;
  
  const slotTag = `{user:${userId}}`;
  const key1 = `${slotTag}:rate:${action}`;
  const key2 = `${slotTag}:profile`;
  
  return (await valkey.eval(script, 2, key1, key2, 60, Date.now().toString())) 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 Cluster Topology Routing, Hash Tagging, and Threaded I/O Guardrails

Valkey 8.0 Cluster Topology Routing, Hash Tagging, and Threaded I/O Guardrails defines an industrial-grade behavioral standard for backend engineering. Enforces strict hash tag partition alignment ({tag}) for atomic Lua scripts and multi-key operations in Valkey 8.0 clusters, alongside deterministic cluster redirect handling (MOVED/ASK) with exponential backoff. Precludes CROSSSLOT exceptions, uncoordinated failover reconnect loops, and unbuffered pipeline degradation in threaded I/O environments. 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: Executing un-tagged multi-key Lua scripts and naive reconnect handling during Valkey cluster failovers. 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 DriftExecuting un-tagged multi-key Lua scripts and naive reconnect handling during Valkey cluster failovers.Critical
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 wrap multi-key parameters and Lua script keys in hash tags (e.g., `{tenant

Engineers MUST ALWAYS wrap multi-key parameters and Lua script keys in hash tags (e.g., `{tenant:123}:session`) to guarantee co-location in the same cluster slot (0-16383).

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 multi-key commands (MGET, MSET, EVAL, FCALL) across distinct hash slots without client-side scatter-gather or explicit key partitioning.

Engineers MUST NOT execute multi-key commands (MGET, MSET, EVAL, FCALL) across distinct hash slots without client-side scatter-gather or explicit key partitioning.

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 configure Valkey cluster client drivers with dynamic topology refresh intervals and bounded exponential backoff to handle MOVED and ASK redirects during node failovers.

Code SHOULD configure Valkey cluster client drivers with dynamic topology refresh intervals and bounded exponential backoff to handle MOVED and ASK redirects during node failovers.

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 enable offline queuing during failover slot migration windows rather than immediately dropping in-flight pipeline commands.

Services MUST enable offline queuing during failover slot migration windows rather than immediately dropping in-flight pipeline commands.

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 assume multi-threaded I/O alters command execution atomicity

Components NEVER assume multi-threaded I/O alters command execution atomicity; Valkey 8.0 executes commands sequentially in the main thread while parallelizing socket read/write operations.

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 Cluster Topology Routing, Hash Tagging, and Threaded I/O Guardrails critical for AI coding assistants?

Enforces strict hash tag partition alignment ({tag}) for atomic Lua scripts and multi-key operations in Valkey 8.0 clusters, alongside deterministic cluster redirect handling (MOVED/ASK) with exponential backoff. Precludes CROSSSLOT exceptions, uncoordinated failover reconnect loops, and unbuffered pipeline degradation in threaded I/O environments. 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 (Executing un-tagged multi-key Lua scripts and naive reconnect handling during Valkey cluster failovers.) 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