← GraphQL learning path
GraphQL · Module 6 of 10

The N+1 problem and DataLoader

Batch and cache database calls for nested queries.

📖 Key ideas

  1. Nested fields can trigger one query per item: the N+1 problem.
  2. DataLoader batches many loads into one query per tick.
  3. It also caches results within a request.
  4. Create loaders per request in the context.
  5. Measure with query logging.
💡 Real-life example: DataLoader is a delivery boy who collects all orders in the lane before going to the shop once.
N+1 problemOne query for a list plus one per item
DataLoaderBatching and caching utility
BatchingCombining many requests into one
Per-request cacheCache cleared after each request