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

Bun 1.2 Native WebSocket Kernel Backpressure and Buffer Draining Standard

Mandates explicit C-level memory buffer monitoring and drain-event orchestration for Bun native WebSockets. In high-throughput streaming pipelines, blind calls to ws.send() accumulate un-drained packets inside native uWebSockets C++ ring buffers outside the JavaScript runtime heap, evading V8/JSC GC thresholds and triggering silent OS OOM termination. This standard enforces watermarked backpressure control, producer pause/resume states, and bounded client disconnect thresholds.

Prohibited Anti-Pattern: Blind ws.send() invocation during high-throughput streaming without bufferedAmount tracking and drain lifecycle handling.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 Native WebSocket Kernel Backpressure and Buffer Draining Standard - Mandates explicit C-level memory buffer monitoring and drain-event orchestration for Bun native WebSockets. In high-throughput streaming pipelines, blind calls to ws.send() accumulate un-drained packets inside native uWebSockets C++ ring buffers outside the JavaScript runtime heap, evading V8/JSC GC thresholds and triggering silent OS OOM termination. This standard enforces watermarked backpressure control, producer pause/resume states, and bounded client disconnect thresholds.
globs: src/**/*.{ts,tsx}, server/**/*.{ts,tsx}, services/**/*.{ts,tsx}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-websocket-kernel-backpressure-standard"
---

# Bun 1.2 Native WebSocket Kernel Backpressure and Buffer Draining Standard

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Blind ws.send() invocation during high-throughput streaming without bufferedAmount tracking and drain lifecycle handling.

## System Constraints
You are a Principal Engineer enforcing Bun 1.2 Native WebSocket Kernel Backpressure Standard. Mandate invariants that eliminate unbounded C-level socket memory accumulation. Native Bun WebSockets rely on uWebSockets C++ buffers. When downstream consumers possess insufficient network throughput, data queued via `ws.send()` builds up within native memory allocations invisible to JavaScript garbage collection. You must enforce explicit backpressure logic using `ws.bufferedAmount`, the `drain(ws)` socket handler, high-watermark producer pausing, low-watermark producer resuming, and eviction of unrecoverable slow consumers.

## Strict Guidelines
- Engineers MUST ALWAYS check `ws.bufferedAmount` prior to writing payload buffers during continuous or high-throughput transmissions.
- Engineers MUST NOT continuously push data to a native Bun WebSocket if `ws.bufferedAmount` exceeds the configured HIGH_WATER_MARK (default: 1MB).
- Code SHOULD define a LOW_WATER_MARK (default: 256KB) and register the `drain(ws)` callback in `Bun.serve({ websocket: { drain } })` to signal producer resumption.
- Services MUST immediately terminate (`ws.close(1008, 'Buffer overflow')`) and clean up upstream producer listeners when `ws.bufferedAmount` breaches a hard safety ceiling (e.g., 16MB) to protect the host process from OS OOM killing.
- Components NEVER store unbounded outbound message arrays in JavaScript memory to bypass `ws.bufferedAmount`; queue bounds MUST be strictly enforced.

## Bad vs Good Implementations
### Naive high-throughput push causing native memory exhaustion vs Watermarked Drain Orchestration

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Calling ws.send() inside high-frequency listeners without inspecting ws.bufferedAmount pushes buffers directly into native uWebSockets C-layer queues. If a consumer's TCP window fills up, native heap memory grows without triggering garbage collection until the host system runs out of memory and kills the Bun process.
```typescript
import { serve } from "bun";

interface MarketTick {
  symbol: string;
  price: number;
  timestamp: number;
}

// Simulated high-throughput source (e.g. 50k ticks/sec)
declare const tickFeed: { subscribe(fn: (tick: MarketTick) => void): void };

serve({
  port: 8080,
  fetch(req, server) {
    server.upgrade(req);
    return undefined;
  },
  websocket: {
    message(ws, message) {},
    open(ws) {
      // CRITICAL VIOLATION: Subscribes unbounded producer directly to ws.send.
      // If client has a slow connection (e.g., 3G mobile), native C++ socket
      // queues expand indefinitely, triggering an uncatchable SIGKILL by the OS kernel.
      tickFeed.subscribe((tick) => {
        ws.send(JSON.stringify(tick));
      });
    },
  },
});
```

