This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: bun-zero-alloc-ring-buffer-tls-pooling

Zero-Allocation Ring-Buffer Arena Pooling for Bun TLS/WebSocket Sockets

Enforces pre-allocated, contiguous SharedArrayBuffer ring-buffer memory arenas recycled across Bun 1.2 TLS WebSocket connections. Prevents V8/JSC major GC pauses under sustained million-connection loads by eliminating per-packet heap allocations, slicing, and transient Buffer creation in high-throughput socket I/O loops.

Prohibited Anti-Pattern: Allocating transient Buffers or sub-slices per incoming TLS/WebSocket frame, inducing catastrophic GC stop-the-world spikes under load.

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Zero-Allocation Ring-Buffer Arena Pooling for Bun TLS/WebSocket Sockets - Enforces pre-allocated, contiguous SharedArrayBuffer ring-buffer memory arenas recycled across Bun 1.2 TLS WebSocket connections. Prevents V8/JSC major GC pauses under sustained million-connection loads by eliminating per-packet heap allocations, slicing, and transient Buffer creation in high-throughput socket I/O loops.
globs: src/**/*.{ts,tsx}, server/**/*.{ts,tsx}
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-zero-alloc-ring-buffer-tls-pooling"
---

# Zero-Allocation Ring-Buffer Arena Pooling for Bun TLS/WebSocket Sockets

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Allocating transient Buffers or sub-slices per incoming TLS/WebSocket frame, inducing catastrophic GC stop-the-world spikes under load.

## System Constraints
You are a Principal Systems Engineer enforcing strict zero-allocation networking patterns in Bun 1.2. Under high-throughput TLS WebSocket workloads, all incoming and outgoing frames must reuse contiguous ring-buffer arenas from a static pre-allocated pool. Forbid Buffer.from, Buffer.concat, Uint8Array.prototype.slice, and closure-allocated payloads inside hot read/write loops. Mandate subarray slicing with zero-copy views, explicit connection lifecycle arena checkouts/returns, and strict adherence to Bun's native streaming APIs.

## Strict Guidelines
- Engineers MUST ALWAYS initialize a fixed-size SlabPool of ArrayBuffers or SharedArrayBuffers at application boot to service all TLS socket read/write loops.
- Engineers MUST NEVER invoke `Buffer.from()`, `Buffer.alloc()`, `Buffer.concat()`, or `.slice()` within the `message`, `data`, or `drain` event cycles.
- Engineers MUST ALWAYS use `.subarray()` over `.slice()` to guarantee zero-copy typed view projection without triggering heap allocations.
- Engineers MUST ALWAYS return leased ring-buffer slices to the arena pool upon WebSocket `close` or payload processing completion.
- Services MUST enforce backpressure using `ws.getBufferedAmount()` and the native Bun `drain` hook before pulling additional frames into the ring-buffer.

## Bad vs Good Implementations
### Per-Message Heap Allocation vs Zero-Allocation Ring-Buffer Slab Leases

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Creates multiple transient Buffer allocations per message (`Buffer.from`, `.slice()`, and `Buffer.concat()`). Under 100,000+ concurrent persistent connections, this exhausts young-generation GC spaces and triggers latency-destroying stop-the-world GC pauses.
```typescript
import type { ServerWebSocket } from "bun";

// Anti-pattern: Allocating dynamic buffers on every inbound frame
export const websocketHandler = {
  message(ws: ServerWebSocket<any>, message: string | Buffer) {
    // Dynamic allocation: Buffer.from copies payload into newly allocated heap memory
    const payload = Buffer.isBuffer(message) ? message : Buffer.from(message);
    
    // Slice triggers secondary allocations in the JavaScript heap
    const header = payload.slice(0, 4);
    const body = payload.slice(4);
    
    const ack = Buffer.concat([Buffer.from([0xAA]), header]);
    ws.send(ack);
  }
};
```

