Loading…
A short voice session: one question, one follow-up, then instant feedback — up to 5 minutes.
I would identify read-heavy and relatively stable data, choose an appropriate cache strategy such as cache-aside, define TTL and invalidation rules, and monitor hit rate and latency. The main risks are stale data, cache stampedes, inconsistent invalidation, and memory pressure.
A high-traffic service should not automatically cache everything. I would first identify expensive, frequently read data and determine whether slightly stale results are acceptable.
A common pattern is cache-aside. The application checks Redis first. On a miss, it reads from the database, stores the result in Redis with a TTL, and returns it:
const cached = await redis.get(key);
if (cached) {
return JSON.parse(cached);
}
const value = await database.getProduct(id);
await redis.set(key, JSON.stringify(value), { EX: 300 });
return value;For writes, the application can update the source of truth and invalidate or update the corresponding cache entry. Choosing between invalidation and write-through depends on consistency requirements and architecture.
At scale, several failure modes matter. A cache stampede occurs when many requests miss simultaneously and all hit the database. Techniques such as request coalescing, short randomized TTLs, locks, or stale-while-revalidate can reduce the problem.
There is also the question of consistency. If cached data must be immediately correct, an eventually consistent cache may be inappropriate. TTL is not a complete invalidation strategy because stale data can remain visible until expiration.
I would monitor cache hit rate, miss rate, latency, memory usage, eviction rate, and backend load. A cache should reduce system cost and latency without becoming an unobservable source of correctness bugs.