<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=1063935717132479&amp;ev=PageView&amp;noscript=1 https://www.facebook.com/tr?id=1063935717132479&amp;ev=PageView&amp;noscript=1 "> Namespace Conventions

Namespace Conventions

A cache is one big shared pool of keys, so naming matters. The convention is colon-delimited namespaces that read from general to specific: user:42:profile. Good key names prevent collisions, make the cache easy to reason about, and let you find or clear related keys by their shared prefix.

A naming pattern

const key = (userId: number) => `user:${userId}:profile`;

await cache.set(key(42), JSON.stringify(profile), { EX: 300 });
const cached = await cache.get(key(42));

Mirror your entity hierarchy

Structure keys to reflect the relationships in your data model. If users own posts and posts have comments, that nesting should show up in the key: user:42:post:7:comments. A reader can look at any key and immediately know what entity it belongs to, where it sits in the hierarchy, and what other keys are related to it.

This also makes invalidation natural. When a user is deleted, all keys under user:42: are candidates for eviction. When a post is updated, user:42:post:7: keys are the scope to clear.

user:42 user:42:profile user:42:post:7 user:42:post:7:comments
Colon-delimited keys read general to specific and mirror your data model. A shared prefix like user:42: groups everything for one user.

Why conventions help

  • No collisionsuser:42 and post:42 never clash, even though both end in 42
  • Grouping — every key under user:42: belongs to one user and can be cleared together
  • Versioning — bump a prefix (v2:user:42:profile) to invalidate a whole shape of data at once
  • Shared caches — prefix by service name (auth:, catalog:) when multiple services share a cache instance so their keys never overlap

Pick one convention and apply it everywhere. A consistent scheme is what keeps a shared cache from turning into a junk drawer.

Exercise

Redis challenge · runs in your browser

Key namespaces — organize keys with prefixes

Organize Redis keys using colon-separated namespaces. Implement three functions using the key format user:{userId}:{field}: • setUserField(redis, userId, field, value) — store user data under a namespaced key • getUserKeys(redis, userId) — return all key names for a given user (use KEYS with a glob) • deleteUser(redis, userId) — remove all data for a user in a single DEL call Redis has no folders — the colon convention is the standard pattern used by every major Redis client and framework. It enables pattern scanning (KEYS user:*) and bulk operations.

Commands run against an in-browser Redis mock that implements the full ioredis API. Your code works identically against a real Redis instance — just swap in new Redis() from ioredis.