#### Production Standard (Correct)
> **Standard**: Implements an industrial-grade backpressure barrier: ws.bufferedAmount is evaluated on every outbound frame. If the socket exceeds the 1MB high-water mark, streaming pauses. The native websocket.drain hook detects when the C++ kernel buffer flushes below 256KB and resumes the stream. If a slow consumer breaches 16MB, it is evicted immediately before heap exhaustion occurs.
```typescript
import { serve, type ServerWebSocket } from "bun";

interface SocketState {
  isPaused: boolean;
  unsubscribe?: () => void;
}

interface MarketTick {
  symbol: string;
  price: number;
  timestamp: number;
}

declare const tickFeed: {
  subscribe(fn: (tick: MarketTick) => void): () => void;
};

const HIGH_WATER_MARK = 1024 * 1024;      // 1 MB: Pause producer
const LOW_WATER_MARK = 256 * 1024;        // 256 KB: Resume producer
const HARD_EVICT_LIMIT = 16 * 1024 * 1024; // 16 MB: Drop toxic slow client

serve<SocketState>({ 
  port: 8080,
  fetch(req, server) {
    const ok = server.upgrade(req, {
      data: { isPaused: false },
    });
    return ok ? undefined : new Response("Upgrade failed", { status: 400 });
  },
  websocket: {
    open(ws: ServerWebSocket<SocketState>) {
      ws.data.unsubscribe = tickFeed.subscribe((tick: MarketTick) => {
        // Guardrail 1: Hard ceiling protection against zombie / adversarial consumers
        if (ws.bufferedAmount >= HARD_EVICT_LIMIT) {
          ws.close(1008, "Kernel buffer capacity exceeded");
          return;
        }

        // Guardrail 2: Soft watermark throttling
        if (ws.bufferedAmount >= HIGH_WATER_MARK) {
          ws.data.isPaused = true;
          return; // Backpressure applied: drop or pause depending on domain semantics
        }

        if (!ws.data.isPaused) {
          ws.send(JSON.stringify(tick));
        }
      });
    },
    drain(ws: ServerWebSocket<SocketState>) {
      // Guardrail 3: Low-watermark recovery via native C-level drain callback
      if (ws.data.isPaused && ws.bufferedAmount <= LOW_WATER_MARK) {
        ws.data.isPaused = false;
      }
    },
    close(ws: ServerWebSocket<SocketState>) {
      if (ws.data.unsubscribe) {
        ws.data.unsubscribe();
        ws.data.unsubscribe = undefined;
      }
    },
    message(ws, msg) {}
  },
});
```

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 Kernel Backpressure and Buffer Draining Standard

Bun 1.2 Native WebSocket Kernel Backpressure and Buffer Draining Standard defines an industrial-grade behavioral standard for backend engineering. Mandates explicit C-level memory buffer monitoring and drain-event orchestration for Bun native WebSockets. In high-throughput streaming pipelines, blind calls to ws.send() accumulate un-drained packets inside native uWebSockets C++ ring buffers outside the JavaScript runtime heap, evading V8/JSC GC thresholds and triggering silent OS OOM termination. This standard enforces watermarked backpressure control, producer pause/resume states, and bounded client disconnect thresholds. 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: Blind ws.send() invocation during high-throughput streaming without bufferedAmount tracking and drain lifecycle handling. 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 DriftBlind ws.send() invocation during high-throughput streaming without bufferedAmount tracking and drain lifecycle handling.Critical
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.bufferedAmount` prior to writing payload buffers during continuous or high-throughput transmissions.

Engineers MUST ALWAYS check `ws.bufferedAmount` prior to writing payload buffers during continuous or high-throughput transmissions.

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 continuously push data to a native Bun WebSocket if `ws.bufferedAmount` exceeds the configured HIGH_WATER_MARK (default

Engineers MUST NOT continuously push data to a native Bun WebSocket if `ws.bufferedAmount` exceeds the configured HIGH_WATER_MARK (default: 1MB).

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

Code SHOULD define a LOW_WATER_MARK (default

Code SHOULD define a LOW_WATER_MARK (default: 256KB) and register the `drain(ws)` callback in `Bun.serve({ websocket: { drain } })` to signal producer resumption.

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

Services MUST immediately terminate (`ws.close(1008, 'Buffer overflow')`) and clean up upstream producer listeners when `ws.bufferedAmount` breaches a hard safety ceiling (e.g., 16MB) to protect the host process from OS OOM killing.

Services MUST immediately terminate (`ws.close(1008, 'Buffer overflow')`) and clean up upstream producer listeners when `ws.bufferedAmount` breaches a hard safety ceiling (e.g., 16MB) to protect the host process from OS OOM killing.

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

Components NEVER store unbounded outbound message arrays in JavaScript memory to bypass `ws.bufferedAmount`

Components NEVER store unbounded outbound message arrays in JavaScript memory to bypass `ws.bufferedAmount`; queue bounds MUST be strictly enforced.

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 Kernel Backpressure and Buffer Draining Standard critical for AI coding assistants?

Mandates explicit C-level memory buffer monitoring and drain-event orchestration for Bun native WebSockets. In high-throughput streaming pipelines, blind calls to ws.send() accumulate un-drained packets inside native uWebSockets C++ ring buffers outside the JavaScript runtime heap, evading V8/JSC GC thresholds and triggering silent OS OOM termination. This standard enforces watermarked backpressure control, producer pause/resume states, and bounded client disconnect thresholds. 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 (Blind ws.send() invocation during high-throughput streaming without bufferedAmount tracking and drain lifecycle handling.) 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