Lesson 4 of 9 · 25m
Cache Keys, TTLs, and the Shape of Cacheability
Design keys that capture every correctness input and choose TTLs from business tolerance rather than superstition.
Application caches fail quietly. Redis does not know that tenant A and tenant B are different customers; it only knows the string you gave it. If the key is wrong, the database of record becomes a witness to corruption rather than a source of truth. Good cache design starts with keys and freshness tolerance, not with a client library.
A cache key is a serialized correctness proof
Every input that can change the output must be represented in the key, or the output must be invariant under that input. For a user profile lookup:
profile:v3:user:42That is enough only if the response depends solely on user 42 and the representation format for version 3. If the response is tenant-scoped, locale-specific, feature-flagged, or permission-filtered, those inputs belong in the key or in a broader derivation.
tenant:17:profile:v3:user:42:locale:en-US:flags:8f2There is a tradeoff: longer keys and more dimensions improve correctness but reduce reuse. Include only inputs that genuinely alter the representation.
Namespace versions are your escape hatch
Embed a schema or semantic version in keys. When the payload shape changes, roll the version instead of trying to make old and new values compatible forever.
key = f"catalog:v4:product:{product_id}:region:{region}"This does not invalidate old data automatically. It makes new reads miss old keys. Old entries then expire naturally, which is operationally gentler than flushing an entire cache during deployment.
Choose TTL from tolerance, not vibes
Ask what happens if a consumer sees data that is five seconds, five minutes, or five hours stale. Different data has different tolerance:
session authorization seconds or explicit invalidation
product price seconds to minutes, business-dependent
product description minutes to hours
countries/timezones hours to days
content-addressed assets effectively infiniteTTL is a business decision expressed in seconds. Shorter is not automatically safer; if every entry expires in one second, you built a very expensive database proxy.
Small TTL plus validation often beats giant TTL
For hot application objects, use a modest TTL and explicit invalidation on writes. Long TTLs can be appropriate for immutable or rarely changing records, but only when invalidation is boring and reliable. If nobody can answer 'what writes affect this key?', do not give it a long lifetime.
Watch key cardinality
High-cardinality keys can fill memory without improving hit ratio. Caching per-user anonymous fragments or one-off search strings often produces a write-heavy, read-never workload. Redis memory pressure then evicts the valuable entries too.
redis-cli --bigkeys
redis-cli info memory | grep -E 'used_memory_human|maxmemory'Metrics should tell you whether keys are reused. A cache with millions of entries and a 3% hit ratio is a queue for eviction, not an optimization.
Field notes
- Include tenant boundaries in keys even when IDs are globally unique; it prevents accidents during refactoring and data migration.
- Never place raw user input into keys without length and character controls. Delimiters and whitespace create ambiguity.
- Negative results need keys too, but usually shorter TTLs.
- Do not cache authorization decisions longer than you are willing to tolerate revoked access.
- A cache flush is not an invalidation strategy; it is an admission of design defeat.
Was this lesson helpful?