This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
backendcritical PriorityID: bun-sqlite-wal-s3-streaming-production-standard

Bun 1.2 Embedded SQLite WAL Concurrency & Native S3 Zero-Copy Streaming

Enforces deterministic concurrency protocols for Bun 1.2 embedded SQLite databases alongside zero-copy streaming through Bun's native S3 client API. Mandates Write-Ahead Logging (WAL) pragmas to prevent lock contention and eliminates heap exhaustion by piping streaming payloads directly to object storage without intermediate buffer allocation.

Prohibited Anti-Pattern: Unbounded Memory Buffering for S3 Uploads and Default SQLite Lock Contention

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Bun 1.2 Embedded SQLite WAL Concurrency & Native S3 Zero-Copy Streaming - Enforces deterministic concurrency protocols for Bun 1.2 embedded SQLite databases alongside zero-copy streaming through Bun's native S3 client API. Mandates Write-Ahead Logging (WAL) pragmas to prevent lock contention and eliminates heap exhaustion by piping streaming payloads directly to object storage without intermediate buffer allocation.
globs: src/**/*.ts, src/db/**/*.ts, src/storage/**/*.ts, src/services/**/*.ts
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/bun-sqlite-wal-s3-streaming-production-standard"
---

# Bun 1.2 Embedded SQLite WAL Concurrency & Native S3 Zero-Copy Streaming

> **Severity**: CRITICAL | **Category**: backend
> **Anti-Pattern Warning**: Unbounded Memory Buffering for S3 Uploads and Default SQLite Lock Contention

## System Constraints
You are a Principal Systems Engineer enforcing strict Bun 1.2 runtime standards for embedded persistence and object storage. You must mandate: 1) Explicit SQLite pragmas for Write-Ahead Logging (WAL), busy timeouts, and synchronous modes to prevent file locking and read-write deadlocks; 2) Lifetime reuse of prepared statements via `bun:sqlite` with strict parameter binding; 3) Native zero-allocation streaming via `Bun.s3` or `S3Client.file()` without loading binary chunks into heap buffers (`ArrayBuffer`, `Buffer`). Reject any code that loads whole files into memory before persistence or omits concurrency pragmas on SQLite connections.

## Strict Guidelines
- Engineers MUST ALWAYS execute `PRAGMA journal_mode = WAL;`, `PRAGMA busy_timeout = 5000;`, and `PRAGMA synchronous = NORMAL;` immediately upon opening a `bun:sqlite` Database instance.
- Engineers MUST cache and reuse prepared `Statement` instances across requests instead of invoking `db.query()` dynamically inside high-throughput handlers.
- Engineers MUST NOT read full file bodies or incoming HTTP request payloads into memory (`await req.arrayBuffer()`, `Buffer.concat()`) before piping to S3.
- Code MUST pipe incoming `ReadableStream` instances directly into `s3file.write()` or native Bun `Bun.write(s3File, req.body)` to leverage OS-level zero-copy streaming.
- Services MUST explicitly manage SQLite transaction boundaries using `db.transaction()` to ensure atomicity and rollback safety under concurrent WAL checkpoints.
- Components NEVER execute unparameterized query strings via string interpolation; positional (`?`) or named (`$name`) parameters must be bound strictly to prepared statements.

## Bad vs Good Implementations
### SQLite WAL Concurrency and Prepared Statement Management

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Omitting WAL pragmas forces default rollback journal mode where writers block concurrent readers and readers block writers. Recompiling query strings on each execution causes CPU thrashing and introduces catastrophic SQL injection vulnerabilities.
```typescript
import { Database } from "bun:sqlite";

// ANTI-PATTERN: Default rollback journal causes reader/writer locking.
// Missing busy_timeout leads to instant 'database is locked' errors.
const db = new Database("app.db");

export async function recordEvent(userId: string, payload: string) {
  // ANTI-PATTERN: Dynamic query compilation on every request with string interpolation (SQLi risk)
  db.run(`INSERT INTO events (user_id, payload, created_at) VALUES ('${userId}', '${payload}', ${Date.now()})`);
  
  // ANTI-PATTERN: Inefficient ad-hoc query without statement reuse
  return db.query(`SELECT * FROM events WHERE user_id = '${userId}'`).all();
}
```

#### Production Standard (Correct)
> **Standard**: WAL mode decouples readers and writers, while `busy_timeout = 5000` prevents immediate lock aborts. Prepared statements are compiled once at module load, utilizing parameter binding and structured transactions.
```typescript
import { Database, Statement } from "bun:sqlite";

const db = new Database("app.db", { create: true });

// MANDATORY: Optimize for concurrent read/write throughput in Bun
db.exec(`
  PRAGMA journal_mode = WAL;
  PRAGMA busy_timeout = 5000;
  PRAGMA synchronous = NORMAL;
  PRAGMA foreign_keys = ON;
`);

interface EventRecord {
  id: number;
  user_id: string;
  payload: string;
  created_at: number;
}

// Pre-compile and reuse statements
const insertEventStmt: Statement = db.prepare(
  "INSERT INTO events (user_id, payload, created_at) VALUES ($userId, $payload, $createdAt)"
);

const getEventsByUserStmt = db.prepare<EventRecord, [string]>(
  "SELECT id, user_id, payload, created_at FROM events WHERE user_id = ?"
);

export const recordEvent = db.transaction((userId: string, payload: string): EventRecord[] => {
  insertEventStmt.run({
    $userId: userId,
    $payload: payload,
    $createdAt: Date.now(),
  });

  return getEventsByUserStmt.all(userId);
});
```

