Interview frequency: …
Short answer
It occurs when one main query is followed by N queries, one per record, to fetch related data; DataLoader batches and caches requests into a unified query.
Explanation
This is common in nested GraphQL and REST APIs. DataLoader uses Node.js microtasks: if loader.load(id) is called several times during the same Event Loop turn, it collects the IDs into an array and calls one batchFunction([1, 2, 3]) to run a query such as SELECT WHERE id IN (...).
Code example
import DataLoader from "dataloader";
const userLoader = new DataLoader(async (ids) => {
const users = await db.query("SELECT * FROM users WHERE id = ANY($1)", [ids]);
return ids.map(id => users.find(u => u.id === id));
});Common mistakes
- Reusing the same DataLoader instance across requests from different users, which can leak cached data.