Loading Depth Study
Preparing your workspace
Caching stores reusable results so expensive operations do not need to run every time a request arrives.
A common Redis pattern is cache-aside: the application first checks Redis, and if the value is missing, it loads the data from the primary database, stores a cache entry, and returns the result. Cache expiration limits how long stale data may remain. Cache invalidation is important when underlying data changes. Cache keys should be predictable and include relevant identifiers so different resources do not overwrite each other.
A public course page can cache a course record for a short period to reduce repeated PostgreSQL queries.
const key = `course:${courseId}`;
let course = await redis.get(key);
if (!course) {
course = JSON.stringify(await loadCourseFromDatabase(courseId));
await redis.set(key, course, 'EX', 60);
}
return JSON.parse(course);The application first checks Redis and only queries the database when the cached course is missing.