Skip to content

Don't short-circuit the in-memory token cache serialization provider - #3970

Merged
Gladwin Johnson VR (gladjohn) merged 4 commits into
masterfrom
gladjohn/inmemory-cache-no-shortcircuit
Jul 24, 2026
Merged

Gladwin Johnson VR (gladjohn) merged 4 commits into
masterfrom
gladjohn/inmemory-cache-no-shortcircuit

Conversation

@gladjohn

@gladjohn Gladwin Johnson VR (gladjohn) commented Jul 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When an app registers AddInMemoryTokenCache / AddInMemoryTokenCaches, TokenAcquisition bypasses the registered IMemoryCache serialization provider and instead switches MSAL to its opaque static (process-wide) cache:

if (_tokenCacheProvider is MsalMemoryTokenCacheProvider)
    builder.WithCacheOptions(CacheOptions.EnableSharedCacheOptions);   // static cache
...
if (!(_tokenCacheProvider is MsalMemoryTokenCacheProvider))           // Initialize() skipped
{ _tokenCacheProvider.Initialize(app.AppTokenCache); _tokenCacheProvider.Initialize(app.UserTokenCache); }

Initialize() is what wires MSAL's serialization callbacks, so skipping it for the in-memory provider means:

  • the registered IMemoryCache is never used (tokens live in MSAL's static dictionary),
  • GetSuggestedCacheKey (the cache-key partitioning hook) is a silent no-op,
  • MsalMemoryTokenCacheOptions.AbsoluteExpirationRelativeToNow and the memory-cache SizeLimit are silently ignored,
  • the cache is unbounded with no eviction.

This is the IdWeb-side driver behind very large single-partition app-token caches for high-cardinality / token-broker workloads (one {clientId}_{tenantId} app-token partition observed at 164,663 tokens; AcquireTokenForClient becomes O(N) over the partition, with saves serializing under MSAL's cache semaphore). Related: #3961.

Change

Initialize the token cache provider for all cache types (so the in-memory provider's serialization callbacks — including GetSuggestedCacheKey and per-entry expiry — actually run), and enable MSAL's shared/static cache only when it is actually required:

bool usesSharedInternalCache =
    isAgentCca ||
    (mergedOptions.UseFastUnboundedCache && _tokenCacheProvider is MsalMemoryTokenCacheProvider);
if (usesSharedInternalCache)
    builder.WithCacheOptions(CacheOptions.EnableSharedCacheOptions);
...
if (!usesSharedInternalCache)
{
    _tokenCacheProvider.Initialize(app.AppTokenCache);
    _tokenCacheProvider.Initialize(app.UserTokenCache);
}

Agent CCAs continue to use the shared/static cache (MSAL forbids combining the internal shared cache with external serialization). Now GetSuggestedCacheKey, entry TTL, and size limits actually work.

Opt-in (config-driven)

A new MicrosoftIdentityOptions.UseFastUnboundedCache flag restores the previous fast, process-wide static cache. It is wired the same way as EnableCacheSynchronization — a config-bound option that flows into MergedOptions (UpdateMergedOptionsFromMicrosoftIdentityOptions) and is read in TokenAcquisition. So it is settable from configuration, not just code:

// appsettings.json
"AzureAd": {
  // ...
  "UseFastUnboundedCache": true
}
  • Default is false (the new, bounded, serialized behavior).
  • true = MSAL static/shared cache: fast, unbounded (no eviction), ignores expiry/size options and the partitioning hook.

Behavior change (minor)

Consumers who set AbsoluteExpirationRelativeToNow / SizeLimit on the in-memory cache previously had them ignored; they now take effect (tokens may expire/evict where they previously persisted for the process lifetime). Opt out via UseFastUnboundedCache if the old semantics are required.

Files

  • src/Microsoft.Identity.Web.TokenAcquisition/TokenAcquisition.cs — un-short-circuit + UseFastUnboundedCache gate
  • src/Microsoft.Identity.Web.TokenAcquisition/MicrosoftIdentityOptions.cs — new UseFastUnboundedCache (config-bindable)
  • src/Microsoft.Identity.Web.TokenAcquisition/MergedOptions.cs — flow it into MergedOptions
  • src/Microsoft.Identity.Web.TokenAcquisition/PublicAPI/{NetCore,NetFramework}/PublicAPI.Unshipped.txt
  • Tests updated accordingly

Testing

Unit suite green across TFMs (local):

Suite / TFM Result
Web.Test net462 / net472 44 / 44
Web.Test net8.0 1031 passed, 0 failed, 11 skipped
Web.Test net9.0 1040 / 0 / 4
Web.Test net10.0 1059 / 0 / 4
UI.Test net8.0 / net9.0 46 / 46

Two behavior tests assert: (1) an app token acquired through the default in-memory cache is written to the backing IMemoryCache (serialization provider wired), and (2) with UseFastUnboundedCache = true nothing is written (static cache used). An initial naive "always initialize" version failed 9 TokenAcquisitionTests.Agent* tests with MsalClientException: "You configured MSAL cache serialization at the same time with internal caching options. These are mutually exclusive.", which drove the usesSharedInternalCache gate. Integration/E2E were not run (require lab credentials).

Open item

UseFastUnboundedCache on MicrosoftIdentityOptions flows into MergedOptions in every host (DefaultTokenAcquisitionHost.GetOptions resolves the MicrosoftIdentityOptions merger), so it works whenever the value is set. Config-binding coverage depends on the entrypoint: TokenAcquirerFactory.GetDefaultInstance("AzureAd") binds the section only to MicrosoftIdentityApplicationOptions. Confirming the MISE-native config entrypoint is a follow-up (if it binds only MicrosoftIdentityApplicationOptions, the flag would also need to live in Microsoft.Identity.Abstractions).


🤖 Prototyped during an investigation with GitHub Copilot.

AddInMemoryTokenCache(s) previously switched MSAL to its opaque static cache and skipped Initialize() for MsalMemoryTokenCacheProvider, so the IMemoryCache serializer was never used. This disabled the GetSuggestedCacheKey partitioning hook, AbsoluteExpirationRelativeToNow, and the memory-cache SizeLimit, leaving the app-token cache unbounded with no eviction (a driver behind very large single-partition app-token caches).

Initialize the provider for all cache types and enable MSAL's shared/static cache only when required (agent CCAs, or the new opt-out). Adds MsalMemoryTokenCacheOptions.UseSharedCache (default false) to restore the legacy static-cache behavior.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 2d02b5ca-22b5-4b48-840a-de79786b3f9d
… opt-out

Unit tests: MsalMemoryTokenCacheOptions.UseSharedCache defaults to false; MsalMemoryTokenCacheProvider.UseSharedCache reflects the option. Behavior tests: an app token acquired through the default in-memory cache is written to the backing IMemoryCache (serialization provider wired), and with UseSharedCache=true nothing is written (legacy static cache path).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 2d02b5ca-22b5-4b48-840a-de79786b3f9d
…ndedCache

Address review feedback: rename UseSharedCache -> UseFastUnboundedCache, move it
off MsalMemoryTokenCacheProvider/Options onto MicrosoftIdentityOptions (which is
config-bindable), and flow it into MergedOptions via
UpdateMergedOptionsFromMicrosoftIdentityOptions (mirroring EnableCacheSynchronization).
TokenAcquisition now reads mergedOptions.UseFastUnboundedCache. It can be set via
configuration under the AzureAd section (UseFastUnboundedCache: true).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 2d02b5ca-22b5-4b48-840a-de79786b3f9d
@gladjohn
Gladwin Johnson VR (gladjohn) merged commit c3e5d4d into master Jul 24, 2026
9 checks passed
@gladjohn
Gladwin Johnson VR (gladjohn) deleted the gladjohn/inmemory-cache-no-shortcircuit branch July 24, 2026 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants