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

Valkey 8.0 Threaded I/O, Cluster Slot Routing, and Hash-Tag Atomicity Invariants

Enforces strict cluster-aware client configurations, explicit hash tag slot co-location for multi-key Lua scripts, and non-blocking multi-threaded I/O constraints in Valkey 8.0 deployments. Prevents unhandled MOVED/ASK redirect storms during replica promotion, cross-slot script runtime exceptions, and main thread execution freezes that degrade heartbeat detection.

Prohibited Anti-Pattern: Unpinned Multi-Key Lua Execution and Unbounded Slot Refresh Storms During Cluster Failover

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 Hash-Tag Atomicity Invariants - Enforces strict cluster-aware client configurations, explicit hash tag slot co-location for multi-key Lua scripts, and non-blocking multi-threaded I/O constraints in Valkey 8.0 deployments. Prevents unhandled MOVED/ASK redirect storms during replica promotion, cross-slot script runtime exceptions, and main thread execution freezes that degrade heartbeat detection.
globs: src/infra/cache/**/*.{ts,js,py,go}, src/services/cache/**/*.{ts,js,py,go}, src/lib/valkey/**/*.{ts,js,py,go}, src/config/valkey*.{ts,js,py,go}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/valkey-cluster-failover-multithreaded-io-standard"
---

# Valkey 8.0 Threaded I/O, Cluster Slot Routing, and Hash-Tag Atomicity Invariants

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unpinned Multi-Key Lua Execution and Unbounded Slot Refresh Storms During Cluster Failover

## System Constraints
You are a Principal Backend Architect specializing in Valkey 8.0 high-throughput infrastructure. Enforce non-negotiable invariants: all multi-key Lua scripts executed on Valkey Cluster must isolate shard boundaries using standardized hash tags {tag}; cluster clients must implement topology-aware backoff buffers to mitigate MOVED/ASK redirect floods during replica failovers; and Lua routines must execute in under 5ms to avoid starving Valkey 8.0 main-thread coordination despite multi-threaded I/O pipelines.

## Strict Guidelines
- Engineers MUST ALWAYS encapsulate cluster keys in explicit hash tags (e.g., `{tenant:123}:session`, `{tenant:123}:profile`) when targeting multiple keys in single commands or Lua scripts.
- Engineers MUST NOT execute Lua scripts containing unbounded iterations or blocking operations; scripts exceeding cluster-node-timeout thresholds trigger false-positive node failovers.
- Cluster client configurations MUST configure non-zero exponential retry backoffs with circuit breaking for MOVED and ASK redirection handling.
- Clients MUST enable proactive cluster topology refresh intervals and set refresh-on-error thresholds to avoid thundering-herd routing failures during node promotions.
- Engineers MUST NOT rely on Valkey 8.0 threaded I/O (`io-threads`) for command atomicity; while socket reads/writes are multi-threaded, core command execution remains strictly single-threaded on the event loop.

## Bad vs Good Implementations
### Cross-Slot Lua Scripting and Naive Client vs Slot-Anchored Cluster Configuration

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: The bad example generates cross-slot runtime exceptions in a Valkey 8.0 cluster because `k1` and `k2` do not use curly brace hash tags (`{...}`). Additionally, the cluster client lacks failover topology refresh options (`slotsRefreshTimeout`, backoff policies), causing continuous command failures and thread stalls when a primary fails and a replica promotes.
```typescript
import Redis, { Cluster } from 'ioredis';

// ANTI-PATTERN: Default cluster options with naive client, no slot refresh,
// and multi-key script execution lacking hash tags (triggers CROSSSLOT error).
export const valkeyCluster = new Cluster([
  { host: 'valkey-node-1', port: 6379 },
  { host: 'valkey-node-2', port: 6379 }
], {
  // Default infinite retries without backoff lead to connection exhaustion during failovers
  scaleReads: 'all'
});

const atomicCounterLua = `
  local current = redis.call('GET', KEYS[1])
  if not current then
    redis.call('SET', KEYS[1], '0')
  end
  redis.call('INCRBY', KEYS[1], ARGV[1])
  redis.call('SET', KEYS[2], ARGV[2]) -- CROSSSLOT error if KEYS[1] and KEYS[2] map to different nodes
  return 1
`;

export async function updateMetrics(userId: string, delta: number, metadata: string) {
  // Anti-pattern: Keys lack curly-brace hash tags: CRC16 calculation puts them on arbitrary slots
  const k1 = `metrics:user:${userId}`;
  const k2 = `audit:log:${userId}`;
  
  return valkeyCluster.eval(atomicCounterLua, 2, k1, k2, delta.toString(), metadata);
}
```

