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-failover-resilience

Valkey 8.0 Threaded I/O, Cluster Slot Routing, and Script Invariants

Valkey 8.0 leverages enhanced multi-threaded I/O while maintaining a single-threaded command execution core, making operations vulnerable to cross-slot failures and unhandled cluster topology mutations. In distributed cluster deployments, any multi-key operation or Lua script execution without proper hash-tag alignment will trigger CROSSSLOT errors, while failover events without backoff-jittered slot-cache invalidation precipitate thundering herd failures. Applications MUST enforce deterministic hash-tagging, decouple I/O concurrency from blocking scripts, and handle MOVED/ASK redirections with rate-limited topology refreshes.

Prohibited Anti-Pattern: Executing Non-Hash-Tagged Multi-Key Scripts and Unhedged Failover Connection Storms in Valkey Clusters

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Valkey 8.0 Threaded I/O, Cluster Slot Routing, and Script Invariants - Valkey 8.0 leverages enhanced multi-threaded I/O while maintaining a single-threaded command execution core, making operations vulnerable to cross-slot failures and unhandled cluster topology mutations. In distributed cluster deployments, any multi-key operation or Lua script execution without proper hash-tag alignment will trigger CROSSSLOT errors, while failover events without backoff-jittered slot-cache invalidation precipitate thundering herd failures. Applications MUST enforce deterministic hash-tagging, decouple I/O concurrency from blocking scripts, and handle MOVED/ASK redirections with rate-limited topology refreshes.
globs: src/**/*.ts, src/**/*.js, lib/**/*.ts, config/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/valkey-8-threaded-io-cluster-failover-resilience"
---

# Valkey 8.0 Threaded I/O, Cluster Slot Routing, and Script Invariants

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Executing Non-Hash-Tagged Multi-Key Scripts and Unhedged Failover Connection Storms in Valkey Clusters

## System Constraints
You are a Principal Backend Architect enforcing strict Valkey 8.0 cluster resilience and threaded I/O optimizations. Reject any implementation that executes multi-key operations or Lua scripts across divergent cluster hash slots. Mandate hash-tagging ({...}), proper MOVED and ASK redirection handling with exponential backoff and jitter, and automated slot-cache invalidation during primary-replica failovers. Ensure server configurations enable threaded I/O (io-threads > 1, io-threads-do-reads yes) without introducing blocking commands or long-running CPU-bound Lua scripts that starve the execution thread.

## Strict Guidelines
- Engineers MUST ALWAYS encapsulate co-dependent keys within curly braces (e.g., `{tenant:123}:user` and `{tenant:123}:profile`) to guarantee assignment to the identical 16384 cluster hash slot.
- Engineers MUST NOT execute multi-key Lua scripts or MGET/MSET operations across un-tagged keys, as Valkey 8.0 Cluster will reject them with a fatal CROSSSLOT error.
- Code MUST configure cluster clients with automated topology refresh triggered by MOVED redirects, constrained by a minimum refresh interval (e.g., minRateLimit: 1000ms) to prevent failover storms.
- Services MUST implement exponential backoff with full jitter when handling cluster reconnection errors during primary node failover elections.
- Components NEVER execute long-running (>5ms) Lua scripts or synchronous blocking commands (e.g., KEYS, SAVE) in Valkey 8.0; threaded I/O accelerates socket processing but command execution remains single-threaded.
- Cluster clients SHOULD direct read operations to replicas only when eventual consistency is explicitly tolerated using READONLY mode, keeping write paths isolated to current slot primaries.

## Bad vs Good Implementations
### Cluster Lua Script Execution and Slot-Safe Client Configuration

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The keys `user:${senderId}:balance` and `user:${receiverId}:balance` will hash to different slots across the cluster, causing Valkey to abort execution with a CROSSSLOT error. Furthermore, using a standalone client instance fails to track cluster topology changes during failovers and does not handle MOVED/ASK redirects.
```typescript
import Redis from 'ioredis';

// ANTI-PATTERN: Standalone client used on cluster, unhedged keys without hash tags,
// and script operations spanning multiple slots causing CROSSSLOT errors.
export const valkey = new Redis('valkey://10.0.0.1:6379');

export async function transferPoints(senderId: string, receiverId: string, amount: number) {
  const script = `
    local senderBal = tonumber(redis.call('get', KEYS[1]) or 0)
    if senderBal < tonumber(ARGV[1]) then
      return -1
    end
    redis.call('decrby', KEYS[1], ARGV[1])
    redis.call('incrby', KEYS[2], ARGV[1])
    return 1
  `;
  
  // BUG: Keys hash to completely different slots in Valkey Cluster -> CROSSSLOT error
  // BUG: No retry policy or cluster redirection handling for failover
  return await valkey.eval(
    script,
    2,
    `user:${senderId}:balance`,
    `user:${receiverId}:balance`,
    amount
  );
}
```

