They answer two questions: who loads the cache on a read, and what happens to the cache on a write?
.NET 10 How to implement it
| Strategy | Library / API | Notes |
| Cache-aside / read-through | HybridCache.GetOrCreateAsync | Checks the cache, runs your factory on a miss, then stores the result. Effectively read-through |
| Write-through | HybridCache.SetAsync after the DB write | Write the DB and cache in the same code path |
| Write-behind | built-in System.Threading.Channels + BackgroundService | Queue writes in memory and flush them to the DB in batches. Accept the data-loss risk, or use a durable queue |
| Refresh-ahead | ZiggyCreatures.FusionCache (EagerRefreshThreshold) | Refreshes hot keys in the background before they expire. FusionCache also adds fail-safe: it serves stale data if the DB is down |
public sealed class ProductService(HybridCache cache, AppDbContext db)
{
// Cache-aside: on a miss, the factory runs once and the result is cached
public ValueTask<Product?> GetAsync(int id, CancellationToken ct) =>
cache.GetOrCreateAsync($"product:{id}",
async token => await db.Products.FindAsync([id], token),
cancellationToken: ct);
public async Task UpdateAsync(Product product, CancellationToken ct)
{
db.Products.Update(product);
await db.SaveChangesAsync(ct); // 1. write the DB
await cache.RemoveAsync($"product:{product.Id}", ct); // 2. then delete the key
}
}
6 caching strategies every engineer should know 🧠
1️⃣ Cache-aside: app checks the cache, loads from the DB on a miss
2️⃣ Read-through: cache loads from the DB for you
3️⃣ Write-through: write cache + DB together
4️⃣ Write-behind: write cache now, DB later (fast but risky)
5️⃣ Write-around: writes skip the cache
6️⃣ Refresh-ahead: refresh hot keys before they expire
Which one runs your system? 👇 #SystemDesign #Caching #Redis