📁 Sangam Dhakal - Portfolio v2.0 - Netscape Navigator 4.0
_
□
×

📚 Interview question

Your EF Core app's memory and CPU keep growing, and the query cache holds thousands of entries. What causes query cache pollution and how do you fix it?

Senior Theory Advanced

🎯 Key points

  • Constants in the expression tree are part of the EF Core cache key
  • Closure-captured values become SQL parameters: one cache entry, one plan
  • Dynamic expression builders must use parameters, not Expression.Constant
  • Impact: translation CPU per request, memory growth, cache churn
  • Also floods SQL Server's plan cache with single-use plans
  • Watch the query cache hit rate in EF Core event counters

⏱ 30-second answer

EF Core's query cache key includes constants in the expression tree, so queries built with Expression.Constant(userValue), string-built dynamic LINQ, or EF.Constant on varying values create a new cache entry and a new translation for every value. The impact: CPU spent translating on every request instead of once, memory growing with cached entries (until the cache's size limit evicts them, causing more re-translation), and on SQL Server a matching flood of single-use plans in the plan cache. Fix it by capturing values in closures (they become SQL parameters), building dynamic predicates with parameter expressions, avoiding EF.Constant for high-cardinality values, and confirming with the query cache hit rate in dotnet-counters monitor Microsoft.EntityFrameworkCore.

🔍 What the interviewer is checking

Checks they know constants in expression trees create a cache entry per value

↩ Usually asked after

More in EF Core Performance

Last updated 23 Nov 2026.

🌐 Sangam's Webring

⏪ Previous 🏠 Main Next ⏩
💬 Chat with me