This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: axum-sse-backpressure-drain-guardrail

Axum 0.8 Bounded SSE Stream Draining and Heartbeat Backpressure

Server-Sent Events (SSE) in Axum 0.8 must use strictly bounded channels combined with proactive keep-alive heartbeats to handle slow or hung HTTP/2 and HTTP/3 consumers. Failure to apply backpressure or monitor disconnect states leads to unbounded memory growth from queued events and leaked asynchronous streaming tasks.

Prohibited Anti-Pattern: Unbounded Tokio MPSC channels paired with unmonitored SSE streams causing OOM crashes during slow-client stalls

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Axum 0.8 Bounded SSE Stream Draining and Heartbeat Backpressure - Server-Sent Events (SSE) in Axum 0.8 must use strictly bounded channels combined with proactive keep-alive heartbeats to handle slow or hung HTTP/2 and HTTP/3 consumers. Failure to apply backpressure or monitor disconnect states leads to unbounded memory growth from queued events and leaked asynchronous streaming tasks.
globs: src/**/*.rs
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/axum-sse-backpressure-drain-guardrail"
---

# Axum 0.8 Bounded SSE Stream Draining and Heartbeat Backpressure

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Tokio MPSC channels paired with unmonitored SSE streams causing OOM crashes during slow-client stalls

## System Constraints
You are a Principal Rust Architect enforcing deterministic resource management and backpressure controls for Axum 0.8 Server-Sent Events (SSE). You mandate bounded channels, client disconnect detection, and keep-alive ping mechanisms.

## Strict Guidelines
- Engineers MUST ALWAYS use bounded channels (such as `tokio::sync::mpsc::channel(N)`) with finite buffer limits when bridging event producers to Axum SSE responses.
- Engineers MUST configure `Sse::new(stream).keep_alive(...)` with explicit intervals and text payloads to detect dead TCP/H2 sessions and trigger stream termination.
- Producers MUST handle channel saturation using either `send().await` to propagate backpressure upstream, or `try_send()` with explicit load-shedding/error metrics; engineers MUST NEVER use `tokio::sync::mpsc::unbounded_channel()`.
- Streams returned to `axum::response::sse::Sse` MUST be wrapped in or derived from receiver streams that terminate promptly when the client aborts or disconnects, dropping internal publisher state.
- Services MUST monitor `tokio_util::sync::CancellationToken` or client disconnection signals if background processing tasks are spawned independently of the immediate response stream pipeline.

## Bad vs Good Implementations
### Bounded SSE Stream with Keep-Alive Heartbeat and Task Cleanup

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Using an unbounded channel allows a slow-reading client to accumulate an arbitrary number of SSE events in server memory, leading to an OOM panic. Omitting keep-alive heartbeats prevents the server from discovering broken TCP or stalled HTTP/2 connections.
```rust
use axum::{response::sse::{Event, Sse}, routing::get, Router};
use futures_util::stream::Stream;
use tokio::sync::mpsc;
use tokio_stream::wrappers::UnboundedReceiverStream;
use std::convert::Infallible;

// ANTI-PATTERN: Unbounded channel and missing keep-alive.
// Stalled HTTP/2 consumers cause unbounded heap allocation in the internal channel queue.
async fn sse_handler() -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
    let (tx, rx) = mpsc::unbounded_channel();

    tokio::spawn(async move {
        let mut counter = 0;
        loop {
            tokio::time::sleep(tokio::time::Duration::from_millis(50)).await;
            // Unbounded send never exerts backpressure on the producer
            let _ = tx.send(Ok(Event::default().data(format!("payload {}", counter))));
            counter += 1;
        }
    });

    // Missing .keep_alive() allows zombie connections to persist behind idle proxies
    Sse::new(UnboundedReceiverStream::new(rx))
}
```

#### Production Standard (Correct)
> **Standard**: Bounded channels cap the queued memory usage per connected client. If the client falls behind, `tx.send().await` pauses execution until buffer capacity opens up. If the client disconnects, `send().await` fails immediately, cleanly terminating the producer task, while `.keep_alive()` continuously probes connectivity.
```rust
use axum::{response::sse::{Event, KeepAlive, Sse}, routing::get, Router};
use futures_util::stream::Stream;
use tokio::sync::mpsc;
use tokio_stream::wrappers::ReceiverStream;
use std::{convert::Infallible, time::Duration};

const SSE_BUFFER_CAPACITY: usize = 64;
const KEEP_ALIVE_INTERVAL: Duration = Duration::from_secs(15);

// PRODUCTION FIX: Strictly bounded channel with backpressure propagation and ping frames
async fn sse_handler() -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
    let (tx, rx) = mpsc::channel::<Result<Event, Infallible>>(SSE_BUFFER_CAPACITY);

    tokio::spawn(async move {
        let mut counter = 0;
        loop {
            let event = Ok(Event::default().data(format!("payload {}", counter)));
            // Backpressure: Awaits slot availability or breaks immediately if client dropped receiver
            if tx.send(event).await.is_err() {
                // Downstream receiver dropped (client disconnected); exit loop to avoid orphan leak
                break;
            }
            counter += 1;
            tokio::time::sleep(Duration::from_millis(50)).await;
        }
    });

    Sse::new(ReceiverStream::new(rx))
        .keep_alive(
            KeepAlive::new()
                .interval(KEEP_ALIVE_INTERVAL)
                .text("keep-alive-ping")
        )
}
```

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 Bounded SSE Stream Draining and Heartbeat Backpressure

Axum 0.8 Bounded SSE Stream Draining and Heartbeat Backpressure defines an industrial-grade behavioral standard for backend engineering. Server-Sent Events (SSE) in Axum 0.8 must use strictly bounded channels combined with proactive keep-alive heartbeats to handle slow or hung HTTP/2 and HTTP/3 consumers. Failure to apply backpressure or monitor disconnect states leads to unbounded memory growth from queued events and leaked asynchronous streaming tasks. 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: Unbounded Tokio MPSC channels paired with unmonitored SSE streams causing OOM crashes during slow-client stalls 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 DriftUnbounded Tokio MPSC channels paired with unmonitored SSE streams causing OOM crashes during slow-client stallsCritical
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 use bounded channels (such as `tokio

Engineers MUST ALWAYS use bounded channels (such as `tokio::sync::mpsc::channel(N)`) with finite buffer limits when bridging event producers to Axum SSE 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.
2

Engineers MUST configure `Sse

Engineers MUST configure `Sse::new(stream).keep_alive(...)` with explicit intervals and text payloads to detect dead TCP/H2 sessions and trigger stream termination.

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

Producers MUST handle channel saturation using either `send().await` to propagate backpressure upstream, or `try_send()` with explicit load-shedding/error metrics

Producers MUST handle channel saturation using either `send().await` to propagate backpressure upstream, or `try_send()` with explicit load-shedding/error metrics; engineers MUST NEVER use `tokio::sync::mpsc::unbounded_channel()`.

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

Streams returned to `axum

Streams returned to `axum::response::sse::Sse` MUST be wrapped in or derived from receiver streams that terminate promptly when the client aborts or disconnects, dropping internal publisher 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

Services MUST monitor `tokio_util

Services MUST monitor `tokio_util::sync::CancellationToken` or client disconnection signals if background processing tasks are spawned independently of the immediate response stream pipeline.

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 Bounded SSE Stream Draining and Heartbeat Backpressure critical for AI coding assistants?

Server-Sent Events (SSE) in Axum 0.8 must use strictly bounded channels combined with proactive keep-alive heartbeats to handle slow or hung HTTP/2 and HTTP/3 consumers. Failure to apply backpressure or monitor disconnect states leads to unbounded memory growth from queued events and leaked asynchronous streaming tasks. 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 (Unbounded Tokio MPSC channels paired with unmonitored SSE streams causing OOM crashes during slow-client stalls) 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