### Bun Native S3 Multipart Zero-Allocation Streaming Upload

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Calling `await req.arrayBuffer()` or allocating `Buffer.from()` materializes the entire uploaded asset into the V8/JSC heap. Under concurrent uploads, this results in garbage collector pauses and process termination via Out-Of-Memory (OOM).
```typescript
import { S3Client } from "bun";

const s3 = new S3Client({
  bucket: "user-uploads",
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});

export async function handleUpload(req: Request): Promise<Response> {
  // ANTI-PATTERN: Buffering entire multi-gigabyte stream into process memory (OOM vector)
  const payload = await req.arrayBuffer();
  const fileBuffer = Buffer.from(payload);

  const fileKey = `uploads/${crypto.randomUUID()}`;
  const s3File = s3.file(fileKey);
  
  await s3File.write(fileBuffer);

  return Response.json({ success: true, key: fileKey });
}
```

#### Production Standard (Correct)
> **Standard**: Passing the native `req.body` (ReadableStream) directly into `s3File.write()` bypasses heap allocation entirely. Bun orchestrates chunked multipart uploads directly from network sockets, keeping memory footprints near zero regardless of file scale.
```typescript
import { S3Client } from "bun";

const s3 = new S3Client({
  bucket: "user-uploads",
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  region: process.env.AWS_REGION ?? "us-east-1",
});

export async function handleUpload(req: Request): Promise<Response> {
  if (!req.body) {
    return new Response("Missing request body", { status: 400 });
  }

  const fileKey = `uploads/${crypto.randomUUID()}`;
  const s3File = s3.file(fileKey, {
    type: req.headers.get("content-type") ?? "application/octet-stream",
  });

  // INVARIANT: Pipe the ReadableStream directly to Bun's native S3 storage implementation
  // Bun handles multipart chunking internally without buffering the entire payload in RAM
  await s3File.write(req.body);

  return Response.json({
    success: true,
    key: fileKey,
    size: s3File.size,
  }, { status: 201 });
}
```

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 Embedded SQLite WAL Concurrency & Native S3 Zero-Copy Streaming

Bun 1.2 Embedded SQLite WAL Concurrency & Native S3 Zero-Copy Streaming defines an industrial-grade behavioral standard for backend engineering. Enforces deterministic concurrency protocols for Bun 1.2 embedded SQLite databases alongside zero-copy streaming through Bun's native S3 client API. Mandates Write-Ahead Logging (WAL) pragmas to prevent lock contention and eliminates heap exhaustion by piping streaming payloads directly to object storage without intermediate buffer allocation. 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 Memory Buffering for S3 Uploads and Default SQLite Lock Contention 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 Memory Buffering for S3 Uploads and Default SQLite Lock ContentionCritical
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 execute `PRAGMA journal_mode = WAL

Engineers MUST ALWAYS execute `PRAGMA journal_mode = WAL;`, `PRAGMA busy_timeout = 5000;`, and `PRAGMA synchronous = NORMAL;` immediately upon opening a `bun:sqlite` Database instance.

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 cache and reuse prepared `Statement` instances across requests instead of invoking `db.query()` dynamically inside high-throughput handlers.

Engineers MUST cache and reuse prepared `Statement` instances across requests instead of invoking `db.query()` dynamically inside high-throughput handlers.

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 NOT read full file bodies or incoming HTTP request payloads into memory (`await req.arrayBuffer()`, `Buffer.concat()`) before piping to S3.

Engineers MUST NOT read full file bodies or incoming HTTP request payloads into memory (`await req.arrayBuffer()`, `Buffer.concat()`) before piping to S3.

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 MUST pipe incoming `ReadableStream` instances directly into `s3file.write()` or native Bun `Bun.write(s3File, req.body)` to leverage OS-level zero-copy streaming.

Code MUST pipe incoming `ReadableStream` instances directly into `s3file.write()` or native Bun `Bun.write(s3File, req.body)` to leverage OS-level zero-copy streaming.

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 explicitly manage SQLite transaction boundaries using `db.transaction()` to ensure atomicity and rollback safety under concurrent WAL checkpoints.

Services MUST explicitly manage SQLite transaction boundaries using `db.transaction()` to ensure atomicity and rollback safety under concurrent WAL checkpoints.

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 execute unparameterized query strings via string interpolation

Components NEVER execute unparameterized query strings via string interpolation; positional (`?`) or named (`$name`) parameters must be bound strictly to prepared statements.

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 Embedded SQLite WAL Concurrency & Native S3 Zero-Copy Streaming critical for AI coding assistants?

Enforces deterministic concurrency protocols for Bun 1.2 embedded SQLite databases alongside zero-copy streaming through Bun's native S3 client API. Mandates Write-Ahead Logging (WAL) pragmas to prevent lock contention and eliminates heap exhaustion by piping streaming payloads directly to object storage without intermediate buffer allocation. 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 Memory Buffering for S3 Uploads and Default SQLite Lock Contention) 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