DataLoadersenior8+ years

A teammate adds a `DataLoader` in front of an existing per-object resolver and reports the N+1 is fixed because "DataLoader caches now." Their dashboard still shows one query per customer at runtime. What did they actually fix, and what didn't they?

They fixed nothing about the call count for distinct keys, which is exactly what a dashboard tracking database queries would show. The lesson names this misconception directly: a DataLoader's cache only dedupes a key that two different resolvers in the same request both ask for — it does nothing to combine separate keys into one call. If twenty orders have twenty different customer ids, the cache never gets a repeat hit, because none of the twenty keys match any other key; every single one is a cache miss, and if the code behind it is still a per-key findById called once per miss, that's still twenty database round trips, just now routed through a DataLoader that never actually deduplicated anything. Batching — deferring keys across a level and dispatching once — is the part that turns N calls into one; a cache sitting in front of a still-per-key loader does not become batched just by being wrapped.

The lesson behind it →
More on DataLoader