This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
securitycritical PriorityID: openfga-cdc-zanzibar-cache-invalidation-stream

OpenFGA CDC-Driven Zanzibar Cache Invalidation & Consistency Guardrail

Eliminates authorization race conditions by streaming transactional tuple mutations directly into distributed ReBAC cache nodes with sub-millisecond consistency. Forbids dual-write cache invalidation from application services and guarantees out-of-order mutation rejection via monotonically increasing Log Sequence Numbers (LSN) and zookie token tracking.

Prohibited Anti-Pattern: Dual-Write Application-Tier Cache Eviction and Untracked Temporal Tuple Race Conditions

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: OpenFGA CDC-Driven Zanzibar Cache Invalidation & Consistency Guardrail - Eliminates authorization race conditions by streaming transactional tuple mutations directly into distributed ReBAC cache nodes with sub-millisecond consistency. Forbids dual-write cache invalidation from application services and guarantees out-of-order mutation rejection via monotonically increasing Log Sequence Numbers (LSN) and zookie token tracking.
globs: src/**/auth/**/*.{ts,js,go,py}, src/**/authorization/**/*.{ts,js,go,py}, src/**/fga/**/*.{ts,js,go,py}, src/**/cdc/**/*.{ts,js,go,py}, src/**/cache/**/*.{ts,js,go,py}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/openfga-cdc-zanzibar-cache-invalidation-stream"
---

# OpenFGA CDC-Driven Zanzibar Cache Invalidation & Consistency Guardrail

> **Severity**: CRITICAL | **Category**: security
> **Anti-Pattern Warning**: Dual-Write Application-Tier Cache Eviction and Untracked Temporal Tuple Race Conditions

## System Constraints
You are a Principal Security Architect enforcing zero-trust authorization consistency using OpenFGA and Zanzibar-style ReBAC. You must ensure that distributed permission caches are strictly invalidated via transactional Change Data Capture (CDC) streams (e.g., Debezium, Postgres WAL) rather than application dual-writes. Mandate causal consistency tracking with monotonically increasing Log Sequence Numbers (LSN) or transaction commit timestamps (zookies) to prevent stale authorization writes and temporal cache poisoning.

## Strict Guidelines
- Services MUST NEVER perform dual-write cache evictions or updates directly from the application request path when mutating OpenFGA authorization tuples.
- Services MUST publish OpenFGA tuple mutations transactionally to persistent storage (e.g., PostgreSQL with Outbox pattern or native OpenFGA datastore) and stream invalidations via Change Data Capture (CDC).
- Cache invalidation workers MUST process CDC events idempotently and verify monotonic ordering using transaction commit timestamps, Log Sequence Numbers (LSN), or zookie checkpoints.
- Cache nodes MUST reject invalidation payloads with an LSN or transaction timestamp older than the node's currently applied high-water mark.
- Authorization check layers MUST propagate Zanzibar consistency tokens ('zookies' or check snapshots) to cache queries to ensure read-your-writes causal consistency across distributed nodes.
- In-memory and distributed L1/L2 ReBAC caches MUST use explicit negative caching (denials) only with short TTLs and explicit CDC-driven tombstone invalidation.

## Bad vs Good Implementations
### Dual-Write Cache Invalidation vs. CDC-Driven Monotonic LSN Eviction

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Directly deleting keys from Redis during an API request creates authorization race conditions, is vulnerable to partial failure (network partition between write and eviction), and cannot guarantee causal order against concurrent writes.
```typescript
import { OpenFgaClient } from '@openfga/sdk';
import { Redis } from 'ioredis';

const fga = new OpenFgaClient({ apiUrl: process.env.FGA_API_URL });
const redis = new Redis();

export async function revokeAccess(user: string, relation: string, object: string) {
  // Anti-pattern: Dual-write to FGA and manually evicting from cache.
  // If the cache eviction fails or runs out-of-order with concurrent writes,
  // stale permissions linger indefinitely, leading to unauthorized access.
  await fga.write({
    deletes: [{ user, relation, object }],
  });

  const cacheKey = `authz:${user}:${relation}:${object}`;
  await redis.del(cacheKey);
}
```

