fix(login): replace custom SWR cache with lru-cache (#11945)

# Which Problems Are Solved

The in-memory SWR promiseCache in zitadel.ts used keys like
getBrandingSettings-${org} that had no instance identifier. In
multi-tenant mode, where a single Next.js process serves multiple
instances, an instance A's cached settings without org context
(branding, languages, settings, etc.) could be served to Instance B
request without org context. The cache now additionally is bound to 100
entries by default (configurable via maxSize in `API_CACHE_CONFIG`).
When capacity is exceeded, expired entries are swept first

It additionally replaces the hand-rolled PromiseCache (Map-based, FIFO
eviction) with lru-cache, leveraging its built-in fetchMethod for
stale-while-revalidate, request deduplication, and true LRU eviction.

# How the Problems Are Solved

Added an instanceCacheKey() helpe that prefixes every cache key with
serviceConfig.instanceHost in addition to the org context if available.

---------

Co-authored-by: Ramon <[email protected]>
This commit is contained in:
Max Peintner
2026-04-13 11:19:50 +02:00
committed by GitHub
co-authored by Ramon
parent 551883dcab
commit 7d231b43d4
6 changed files with 363 additions and 26 deletions
+4 -3
View File
@@ -85,12 +85,13 @@ declare namespace NodeJS {
API_CACHE_ENABLED?: string;
/**
* Optional: JSON string to configure the cache TTLs (in minutes) for specific backend API routes or global fallbacks.
* Example: '{"defaultMinutes": 15, "longMinutes": 60, "getBrandingSettings": 120}'
*
* Optional: JSON string to configure the cache TTLs (in minutes) and size limits for specific backend API routes or global fallbacks.
* Example: '{"defaultMinutes": 15, "longMinutes": 60, "maxSize": 200, "getBrandingSettings": 120}'
*
* Properties:
* - \`defaultMinutes\`: The globally utilized default TTL in minutes (falls back to 15 if not set).
* - \`longMinutes\`: The TTL utilized string for long-cached routes, like branding/translation (falls back to 60 if not set).
* - \`maxSize\`: Maximum number of entries the in-memory cache may hold. Oldest entries are evicted when capacity is reached (defaults to 100).
* - \`[route_name]\`: Explicit overrides per specific API method (e.g., \`getHostedLoginTranslation\`).
*/
API_CACHE_CONFIG?: string;