This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: axum-tower-gcra-rate-limiting-degradation

Axum 0.8 Tower GCRA Rate-Limiting Degradation Standard

Maintains deterministic sub-millisecond tail latency under traffic surges by decoupling distributed rate-limiting from hot request paths. The system mandates a circuit-broken fallback from Redis-backed Generic Cell Rate Algorithm (GCRA) counters to lock-free in-memory atomic token buckets whenever Redis latency exceeds deadlines or encounters connection pool saturation.

Prohibited Anti-Pattern: Synchronously Awaiting Redis GCRA State in Tower Middleware Without Hard Latency Budgets or In-Memory Degradation

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Tower GCRA Rate-Limiting Degradation Standard - Maintains deterministic sub-millisecond tail latency under traffic surges by decoupling distributed rate-limiting from hot request paths. The system mandates a circuit-broken fallback from Redis-backed Generic Cell Rate Algorithm (GCRA) counters to lock-free in-memory atomic token buckets whenever Redis latency exceeds deadlines or encounters connection pool saturation.
globs: src/**/*.rs, crates/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-tower-gcra-rate-limiting-degradation"
---

# Axum 0.8 Tower GCRA Rate-Limiting Degradation Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Synchronously Awaiting Redis GCRA State in Tower Middleware Without Hard Latency Budgets or In-Memory Degradation

## System Constraints
You are a Principal Rust Systems Architect enforcing strict tail-latency bounds and fault-tolerant rate limiting on Axum 0.8 and Tower services. Mandate the use of non-blocking, two-tier GCRA architectures that fall back to atomic in-memory state when distributed stores fail or spike in latency.

## Strict Guidelines
- Engineers MUST ALWAYS wrap distributed rate-limiting operations (e.g., Redis GCRA Lua scripts) in a strict timeout budget (maximum 2-5ms) using `tokio::time::timeout`.
- Services MUST implement a circuit-breaker mechanism (e.g., Fail-Open to Local Memory) that routes traffic to an in-memory lock-free GCRA implementation (such as `governor` with `AtomicU64` state) when distributed rate-limiting errors out or breaches timeouts.
- Engineers MUST NOT block Tokio worker threads with synchronous I/O or unbounded async locks while acquiring or updating quota state.
- Middleware MUST inject telemetry headers (`X-RateLimit-Limit`, `X-RateLimit-Remaining`, `X-RateLimit-Degraded`) indicating whether throttling was evaluated via distributed consensus or degraded local state.
- Tower services MUST implement `poll_ready` deterministically and NEVER defer rate-limit evaluation directly inside `poll_ready`; limit evaluation belongs in the `call` path or an explicit Tower middleware layer.

## Bad vs Good Implementations
### Degradable GCRA Tower Layer with Redis Timeout and Local Fallback

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Awaiting a remote Redis command without a hard deadline blocks execution indefinitely if Redis experiences network partitions, slow queries, or failover. When Redis fails, requests fail with 500 errors instead of safely degrading.
```rust
use axum::{extract::Request, response::Response, middleware::Next};
use fred::prelude::*;

pub async fn rate_limit_middleware(req: Request, next: Next) -> Result<Response, (axum::http::StatusCode, &'static str)> {
    let client = req.extensions().get::<RedisClient>().expect("Redis client missing");
    let key = "rate_limit:global";
    
    // CRITICAL FLAW: Direct await with no timeout. If Redis stalls or reconnects,
    // all Tokio workers halt on this future, cascading into widespread 504 gateway timeouts.
    let current: u64 = client.incr(key, None).await.map_err(|_| {
        (axum::http::StatusCode::INTERNAL_SERVER_ERROR, "Redis Failure")
    })?;
    
    if current > 1000 {
        return Err((axum::http::StatusCode::TOO_MANY_REQUESTS, "Rate limit exceeded"));
    }
    
    Ok(next.run(req).await)
}
```

#### Production Standard (Correct)
> **Standard**: Wraps distributed GCRA checks in a 3ms hard deadline using `tokio::time::timeout`. If Redis hangs, exhausts its pool, or encounters errors, the request instantly degrades to a thread-safe local atomic token bucket (`governor`), ensuring sub-millisecond latency boundaries are preserved.
```rust
use axum::{extract::Request, response::{IntoResponse, Response}, middleware::Next};
use std::{sync::Arc, time::Duration};
use tokio::time::timeout;
use governor::{Quota, RateLimiter, clock::DefaultClock, state::keyed::DefaultKeyedStateStore};
use nonzero_ext::nonzero;

#[derive(Clone)]
pub struct RateLimiterState {
    pub local_limiter: Arc<RateLimiter<String, DefaultKeyedStateStore<String>, DefaultClock>>,
    pub redis_pool: deadpool_redis::Pool,
}

pub async fn gcra_rate_limit_middleware(
    state: axum::extract::State<RateLimiterState>,
    req: Request,
    next: Next,
) -> Response {
    let ip_key = req.extensions().get::<std::net::SocketAddr>()
        .map(|a| a.ip().to_string())
        .unwrap_or_else(|| "anonymous".into());

    // Bound remote evaluation strictly to 3ms to preserve sub-millisecond tail latency budgets
    let redis_check = timeout(Duration::from_millis(3), async {
        let mut conn = state.redis_pool.get().await.map_err(|e| e.to_string())?;
        // Execute atomic GCRA evaluation via evalsha/lua script in production
        deadpool_redis::redis::cmd("CL.THROTTLE")
            .arg(&ip_key).arg(100).arg(100).arg(60).arg(1)
            .query_async::<_, (i64, i64, i64, i64, i64)>(&mut conn)
            .await
            .map_err(|e| e.to_string())
    }).await;

    let allowed = match redis_check {
        Ok(Ok((allowed, _, _, _, _))) => allowed == 0,
        _ => {
            // Fallback: Degrade immediately to lock-free local in-memory GCRA governor
            state.local_limiter.check_key(&ip_key).is_ok()
        }
    };

    if !allowed {
        return (axum::http::StatusCode::TOO_MANY_REQUESTS, "Too Many Requests").into_response();
    }

    next.run(req).await
}
```

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: Axum 0.8 Tower GCRA Rate-Limiting Degradation Standard

Axum 0.8 Tower GCRA Rate-Limiting Degradation Standard defines an industrial-grade behavioral standard for backend engineering. Maintains deterministic sub-millisecond tail latency under traffic surges by decoupling distributed rate-limiting from hot request paths. The system mandates a circuit-broken fallback from Redis-backed Generic Cell Rate Algorithm (GCRA) counters to lock-free in-memory atomic token buckets whenever Redis latency exceeds deadlines or encounters connection pool saturation. 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: Synchronously Awaiting Redis GCRA State in Tower Middleware Without Hard Latency Budgets or In-Memory Degradation 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 DriftSynchronously Awaiting Redis GCRA State in Tower Middleware Without Hard Latency Budgets or In-Memory DegradationCritical
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 distributed rate-limiting operations (e.g., Redis GCRA Lua scripts) in a strict timeout budget (maximum 2-5ms) using `tokio

Engineers MUST ALWAYS wrap distributed rate-limiting operations (e.g., Redis GCRA Lua scripts) in a strict timeout budget (maximum 2-5ms) using `tokio::time::timeout`.

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 implement a circuit-breaker mechanism (e.g., Fail-Open to Local Memory) that routes traffic to an in-memory lock-free GCRA implementation (such as `governor` with `AtomicU64` state) when distributed rate-limiting errors out or breaches timeouts.

Services MUST implement a circuit-breaker mechanism (e.g., Fail-Open to Local Memory) that routes traffic to an in-memory lock-free GCRA implementation (such as `governor` with `AtomicU64` state) when distributed rate-limiting errors out or breaches timeouts.

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

Engineers MUST NOT block Tokio worker threads with synchronous I/O or unbounded async locks while acquiring or updating quota state.

Engineers MUST NOT block Tokio worker threads with synchronous I/O or unbounded async locks while acquiring or updating quota state.

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

Middleware MUST inject telemetry headers (`X-RateLimit-Limit`, `X-RateLimit-Remaining`, `X-RateLimit-Degraded`) indicating whether throttling was evaluated via distributed consensus or degraded local state.

Middleware MUST inject telemetry headers (`X-RateLimit-Limit`, `X-RateLimit-Remaining`, `X-RateLimit-Degraded`) indicating whether throttling was evaluated via distributed consensus or degraded local state.

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

Tower services MUST implement `poll_ready` deterministically and NEVER defer rate-limit evaluation directly inside `poll_ready`

Tower services MUST implement `poll_ready` deterministically and NEVER defer rate-limit evaluation directly inside `poll_ready`; limit evaluation belongs in the `call` path or an explicit Tower middleware layer.

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 Axum 0.8 Tower GCRA Rate-Limiting Degradation Standard critical for AI coding assistants?

Maintains deterministic sub-millisecond tail latency under traffic surges by decoupling distributed rate-limiting from hot request paths. The system mandates a circuit-broken fallback from Redis-backed Generic Cell Rate Algorithm (GCRA) counters to lock-free in-memory atomic token buckets whenever Redis latency exceeds deadlines or encounters connection pool saturation. 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 (Synchronously Awaiting Redis GCRA State in Tower Middleware Without Hard Latency Budgets or In-Memory Degradation) 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

Axum