Caching interview question · Most asked
How do you design good cache keys?
Summary
Keys should be deterministic, namespaced, versioned and include every input that changes the result (tenant, locale, user/role when personalized, query parameters), while staying reasonably short.
Longer Answer
Example: catalog:v3:tenant:42:product:1001:locale:en. Versioning lets you change the cached format without collisions. Hash long inputs (search queries). Forgetting an input (like tenant or permissions) causes data leaks between users.
Key Points
- Namespace + version
- Include every varying input
- Hash long inputs
- Missing tenant/user = data leak
More most-asked Caching questions
- Explain the main caching patterns: cache-aside, read-through, write-through and write-behind.
- Why is cache invalidation hard, and what strategies help?
- What is a cache stampede (thundering herd) and how do you prevent it?
- How does HTTP caching work (Cache-Control, ETag, CDNs)?
- Explain the difference between cache penetration, cache breakdown and cache avalanche.
- In cache-aside, why can a value be stale even though you delete the key on every write?
- How can a CDN leak one user's data to another, and how do you prevent it?
- How does stale-while-revalidate work, and when is it inappropriate?