#### Production Standard (Correct)
> **Standard**: Pre-allocates a contiguous memory slab at boot. Connection I/O borrows a slot index, copies bytes into a fixed offset, and performs frame operations using `.subarray()`. No dynamic objects or transient buffers are allocated on the JavaScript heap during message ingestion.
```typescript
import type { ServerWebSocket } from "bun";

interface SocketContext {
  arenaIndex: number;
}

class SlabRingBufferPool {
  private pool: Uint8Array;
  private freeSlots: number[];
  public readonly slotSize: number;

  constructor(slots: number, slotSize: number) {
    this.slotSize = slotSize;
    // Allocate one contiguous block off-heap / contiguous memory
    this.pool = new Uint8Array(new SharedArrayBuffer(slots * slotSize));
    this.freeSlots = Array.from({ length: slots }, (_, i) => i);
  }

  acquire(): number | null {
    return this.freeSlots.pop() ?? null;
  }

  release(index: number): void {
    this.freeSlots.push(index);
  }

  getView(index: number, length: number): Uint8Array {
    const offset = index * this.slotSize;
    return this.pool.subarray(offset, offset + length);
  }
}

const RING_POOL = new SlabRingBufferPool(65536, 4096);

export const websocketHandler = {
  open(ws: ServerWebSocket<SocketContext>) {
    const slot = RING_POOL.acquire();
    if (slot === null) {
      ws.close(1013, "Server Busy: No Slab Available");
      return;
    }
    ws.data = { arenaIndex: slot };
  },
  message(ws: ServerWebSocket<SocketContext>, message: ArrayBuffer | Uint8Array) {
    const { arenaIndex } = ws.data;
    const inputBytes = message instanceof Uint8Array ? message : new Uint8Array(message);
    
    if (inputBytes.byteLength > RING_POOL.slotSize) {
      ws.close(1009, "Payload Exceeds Slab Limit");
      return;
    }

    const arenaView = RING_POOL.getView(arenaIndex, inputBytes.byteLength);
    arenaView.set(inputBytes);

    // Zero-allocation header dissection via zero-copy subarray views
    const header = arenaView.subarray(0, 4);
    
    // Send using zero-copy view, verified against backpressure limits
    if (ws.getBufferedAmount() < 64 * 1024) {
      ws.send(header);
    }
  },
  close(ws: ServerWebSocket<SocketContext>) {
    if (ws.data?.arenaIndex !== undefined) {
      RING_POOL.release(ws.data.arenaIndex);
    }
  }
};
```

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: Zero-Allocation Ring-Buffer Arena Pooling for Bun TLS/WebSocket Sockets

Zero-Allocation Ring-Buffer Arena Pooling for Bun TLS/WebSocket Sockets defines an industrial-grade behavioral standard for backend engineering. Enforces pre-allocated, contiguous SharedArrayBuffer ring-buffer memory arenas recycled across Bun 1.2 TLS WebSocket connections. Prevents V8/JSC major GC pauses under sustained million-connection loads by eliminating per-packet heap allocations, slicing, and transient Buffer creation in high-throughput socket I/O loops. 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: Allocating transient Buffers or sub-slices per incoming TLS/WebSocket frame, inducing catastrophic GC stop-the-world spikes under load. 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 DriftAllocating transient Buffers or sub-slices per incoming TLS/WebSocket frame, inducing catastrophic GC stop-the-world spikes under load.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 initialize a fixed-size SlabPool of ArrayBuffers or SharedArrayBuffers at application boot to service all TLS socket read/write loops.

Engineers MUST ALWAYS initialize a fixed-size SlabPool of ArrayBuffers or SharedArrayBuffers at application boot to service all TLS socket read/write loops.

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 NEVER invoke `Buffer.from()`, `Buffer.alloc()`, `Buffer.concat()`, or `.slice()` within the `message`, `data`, or `drain` event cycles.

Engineers MUST NEVER invoke `Buffer.from()`, `Buffer.alloc()`, `Buffer.concat()`, or `.slice()` within the `message`, `data`, or `drain` event cycles.

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 ALWAYS use `.subarray()` over `.slice()` to guarantee zero-copy typed view projection without triggering heap allocations.

Engineers MUST ALWAYS use `.subarray()` over `.slice()` to guarantee zero-copy typed view projection without triggering heap allocations.

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

Engineers MUST ALWAYS return leased ring-buffer slices to the arena pool upon WebSocket `close` or payload processing completion.

Engineers MUST ALWAYS return leased ring-buffer slices to the arena pool upon WebSocket `close` or payload processing completion.

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 enforce backpressure using `ws.getBufferedAmount()` and the native Bun `drain` hook before pulling additional frames into the ring-buffer.

Services MUST enforce backpressure using `ws.getBufferedAmount()` and the native Bun `drain` hook before pulling additional frames into the ring-buffer.

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 Zero-Allocation Ring-Buffer Arena Pooling for Bun TLS/WebSocket Sockets critical for AI coding assistants?

Enforces pre-allocated, contiguous SharedArrayBuffer ring-buffer memory arenas recycled across Bun 1.2 TLS WebSocket connections. Prevents V8/JSC major GC pauses under sustained million-connection loads by eliminating per-packet heap allocations, slicing, and transient Buffer creation in high-throughput socket I/O loops. 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 (Allocating transient Buffers or sub-slices per incoming TLS/WebSocket frame, inducing catastrophic GC stop-the-world spikes under load.) 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

Zero-Allocation