Architectural Invariant Design: Bun 1.2 Native WebSocket Hibernation & Backpressure Drainage Policy
Bun 1.2 Native WebSocket Hibernation & Backpressure Drainage Policy defines an industrial-grade behavioral standard for backend engineering. Enforces Bun 1.2 native WebSocket server invariants by delegating idle connection state to the native C++ event loop via WebSocket Hibernation and strictly handling TCP backpressure during packet transmission. Prevents V8 heap exhaustion and silent message dropping under high-concurrency loads (100k+ concurrent connections) by forbidding JavaScript closure retention on socket instances and requiring drain-driven writes. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.
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 Mode | AI Symptom (The Anti-Pattern) | Severity | Production Impact & Remedy |
|---|---|---|---|
| AI Hallucination & Deprecation Drift | Retaining JavaScript WebSocket References in Userland State and Ignoring ws.send() Backpressure Return Codes | 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 Inconsistency | AI 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. |
Step-by-Step Production Directives
Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:
Engineers MUST ALWAYS configure `Bun.serve` with native `websocket` handler hooks (`open`, `message`, `close`, `drain`) rather than instantiating non-native third-party WebSocket libraries.
Engineers MUST ALWAYS configure `Bun.serve` with native `websocket` handler hooks (`open`, `message`, `close`, `drain`) rather than instantiating non-native third-party WebSocket libraries.
Engineers MUST NOT store `ServerWebSocket` object references in userland collections (e.g., `new Map<string, ServerWebSocket>()`, `new Set()`, arrays, or object properties), as doing so prevents the Bun C++ runtime from sweeping the V8 wrapper and hibernating the socket.
Engineers MUST NOT store `ServerWebSocket` object references in userland collections (e.g., `new Map<string, ServerWebSocket>()`, `new Set()`, arrays, or object properties), as doing so prevents the Bun C++ runtime from sweeping the V8 wrapper and hibernating the socket.
Engineers MUST ALWAYS pass connection state through the `data` property of `server.upgrade(req, { data
Engineers MUST ALWAYS pass connection state through the `data` property of `server.upgrade(req, { data: { ... } })`, typing the socket using `ServerWebSocket<SocketDataType>`.
Engineers MUST ALWAYS evaluate the integer return value of `ws.send(data)`
Engineers MUST ALWAYS evaluate the integer return value of `ws.send(data)`: if `ws.send()` returns `0` (dropped) or indicates backpressure (bytes queued exceeds threshold via `ws.getBufferedAmount()`), writes MUST be suspended until the native `drain(ws)` hook executes.
Services MUST implement a per-socket outbound chunk queue attached to `ws.data` that is flushed exclusively within the `drain(ws)` lifecycle callback.
Services MUST implement a per-socket outbound chunk queue attached to `ws.data` that is flushed exclusively within the `drain(ws)` lifecycle callback.
Engineers MUST ALWAYS use native Bun topic subscription (`ws.subscribe(topic)` and `server.publish(topic, message)`) for broadcasting, completely avoiding manual iteration over userland socket sets.
Engineers MUST ALWAYS use native Bun topic subscription (`ws.subscribe(topic)` and `server.publish(topic, message)`) for broadcasting, completely avoiding manual iteration over userland socket sets.
Frequently Asked Technical Questions
Common questions developers face when configuring AI coding assistants for this stack:
Q:Why is Bun 1.2 Native WebSocket Hibernation & Backpressure Drainage Policy critical for AI coding assistants?
Enforces Bun 1.2 native WebSocket server invariants by delegating idle connection state to the native C++ event loop via WebSocket Hibernation and strictly handling TCP backpressure during packet transmission. Prevents V8 heap exhaustion and silent message dropping under high-concurrency loads (100k+ concurrent connections) by forbidding JavaScript closure retention on socket instances and requiring drain-driven writes. 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 (Retaining JavaScript WebSocket References in Userland State and Ignoring ws.send() Backpressure Return Codes) 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.