PART 1 · VISUAL CONCEPT 02

The N+1 Problem & DataLoader Tick Batching

See how a single nested list field explodes into 1 + N database roundtrips — and how DataLoader collapses them into one SQL IN(...) batch inside a single event-loop tick.

Interactive Lab 01 · Event-Loop Tick Batching

Naive Resolver (1 + N) vs. DataLoader Tick Batching

2 Queries (-71%)
Total SQL Queries 2 Queries (-71%) 1 posts + 1 batched IN(...)
DB Roundtrips & Pool Load 2 Trips · 1 Conn Pool util: 10% (1/10 conns)
Request Cache Hits 3 Hits (Deduped) 6 resolvers → 3 unique keys
Simulated DB Latency 30 ms 14ms posts + 16ms batch
Live SQL Wire Log 2 Queries Fired
Key-Order Alignment Contract
Interactive Lab 02 · Security & Memory Isolation

Request-Scoped Cache vs. Global Singleton Leak Sandbox

SAFE · Isolated Per Request
Retained Cache Keys: 0 (GC'd)
Live Request & Mutation Timeline DB u1.role = "Admin"
Active Request DataLoader Cache GC'd After Response
Mental Model · 28 Lines of Pure TypeScript

MiniDataLoader.ts — Tick Batching & Key Alignment

MiniDataLoader.ts

      

Tick Batching ≠ Redis

DataLoader batches calls made within one JS event-loop tick (queueMicrotask) and deduplicates keys within one HTTP request—it is not a cross-request cache.

Order & Length Contract

SQL WHERE id IN (...) returns rows in arbitrary order. Always return keys.map(k => byId.get(k) ?? null) so indices and lengths match 1:1.

Instantiate Per-Request

Construct new DataLoader(...) inside your GraphQL context() factory on every request to prevent cross-user data leaks and unbounded memory growth.