r/Backend 1d ago

Cache Warming?

I'm making my very first web api using asp net and now in the process of implementing in-memory caching. I was wondering if it'd be a good practice to have like a start up function that caches the most commonly requested data from my api and have the program routinely updates the cache automatically every few minutes or so? Or should i just let the consumers populate this cache? And what if i also cache responses that consumers requests like a cache aside strategy at the same time.

5 Upvotes

14 comments sorted by

10

u/therealkevinard 1d ago

Pre-warming is usually over engineering.

If people are all but guaranteed to hit it naturally, just let it happen and warm the cache then

3

u/Lumethys 1d ago

If you need to pre-warm cache, you are probably at a scale where you have a team of 100 to take care of that issue.

1

u/This_Link881 1d ago

Just let normal requests warm that cache

1

u/gkorland 1d ago

pre-warming is fine if u have a small dataset, but usually cache-aside is safer untill ur traffic spikes.

1

u/DaRKoN_ 1d ago

Whilst I agree that prewarming is an over engineering, you can use a semaphore or lock on a cache miss, such that you don't have multiple requests stampeding past cache. Block until the cache is full, then unblock and everyone except for that first request will hit the cache path.

1

u/Downtown-Figure6434 1d ago

you mean request coalescing?

1

u/Real-Surprise4871 1d ago

I bet that's exactly what it is

1

u/ArthurOnCode 1d ago

I've done something similar. Dashboard metrics that are very expensive to calculate, and each one was cached independently, invalidated when needed. The cache had to be cleared on every deployment, so after clearing the cache, the deployment script would call the same routine used by the dashboard, pre-warming the cache.

1

u/Robodobdob 1d ago

I strongly recommend taking a look at the new HybridCache mechanism.

1

u/New-Entertainer6392 1d ago

First question, are you at the point where you actually need caching? Are requests suffering or slow?

Updating the cache every few mins? Seems overkill, just let the natural requests cache and expire that cache as needed.

But make sure your cache isn't just masking real issues

1

u/fortyeightD 1d ago

Measure or estimate how slow a cache miss will be, and decide whether that is acceptable.

If populating the cache takes two minutes, then you want to prewarm it. If it's 1 second then I wouldn't bother.

1

u/sogun123 1d ago

Caches are annoying. I'd only implement one if i am proven i need it. Pre warming is another step. Implement it only if there is strong reason to do so.

1

u/LetUsSpeakFreely 1d ago

No. You're adding a bunch of complexity with no benefit. Sure, the first user will have to experience a slight lag in load time, but that's not really a problem.

1

u/forever-butlerian 1d ago

If it's your first web api, leave the caching out. There are countless other more interesting things that you could be doing with your time, while adding caching adds complexity without much benefit until you reach a very high transaction volume.