#### Production Standard (Correct)
> **Standard**: Hash tags (`{tenant:...}`) guarantee that both keys hash to the identical slot, enabling atomic execution in Valkey Cluster. The Cluster client manages dynamic topology refreshes, ignores non-functional slots gracefully, prevents reconnect storms via jittered backoff, and ensures commands survive node failovers.
```typescript
import { Cluster, ClusterOptions } from 'ioredis';

const clusterOptions: ClusterOptions = {
  clusterRetryStrategy(times: number) {
    // Exponential backoff with jitter during failover election windows
    const delay = Math.min(100 * Math.pow(2, times), 3000);
    const jitter = Math.floor(Math.random() * 200);
    return delay + jitter;
  },
  enableOfflineQueue: false, // Fail fast rather than building unbound memory pressure
  redisOptions: {
    connectTimeout: 5000,
    commandTimeout: 2000,
  },
  slotsRefreshMinInterval: 1000, // Throttles topology refreshes to protect servers
  scaleReads: 'master', // Enforce linearizable writes and strictly consistent reads
};

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

export async function transferTenantPoints(
  tenantId: string,
  senderId: string,
  receiverId: string,
  amount: number
): Promise<boolean> {
  // Invariant: Use curly braces {tenantId} to force identical cluster slot assignment
  const senderKey = `{tenant:${tenantId}}:user:${senderId}:balance`;
  const receiverKey = `{tenant:${tenantId}}:user:${receiverId}:balance`;

  const script = `
    local senderBal = tonumber(redis.call('get', KEYS[1]) or 0)
    if senderBal < tonumber(ARGV[1]) then
      return 0
    end
    redis.call('decrby', KEYS[1], ARGV[1])
    redis.call('incrby', KEYS[2], ARGV[1])
    return 1
  `;

  const result = await valkeyCluster.eval(
    script,
    2,
    senderKey,
    receiverKey,
    amount
  );

  return result === 1;
}
```

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, Cluster Slot Routing, and Script Invariants

Valkey 8.0 Threaded I/O, Cluster Slot Routing, and Script Invariants defines an industrial-grade behavioral standard for backend engineering. Valkey 8.0 leverages enhanced multi-threaded I/O while maintaining a single-threaded command execution core, making operations vulnerable to cross-slot failures and unhandled cluster topology mutations. In distributed cluster deployments, any multi-key operation or Lua script execution without proper hash-tag alignment will trigger CROSSSLOT errors, while failover events without backoff-jittered slot-cache invalidation precipitate thundering herd failures. Applications MUST enforce deterministic hash-tagging, decouple I/O concurrency from blocking scripts, and handle MOVED/ASK redirections with rate-limited topology refreshes. 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 Non-Hash-Tagged Multi-Key Scripts and Unhedged Failover Connection Storms in Valkey Clusters 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 Non-Hash-Tagged Multi-Key Scripts and Unhedged Failover Connection Storms in Valkey ClustersCritical
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 encapsulate co-dependent keys within curly braces (e.g., `{tenant

Engineers MUST ALWAYS encapsulate co-dependent keys within curly braces (e.g., `{tenant:123}:user` and `{tenant:123}:profile`) to guarantee assignment to the identical 16384 cluster hash 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.
2

Engineers MUST NOT execute multi-key Lua scripts or MGET/MSET operations across un-tagged keys, as Valkey 8.0 Cluster will reject them with a fatal CROSSSLOT error.

Engineers MUST NOT execute multi-key Lua scripts or MGET/MSET operations across un-tagged keys, as Valkey 8.0 Cluster will reject them with a fatal CROSSSLOT error.

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 MUST configure cluster clients with automated topology refresh triggered by MOVED redirects, constrained by a minimum refresh interval (e.g., minRateLimit

Code MUST configure cluster clients with automated topology refresh triggered by MOVED redirects, constrained by a minimum refresh interval (e.g., minRateLimit: 1000ms) to prevent failover 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.
4

Services MUST implement exponential backoff with full jitter when handling cluster reconnection errors during primary node failover elections.

Services MUST implement exponential backoff with full jitter when handling cluster reconnection errors during primary node failover elections.

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 long-running (>5ms) Lua scripts or synchronous blocking commands (e.g., KEYS, SAVE) in Valkey 8.0

Components NEVER execute long-running (>5ms) Lua scripts or synchronous blocking commands (e.g., KEYS, SAVE) in Valkey 8.0; threaded I/O accelerates socket processing but command execution remains single-threaded.

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.
6

Cluster clients SHOULD direct read operations to replicas only when eventual consistency is explicitly tolerated using READONLY mode, keeping write paths isolated to current slot primaries.

Cluster clients SHOULD direct read operations to replicas only when eventual consistency is explicitly tolerated using READONLY mode, keeping write paths isolated to current slot primaries.

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, Cluster Slot Routing, and Script Invariants critical for AI coding assistants?

Valkey 8.0 leverages enhanced multi-threaded I/O while maintaining a single-threaded command execution core, making operations vulnerable to cross-slot failures and unhandled cluster topology mutations. In distributed cluster deployments, any multi-key operation or Lua script execution without proper hash-tag alignment will trigger CROSSSLOT errors, while failover events without backoff-jittered slot-cache invalidation precipitate thundering herd failures. Applications MUST enforce deterministic hash-tagging, decouple I/O concurrency from blocking scripts, and handle MOVED/ASK redirections with rate-limited topology refreshes. 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 Non-Hash-Tagged Multi-Key Scripts and Unhedged Failover Connection Storms in Valkey Clusters) 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