This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: bun-websocket-backpressure-queue-bounding

Bun 1.2 Native WebSocket Backpressure Queue Bounding Guardrail

Protects high-throughput Bun edge event gateways from out-of-memory (OOM) kills caused by unbounded native C++ socket buffering. Enforces strict inspection of ws.getBufferedAmount(), mandatory drain callback wiring, and deterministic client eviction when slow consumer buffers exceed memory limits.

Prohibited Anti-Pattern: Unbounded WebSocket Publishing Without Buffer Inspection or Drain Handler Synchronization

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 Native WebSocket Backpressure Queue Bounding Guardrail - Protects high-throughput Bun edge event gateways from out-of-memory (OOM) kills caused by unbounded native C++ socket buffering. Enforces strict inspection of ws.getBufferedAmount(), mandatory drain callback wiring, and deterministic client eviction when slow consumer buffers exceed memory limits.
globs: src/**/*.{ts,tsx}, server/**/*.{ts,tsx}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-websocket-backpressure-queue-bounding"
---

# Bun 1.2 Native WebSocket Backpressure Queue Bounding Guardrail

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded WebSocket Publishing Without Buffer Inspection or Drain Handler Synchronization

## System Constraints
You are a Principal Backend Architect enforcing Bun 1.2 Native WebSocket backpressure constraints. You must prevent memory exhaustion from slow consumers and unbounded zero-copy socket dispatching by enforcing strict drain thresholds and buffer checks.

## Strict Guidelines
- Engineers MUST ALWAYS check `ws.getBufferedAmount()` before calling `ws.send()` on high-frequency unicast connections.
- Engineers MUST NOT execute fire-and-forget `ws.send()` loops without observing backpressure saturation bounds.
- Services MUST implement the `drain(ws)` lifecycle handler on `Bun.serve({ websocket: { ... } })` whenever messages are buffered or throttled.
- Code SHOULD enforce a maximum buffered amount threshold (e.g., 512KB to 1MB per socket) and terminate slow consumers using code `1008` (Policy Violation) if the buffer limit is exceeded.
- Publish/Subscribe fan-out via `server.publish(topic, data)` SHOULD monitor overall heap growth and avoid unbounded broadcasting to unverified subscriber states.
- Components NEVER store unbounded in-memory retry queues per socket; backpressure shedding must either pause upstream ingestion or drop transient packets according to SLA.

## Bad vs Good Implementations
### Naive Unbounded WebSocket Dispatch vs. Bounded Backpressure with Drain Lifecycle

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Calling `ws.send()` repeatedly without monitoring buffer size or implementing `drain()` allows slow network consumers to accumulate unbounded native memory. Because these allocations live in Bun's native uWebSockets layer rather than the JavaScript heap, standard GC cannot reclaim them, causing deterministic SIGKILL terminations under load.
```typescript
import { serve } from 'bun';

// ANTI-PATTERN: Fire-and-forget broadcasting without checking socket buffer
// Under high packet rates and slow consumer network links, native uWebSockets buffers
// grow without triggering JSC garbage collection, triggering an immediate OS OOM kill.
serve({
  port: 8080,
  fetch(req, server) {
    if (server.upgrade(req)) return;
    return new Response('Upgrade failed', { status: 400 });
  },
  websocket: {
    message(ws, message) {
      // Blindly echoing or fanning out heavy data
      const largePayload = new Uint8Array(64 * 1024);
      for (let i = 0; i < 1000; i++) {
        ws.send(largePayload); // Unbounded native buffer allocation!
      }
    },
  },
});
```

