FusionCache icon indicating copy to clipboard operation
FusionCache copied to clipboard

[FEATURE] Eager Refresh support when using SkipMemoryCache (L2-only scenarios)

Open Coelho04 opened this issue 8 months ago • 2 comments

Problem

We wanted to use SkipMemoryCache = true to avoid memory pressure on our pods, while still benefiting from Eager Refresh to keep our Redis cache warm and reduce latency. However, after enabling SkipMemoryCache = true, we noticed Eager Refresh completely stopped firing — zero EagerRefresh events in our OpenTelemetry metrics.

Solution

I believe that the solution would be when validating that the distributed entry is valid to also perform the check if it should execute the eager refresh.

Thank you for the amazing work.

Coelho04 avatar Mar 18 '26 01:03 Coelho04

I believe the solution is something like this

if (distributedEntryIsValid)
						{
							isStale = false;
							entry = FusionCacheMemoryEntry<TValue>.CreateFromOtherEntry(distributedEntry!, options);
							
							if (isRealFactory && entry.ShouldEagerlyRefresh())
							{
								// RESET EAGER REFRESH
								if (entry!.Metadata is not null)
									entry.Metadata.EagerExpirationTimestamp = null;

								if (_logger?.IsEnabled(LogLevel.Trace) ?? false)
									_logger.Log(LogLevel.Trace, "FUSION [N={CacheName} I={CacheInstanceId}] (O={CacheOperationId} K={CacheKey}): should eagerly refresh", CacheName, InstanceId, operationId, key);

								// TRY TO GET THE MEMORY LOCK WITHOUT WAITING, SO THAT ONLY THE FIRST ONE WILL ACTUALLY REFRESH THE ENTRY
								var tmpMemoryLockObj = await AcquireMemoryLockAsync(operationId, key, TimeSpan.Zero, token).ConfigureAwait(false);
								if (tmpMemoryLockObj is null)
								{
									if (_logger?.IsEnabled(LogLevel.Trace) ?? false)
										_logger.Log(LogLevel.Trace, "FUSION [N={CacheName} I={CacheInstanceId}] (O={CacheOperationId} K={CacheKey}): eager refresh already occurring", CacheName, InstanceId, operationId, key);
								}
								else
								{
									_ = Task.Run(async () =>
									{
										await MaybeExecuteEagerRefreshWithAsyncFactoryAsync<TValue>(operationId, key, originalKey, tags, factory, options, entry!, tmpMemoryLockObj, token).ConfigureAwait(false);
									});
								}
							}
						}

Adding this code in both checks if (distributedEntryIsValid) would maybe work

Coelho04 avatar Mar 18 '26 02:03 Coelho04

Related to this, eager refresh exclusively uses the factory operation to get a new key, even though the L2 may have an updated key from another replica's update.

Even in the case of L1/L2/Live Backend setup, there is unneeded backend pressure.

Eager refresh should consult the L2 before making the factory call. If the distributed key is within eager refresh period, only then make the live request, otherwise the eager refresh should update the L1 with the L2.

ghost avatar Aug 14 '26 00:08 ghost