L1 required for stampede protection, and validation of setup #326
-
Hi there 👋 Thanks a lot for this package, we're currently evaluating it, and it looks great. We're currently using raw L1 required for stampede protectionI saw in this discussion and in the XML comments of the I tried to use The discussion linked above mentioned to use L1 and set a short duration, so stampede protection is enabled, while most of the time the data will be fetched with L2. We think we achieved this with the following: var fusionCacheBuilder = builder.Services.AddFusionCache()
.WithSystemTextJsonSerializer()
.WithRegisteredDistributedCache(ignoreMemoryDistributedCache: !builder.Environment.IsDevelopment())
.WithDefaultEntryOptions(new FusionCacheEntryOptions()
{
// Memory cache duration
Duration = TimeSpan.FromSeconds(1),
}); Then, when interacting with return await fusionCache.GetOrSetAsync(
"MyCacheKey",
async _ => await RunFactory(),
options => options.SetDistributedCacheDuration(TimeSpan.FromMinutes(5))); My understanding of the following setup is:
Could you please confirm whether that's accurate? Special case during local dev with no real distributed cacheDuring local dev, we register the in-memory distributed cache, as we want cached data to be purged across application restarts. We seemed to face an issue initially where because both L1 and L2 used the same One workaround we found is to register a separate if (builder.Environment.IsDevelopment())
{
// Use a different memory cache implementation when in local dev so the shorter
// memory cache duration doesn't wipe out the distributed cache.
const string localDevFusionCacheMemoryCacheKey = "LocalDevFusionCacheMemoryCache";
builder.Services.AddKeyedSingleton<IMemoryCache, MemoryCache>(localDevFusionCacheMemoryCacheKey);
fusionCacheBuilder.WithRegisteredKeyedMemoryCache(localDevFusionCacheMemoryCacheKey);
} Does that look OK to you? Is there a better way to do this? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Hi @mderriey
Great, happy you're liking it!
Got it.
Yes, this is a different design from some potential alternatives mostly because it allows for extra features like fail-safe and similar to work seamlessly.
Correct.
Interesting... why use a fake ditributed cache (the in-memory one) while in dev env instead of not using an L2 at all? If it is about the difference in how to set up the Out of curiosity: I'm thinking from some time about adding a Thoughts?
Correct.
Mmmh, help me understand: if the goal is to have clean cold starts (eg: on restarts the cache is empty), then why use an L2 at all? As stated above you can just use L1 and call it a day.
I don't think this is how it works: if you don't specify anything in particular, FusionCache will create a private
Nope, very strange.
If the need is not to share the // THIS WILL HAVE ITS OWN PRIVATE MemoryCache
services.AddFusionCache(); Hope this helps, let me know. |
Beta Was this translation helpful? Give feedback.
Hi @mderriey
Great, happy you're liking it!
Got it.
Out of curiosity, if I may, why is that? Memory allocation concerns? Fear of updates not being reflected instantly? Something else?
Yes, this is a different design from some potential alternatives mostly becau…