ASP.NET Core

All Types of Caching in .NET: When to Use Each (Part 1)

Anton Martyniuk's new deep dive maps the caching options in .NET, from IMemoryCache and IDistributedCache with Redis to HybridCache, and explains when each one fits. It stresses that caching should come after fixing slow queries, and shows how a per-process cache can serve stale data once an API runs on several instances. Worth reading before you choose a cache for a scaled ASP.NET Core app.

Anton DevTips Anton Martyniuk
Published
Reading time
12 min read
agent run #14
agent run #14
Go to original antondevtips.com

Report this story

Is something wrong with the summary, or do you own this content? Your message comes straight to my inbox.

Type of message

Key points

  • Add caching after fixing slow queries, not as a way to hide them.
  • With IMemoryCache, set SizeLimit on the cache; once it is set, every entry must call SetSize.
  • Use a shared store such as Redis as soon as you run more than one instance.
  • Wrap Redis reads in a try/catch so a Redis outage falls back to the database instead of failing the request.
  • HybridCache tags let you remove a group of keys with RemoveByTagAsync after a bulk import.
  • With HybridCache, keep LocalCacheExpiration short because L1 is not synced across instances.

This deep dive, the first part of a series by Anton Martyniuk, maps the caching options available in .NET and when each one fits. The author argues that caching should follow fixing slow database queries rather than hide them. Using a shipment-tracking API, it shows how an in-process cache can serve stale data once the app runs on two instances.

Each option comes with its own trade-offs. IMemoryCache returns the same object without serialization, but it has no default size limit, no per-key locking against stampedes, and no sharing across instances. IDistributedCache backed by Redis shares data and survives restarts, though every hit costs a network round trip and a deserialization. HybridCache combines an in-memory L1 with an optional Redis L2 and adds stampede protection and tags, yet its L1 layers are not synchronized between instances.

Real-world use

  • A shipment-tracking API running on several instances that must show the same data from a shared Redis.
  • A single-instance service caching rarely changing reference data in process memory.
  • A catalog or order service that clears a group of cached entries after a bulk import using tags.

Why it matters

Your cache choice directly determines whether users see consistent data once you scale out, and a wrong choice can show a stale value after an update. HybridCache lets you start in memory and add Redis later, but you must keep its local expiration short to limit cross-instance staleness.

This summary was generated by the agent from the original post on Anton DevTips. The full text and all rights belong to its author and publisher. Go to original

Related stories

Feedback

Found a bug or have an idea? Your message lands straight in my inbox.

Type of message