#### Production Standard (Correct)
> **Standard**: Relies entirely on an idempotent CDC stream event driven by storage write-ahead logs. Guarantees monotonic ordering using Log Sequence Numbers (LSN) via an atomic Lua script, preventing out-of-order stale writes and dual-write divergence.
```typescript
import { z } from 'zod';
import { Redis } from 'ioredis';

const TupleMutationEventSchema = z.object({
  lsn: z.string().regex(/^\d+$/),
  op: z.enum(['c', 'u', 'd']),
  tuple: z.object({
    user: z.string(),
    relation: z.string(),
    object: z.string(),
  }),
  commitTimestamp: z.number(),
});

type TupleMutationEvent = z.infer<typeof TupleMutationEventSchema>;

export async function handleCdcInvalidation(
  rawPayload: unknown,
  redis: Redis
): Promise<void> {
  const event = TupleMutationEventSchema.parse(rawPayload);
  const { user, relation, object } = event.tuple;
  const currentLsn = BigInt(event.lsn);
  
  const watermarkKey = `authz:watermark:${object}`;
  const cacheKey = `authz:${user}:${relation}:${object}`;

  // Atomic Redis pipeline with Lua script to ensure monotonic LSN processing
  const luaScript = `
    local watermarkKey = KEYS[1]
    local cacheKey = KEYS[2]
    local incomingLsn = ARGV[1]
    
    local currentWatermark = redis.call('GET', watermarkKey)
    if not currentWatermark or tonumber(incomingLsn) > tonumber(currentWatermark) then
      redis.call('SET', watermarkKey, incomingLsn)
      redis.call('DEL', cacheKey)
      return 1
    end
    return 0
  `;

  const applied = await redis.eval(luaScript, 2, watermarkKey, cacheKey, currentLsn.toString());
  if (!applied) {
    // Log out-of-order drop for distributed tracing audit
    console.warn(`[CDC-ReBAC] Stale invalidation dropped for ${cacheKey} (LSN ${currentLsn})`);
  }
}
```

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: OpenFGA CDC-Driven Zanzibar Cache Invalidation & Consistency Guardrail

OpenFGA CDC-Driven Zanzibar Cache Invalidation & Consistency Guardrail defines an industrial-grade behavioral standard for security engineering. Eliminates authorization race conditions by streaming transactional tuple mutations directly into distributed ReBAC cache nodes with sub-millisecond consistency. Forbids dual-write cache invalidation from application services and guarantees out-of-order mutation rejection via monotonically increasing Log Sequence Numbers (LSN) and zookie token tracking. 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: Dual-Write Application-Tier Cache Eviction and Untracked Temporal Tuple Race Conditions 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 DriftDual-Write Application-Tier Cache Eviction and Untracked Temporal Tuple Race ConditionsCritical
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

Services MUST NEVER perform dual-write cache evictions or updates directly from the application request path when mutating OpenFGA authorization tuples.

Services MUST NEVER perform dual-write cache evictions or updates directly from the application request path when mutating OpenFGA authorization tuples.

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

Services MUST publish OpenFGA tuple mutations transactionally to persistent storage (e.g., PostgreSQL with Outbox pattern or native OpenFGA datastore) and stream invalidations via Change Data Capture (CDC).

Services MUST publish OpenFGA tuple mutations transactionally to persistent storage (e.g., PostgreSQL with Outbox pattern or native OpenFGA datastore) and stream invalidations via Change Data Capture (CDC).

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

Cache invalidation workers MUST process CDC events idempotently and verify monotonic ordering using transaction commit timestamps, Log Sequence Numbers (LSN), or zookie checkpoints.

Cache invalidation workers MUST process CDC events idempotently and verify monotonic ordering using transaction commit timestamps, Log Sequence Numbers (LSN), or zookie checkpoints.

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

Cache nodes MUST reject invalidation payloads with an LSN or transaction timestamp older than the node's currently applied high-water mark.

Cache nodes MUST reject invalidation payloads with an LSN or transaction timestamp older than the node's currently applied high-water mark.

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

Authorization check layers MUST propagate Zanzibar consistency tokens ('zookies' or check snapshots) to cache queries to ensure read-your-writes causal consistency across distributed nodes.

Authorization check layers MUST propagate Zanzibar consistency tokens ('zookies' or check snapshots) to cache queries to ensure read-your-writes causal consistency across distributed 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

In-memory and distributed L1/L2 ReBAC caches MUST use explicit negative caching (denials) only with short TTLs and explicit CDC-driven tombstone invalidation.

In-memory and distributed L1/L2 ReBAC caches MUST use explicit negative caching (denials) only with short TTLs and explicit CDC-driven tombstone invalidation.

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 OpenFGA CDC-Driven Zanzibar Cache Invalidation & Consistency Guardrail critical for AI coding assistants?

Eliminates authorization race conditions by streaming transactional tuple mutations directly into distributed ReBAC cache nodes with sub-millisecond consistency. Forbids dual-write cache invalidation from application services and guarantees out-of-order mutation rejection via monotonically increasing Log Sequence Numbers (LSN) and zookie token tracking. 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 (Dual-Write Application-Tier Cache Eviction and Untracked Temporal Tuple Race Conditions) 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

OpenFGA