#### Production Standard (Correct)
> **Standard**: The good example encapsulates shard isolation with `{user:${userId}}`, guaranteeing both keys map to the exact same hash slot under Valkey's CRC16 calculation. The cluster configuration defines explicit slot refresh timers, jittered exponential backoffs for failovers, and robust redirect handling, preventing connection storms during primary-replica failovers.
```typescript
import { Cluster, ClusterOptions } from 'ioredis';

const clusterConfig: ClusterOptions = {
  enableReadyCheck: true,
  maxRedirections: 16,
  slotsRefreshTimeout: 2000,
  slotsRefreshInterval: 30000, // Proactively update topology every 30s
  retryDelayOnFailover: 100,
  retryDelayOnClusterDown: 250,
  retryDelayOnTryAgain: 100,
  clusterRetryStrategy(times: number) {
    // Exponential backoff capped at 2000ms with jitter to prevent reconnect storms
    const delay = Math.min(100 * Math.pow(2, times), 2000);
    return delay + Math.floor(Math.random() * 50);
  },
  scaleReads: 'slave'
};

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

// Deterministic hash tag ensures all keys hash to the identical slot: CRC16({user:XYZ})
const ATOMIC_COUNTER_LUA = `
  local current = redis.call('GET', KEYS[1])
  if not current then
    redis.call('SET', KEYS[1], '0')
  end
  redis.call('INCRBY', KEYS[1], ARGV[1])
  redis.call('SET', KEYS[2], ARGV[2])
  return 1
`;

export async function updateMetrics(userId: string, delta: number, metadata: string): Promise<number> {
  // Invariant: Standardize slot anchor via hash tags {...}
  const hashTag = `{user:${userId}}`;
  const k1 = `${hashTag}:metrics`;
  const k2 = `${hashTag}:audit`;

  // Guaranteed co-located on the same shard; failover resilient via clusterRetryStrategy
  const result = await valkeyCluster.eval(ATOMIC_COUNTER_LUA, 2, k1, k2, delta.toString(), metadata);
  return result 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, Cluster Slot Routing, and Hash-Tag Atomicity Invariants

Valkey 8.0 Threaded I/O, Cluster Slot Routing, and Hash-Tag Atomicity Invariants defines an industrial-grade behavioral standard for backend engineering. Enforces strict cluster-aware client configurations, explicit hash tag slot co-location for multi-key Lua scripts, and non-blocking multi-threaded I/O constraints in Valkey 8.0 deployments. Prevents unhandled MOVED/ASK redirect storms during replica promotion, cross-slot script runtime exceptions, and main thread execution freezes that degrade heartbeat detection. 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: Unpinned Multi-Key Lua Execution and Unbounded Slot Refresh Storms During Cluster Failover 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 DriftUnpinned Multi-Key Lua Execution and Unbounded Slot Refresh Storms During Cluster FailoverCritical
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 cluster keys in explicit hash tags (e.g., `{tenant

Engineers MUST ALWAYS encapsulate cluster keys in explicit hash tags (e.g., `{tenant:123}:session`, `{tenant:123}:profile`) when targeting multiple keys in single commands or Lua scripts.

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 containing unbounded iterations or blocking operations

Engineers MUST NOT execute Lua scripts containing unbounded iterations or blocking operations; scripts exceeding cluster-node-timeout thresholds trigger false-positive 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.
3

Cluster client configurations MUST configure non-zero exponential retry backoffs with circuit breaking for MOVED and ASK redirection handling.

Cluster client configurations MUST configure non-zero exponential retry backoffs with circuit breaking for MOVED and ASK redirection handling.

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

Clients MUST enable proactive cluster topology refresh intervals and set refresh-on-error thresholds to avoid thundering-herd routing failures during node promotions.

Clients MUST enable proactive cluster topology refresh intervals and set refresh-on-error thresholds to avoid thundering-herd routing failures during node promotions.

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 NOT rely on Valkey 8.0 threaded I/O (`io-threads`) for command atomicity

Engineers MUST NOT rely on Valkey 8.0 threaded I/O (`io-threads`) for command atomicity; while socket reads/writes are multi-threaded, core command execution remains strictly single-threaded on the event loop.

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 Hash-Tag Atomicity Invariants critical for AI coding assistants?

Enforces strict cluster-aware client configurations, explicit hash tag slot co-location for multi-key Lua scripts, and non-blocking multi-threaded I/O constraints in Valkey 8.0 deployments. Prevents unhandled MOVED/ASK redirect storms during replica promotion, cross-slot script runtime exceptions, and main thread execution freezes that degrade heartbeat detection. 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 (Unpinned Multi-Key Lua Execution and Unbounded Slot Refresh Storms During Cluster Failover) 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