#### Production Standard (Correct)
> **Standard**: The production implementation guards every dispatch with `ws.getBufferedAmount()`. If a consumer falls behind and exceeds `MAX_BUFFERED_BYTES`, it is gracefully terminated with WebSocket code 1008 (Policy Violation) before native memory exhaustion occurs. The `drain` hook guarantees safe resumption when the client drains pending network packets below low-watermark thresholds.
```typescript
import { serve, type ServerWebSocket } from 'bun';

interface SocketData {
  id: string;
  isPaused: boolean;
}

const MAX_BUFFERED_BYTES = 512 * 1024; // 512 KB per connection limit

export const server = serve<SocketData>({
  port: 8080,
  fetch(req, server) {
    if (server.upgrade(req, { data: { id: crypto.randomUUID(), isPaused: false } })) {
      return;
    }
    return new Response('Upgrade failed', { status: 400 });
  },
  websocket: {
    open(ws: ServerWebSocket<SocketData>) {
      ws.data.isPaused = false;
    },
    message(ws: ServerWebSocket<SocketData>, message) {
      const buffered = ws.getBufferedAmount();

      // Hard backpressure protection: evict toxic slow consumer
      if (buffered > MAX_BUFFERED_BYTES) {
        console.warn(`[Slow Consumer] Terminating socket ${ws.data.id}: buffer exceeded ${MAX_BUFFERED_BYTES} bytes`);
        ws.close(1008, 'Buffer overflow: slow consumer evicted');
        return;
      }

      const payload = Buffer.from('telemetry-payload');

      // Soft backpressure handling: check before send
      if (buffered + payload.byteLength > MAX_BUFFERED_BYTES) {
        ws.data.isPaused = true;
        // Back off or drop non-critical transient frame
        return;
      }

      ws.send(payload);
    },
    drain(ws: ServerWebSocket<SocketData>) {
      // Native backpressure cleared: resume ingestion or unpause worker stream
      if (ws.data.isPaused && ws.getBufferedAmount() < MAX_BUFFERED_BYTES / 2) {
        ws.data.isPaused = false;
      }
    },
    close(ws: ServerWebSocket<SocketData>) {
      ws.data.isPaused = false;
    }
  },
});
```

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: Bun 1.2 Native WebSocket Backpressure Queue Bounding Guardrail

Bun 1.2 Native WebSocket Backpressure Queue Bounding Guardrail defines an industrial-grade behavioral standard for backend engineering. Protects high-throughput Bun edge event gateways from out-of-memory (OOM) kills caused by unbounded native C++ socket buffering. Enforces strict inspection of ws.getBufferedAmount(), mandatory drain callback wiring, and deterministic client eviction when slow consumer buffers exceed memory limits. 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 WebSocket Publishing Without Buffer Inspection or Drain Handler Synchronization 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 WebSocket Publishing Without Buffer Inspection or Drain Handler SynchronizationCritical
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 check `ws.getBufferedAmount()` before calling `ws.send()` on high-frequency unicast connections.

Engineers MUST ALWAYS check `ws.getBufferedAmount()` before calling `ws.send()` on high-frequency unicast connections.

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 fire-and-forget `ws.send()` loops without observing backpressure saturation bounds.

Engineers MUST NOT execute fire-and-forget `ws.send()` loops without observing backpressure saturation bounds.

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

Services MUST implement the `drain(ws)` lifecycle handler on `Bun.serve({ websocket

Services MUST implement the `drain(ws)` lifecycle handler on `Bun.serve({ websocket: { ... } })` whenever messages are buffered or throttled.

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

Code SHOULD enforce a maximum buffered amount threshold (e.g., 512KB to 1MB per socket) and terminate slow consumers using code `1008` (Policy Violation) if the buffer limit is exceeded.

Code SHOULD enforce a maximum buffered amount threshold (e.g., 512KB to 1MB per socket) and terminate slow consumers using code `1008` (Policy Violation) if the buffer limit is exceeded.

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

Publish/Subscribe fan-out via `server.publish(topic, data)` SHOULD monitor overall heap growth and avoid unbounded broadcasting to unverified subscriber states.

Publish/Subscribe fan-out via `server.publish(topic, data)` SHOULD monitor overall heap growth and avoid unbounded broadcasting to unverified subscriber states.

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

Components NEVER store unbounded in-memory retry queues per socket

Components NEVER store unbounded in-memory retry queues per socket; backpressure shedding must either pause upstream ingestion or drop transient packets according to SLA.

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 Bun 1.2 Native WebSocket Backpressure Queue Bounding Guardrail critical for AI coding assistants?

Protects high-throughput Bun edge event gateways from out-of-memory (OOM) kills caused by unbounded native C++ socket buffering. Enforces strict inspection of ws.getBufferedAmount(), mandatory drain callback wiring, and deterministic client eviction when slow consumer buffers exceed memory limits. 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 WebSocket Publishing Without Buffer Inspection or Drain Handler Synchronization) 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

Bun