Tenant-safe cache keys and stampedes
Cache keys must distinguish every scope that affects the returned data, including tenant and relevant authorization context. A cache stampede occurs when many requests simultaneously refill the same expired or missing entry. Use unambiguous key encoding rather than joining arbitrary strings with a delimiter. A local in-flight map can coalesce simultaneous loads for the same key within one process. It must remove completed or failed loads, bound the number of unique outstanding keys and enforce deadlines in the underlying loader. Across replicas, local coalescing is insufficient; consider jittered TTL, bounded concurrency or coordinated refresh. Do not serve private data without authorization just because the key contains a tenant. Redis pub/sub is not a durable queue. Do not use a cache key based only on the item ID for tenant-scoped records. Do not let a rejected in-flight promise permanently poison future refreshes. Evidence: Dependency-free application model; no external service execution. Source: https://redis.io/docs/latest/develop/clients/nodejs/
Formal Syntax & Structure
See example for the scoped contractWorking Example
const inflight=new Map();let loads=0;function read(tenant,id){const key=JSON.stringify([tenant,id]);if(inflight.has(key))return inflight.get(key);if(inflight.size>=4)return Promise.reject(Error('busy')); const work=Promise.resolve().then(()=>{loads++;return tenant+':'+id;}).finally(()=>inflight.delete(key));inflight.set(key,work);return work;}console.log(JSON.stringify(await Promise.all([read('a','x'),read('a','x'),read('b','x')])));console.log(loads,inflight.size);