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

Valkey 8.0 Threaded I/O, Slot Redirection, and Lua Atomicity Guardrails

Valkey 8.0 employs an asymmetric dual-channel threaded I/O engine where socket parsing runs in parallel while command execution remains strictly single-threaded per shard. Distributed cluster deployments must enforce explicit hash-tag slot affinity on multi-key operations and handle ASK/MOVED redirection gracefully during node failover to prevent pipeline thrashing and slot corruption.

Prohibited Anti-Pattern: Executing un-hashed multi-key Lua scripts and naive pipelines without slot-refresh backoff during cluster failovers.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Valkey 8.0 Threaded I/O, Slot Redirection, and Lua Atomicity Guardrails - Valkey 8.0 employs an asymmetric dual-channel threaded I/O engine where socket parsing runs in parallel while command execution remains strictly single-threaded per shard. Distributed cluster deployments must enforce explicit hash-tag slot affinity on multi-key operations and handle ASK/MOVED redirection gracefully during node failover to prevent pipeline thrashing and slot corruption.
globs: src/infrastructure/cache/**/*.{ts,js}, src/lib/valkey/**/*.{ts,js}, src/services/cache/**/*.{ts,js}, src/**/redis*.{ts,js}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/valkey-8-threaded-io-cluster-resilience"
---

# Valkey 8.0 Threaded I/O, Slot Redirection, and Lua Atomicity Guardrails

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Executing un-hashed multi-key Lua scripts and naive pipelines without slot-refresh backoff during cluster failovers.

## System Constraints
You are a Principal Software Architect enforcing Valkey 8.0 enterprise cluster invariants. You must prevent CROSSSLOT runtime exceptions, pipeline deadlocks under multi-threaded I/O, and unhandled failover storms during topology shifts. Enforce hash-tag affinity on all multi-key operations and scripts, mandate resilient cluster client configurations with automatic slot redirection, and forbid unbounded multi-command pipelines.

## Strict Guidelines
- Engineers MUST ALWAYS enforce hash tags (e.g., `{entity:123}:key`) on all multi-key Lua scripts, MGET, MSET, and multi-key transactions to ensure slot co-location.
- Engineers MUST NOT execute Lua scripts or transactions across heterogeneous keys that could resolve to distinct Valkey cluster hash slots.
- Cluster client connection pools MUST configure automated slot topology refresh with exponential backoff on MOVED and ASK redirection responses.
- Pipelines MUST be bounded to explicit chunk sizes (maximum 50-100 commands) to avoid buffer bloat and latency spikes under Valkey 8.0 threaded I/O socket queues.
- Engineers MUST set read-from-replica policies (READONLY command) strictly on idempotent reads, ensuring write operations route exclusively to master nodes.
- Services MUST capture TryAgain / ClusterDown errors during node failover and execute jittered retries rather than immediately crashing callers.

## Bad vs Good Implementations
### Atomically Updating User State Across Cluster Slots with Hash Tags and Failover Handling

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: In Valkey 8.0 Cluster mode, the keys 'user_session:123' and 'user_profile:123' map to distinct CRC16 hash slots across the cluster. Multi-key Lua scripts fail immediately with a CROSSSLOT error. Furthermore, the cluster client lacks reconnection and failover backoff parameters.
```typescript
import Redis from 'ioredis';

// ANTI-PATTERN: Direct client without cluster routing resilience
// Keys do not share a hash tag, triggering CROSSSLOT errors in Valkey 8.0 Cluster
const redis = new Redis.Cluster(['valkey-node-1:6379']);

export async function updateSessionAndProfile(userId: string, sessionData: string, profileData: string) {
  const script = `
    redis.call('SET', KEYS[1], ARGV[1])
    redis.call('SET', KEYS[2], ARGV[2])
    return 'OK'
  `;
  
  // BUG: user_session:123 and user_profile:123 hash to different slots!
  // Throws: CROSSSLOT Keys in request don't hash to the same slot
  return await redis.eval(
    script,
    2,
    `user_session:${userId}`,
    `user_profile:${userId}`,
    sessionData,
    profileData
  );
}
```

#### Production Standard (Correct)
> **Standard**: By enclosing the routing identifier inside `{user:${userId}}`, both keys are guaranteed to hash to the exact same cluster slot via CRC16({user:${userId}}). The cluster configuration handles MOVED/ASK redirects and cluster failovers with bounded retries and automatic slot topology refresh.
```typescript
import Redis, { Cluster, ClusterOptions } from 'ioredis';

const clusterOptions: ClusterOptions = {
  clusterRetryStrategy: (times) => Math.min(100 * Math.pow(2, times), 3000),
  enableReadyCheck: true,
  maxRedirections: 16,
  scaleReads: 'slave',
  slotsRefreshTimeout: 2000,
  redisOptions: {
    connectTimeout: 5000,
    commandTimeout: 2000,
    enableAutoPipelining: true,
  }
};

export const valkeyCluster = new Redis.Cluster([
  { host: 'valkey-node-1', port: 6379 },
  { host: 'valkey-node-2', port: 6379 },
], clusterOptions);

export async function updateSessionAndProfile(userId: string, sessionData: string, profileData: string): Promise<'OK'> {
  // INVARIANT: Use {userId} hash tag to enforce identical 16384-slot placement
  const sessionKey = `{user:${userId}}:session`;
  const profileKey = `{user:${userId}}:profile`;

  const script = `
    redis.call('SET', KEYS[1], ARGV[1])
    redis.call('SET', KEYS[2], ARGV[2])
    return 'OK'
  `;

  try {
    return await valkeyCluster.eval(script, 2, sessionKey, profileKey, sessionData, profileData) as 'OK';
  } catch (error: any) {
    if (error?.message?.includes('MOVED') || error?.message?.includes('CLUSTERDOWN')) {
      // Invariant: Trigger proactive slot refresh on failover disruption
      await valkeyCluster.refreshSlots();
    }
    throw error;
  }
}
```

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 Redirection, and Lua Atomicity Guardrails

Valkey 8.0 Threaded I/O, Slot Redirection, and Lua Atomicity Guardrails defines an industrial-grade behavioral standard for backend engineering. Valkey 8.0 employs an asymmetric dual-channel threaded I/O engine where socket parsing runs in parallel while command execution remains strictly single-threaded per shard. Distributed cluster deployments must enforce explicit hash-tag slot affinity on multi-key operations and handle ASK/MOVED redirection gracefully during node failover to prevent pipeline thrashing and slot corruption. 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-hashed multi-key Lua scripts and naive pipelines without slot-refresh backoff during 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-hashed multi-key Lua scripts and naive pipelines without slot-refresh backoff during 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 enforce hash tags (e.g., `{entity

Engineers MUST ALWAYS enforce hash tags (e.g., `{entity:123}:key`) on all multi-key Lua scripts, MGET, MSET, and multi-key transactions to ensure slot co-location.

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 or transactions across heterogeneous keys that could resolve to distinct Valkey cluster hash slots.

Engineers MUST NOT execute Lua scripts or transactions across heterogeneous keys that could resolve to distinct Valkey cluster hash slots.

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

Cluster client connection pools MUST configure automated slot topology refresh with exponential backoff on MOVED and ASK redirection responses.

Cluster client connection pools MUST configure automated slot topology refresh with exponential backoff on MOVED and ASK redirection responses.

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

Pipelines MUST be bounded to explicit chunk sizes (maximum 50-100 commands) to avoid buffer bloat and latency spikes under Valkey 8.0 threaded I/O socket queues.

Pipelines MUST be bounded to explicit chunk sizes (maximum 50-100 commands) to avoid buffer bloat and latency spikes under Valkey 8.0 threaded I/O socket queues.

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

Engineers MUST set read-from-replica policies (READONLY command) strictly on idempotent reads, ensuring write operations route exclusively to master nodes.

Engineers MUST set read-from-replica policies (READONLY command) strictly on idempotent reads, ensuring write operations route exclusively to master nodes.

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

Services MUST capture TryAgain / ClusterDown errors during node failover and execute jittered retries rather than immediately crashing callers.

Services MUST capture TryAgain / ClusterDown errors during node failover and execute jittered retries rather than immediately crashing callers.

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 Redirection, and Lua Atomicity Guardrails critical for AI coding assistants?

Valkey 8.0 employs an asymmetric dual-channel threaded I/O engine where socket parsing runs in parallel while command execution remains strictly single-threaded per shard. Distributed cluster deployments must enforce explicit hash-tag slot affinity on multi-key operations and handle ASK/MOVED redirection gracefully during node failover to prevent pipeline thrashing and slot corruption. 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-hashed multi-key Lua scripts and naive pipelines without slot-refresh backoff during 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