0
0
Node.jsframework~10 mins

Redis for distributed caching in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Redis for distributed caching
App requests data
Check Redis cache
Return cached
Send response
The app first checks Redis cache for data. If found, it returns cached data. If not, it fetches from database, stores it in Redis, then returns it.
Execution Sample
Node.js
const redis = require('redis');
const client = redis.createClient();

client.connect();

async function getData(key) {
  const cache = await client.get(key);
  let data;
  if (cache) {
    data = JSON.parse(cache);
  } else {
    data = await fetchFromDB(key);
    await client.set(key, JSON.stringify(data));
  }
  return data;
}
This code tries to get data from Redis cache first; if missing, it fetches from DB, caches it, then returns.
Execution Table
StepActionRedis GET ResultDB FetchRedis SETReturn Value
1Call getData('user:1')nullNoNoNo
2Redis GET 'user:1'nullNoNoNo
3Cache miss, fetch from DBnullUser data from DBNoNo
4Store data in RedisnullUser data from DBSet user:1No
5Return fetched datanullUser data from DBSet user:1User data from DB
6Call getData('user:1') againUser data JSONNoNoNo
7Redis GET 'user:1'User data JSONNoNoNo
8Cache hit, parse and returnUser data JSONNoNoUser data from cache
💡 Execution stops after returning data from cache or DB.
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 8
cacheundefinednullnullUser data JSON string
dataundefinedUser data from DBUser data from DBUser data from cache (parsed)
Key Moments - 3 Insights
Why do we check Redis before fetching from the database?
Because Redis is a fast cache; checking it first avoids slow database calls if data is already cached, as shown in execution_table steps 2 and 7.
What happens if Redis returns null?
It means cache miss; then the code fetches data from the database and stores it in Redis, as seen in steps 3 and 4.
Why do we store data in Redis after fetching from DB?
To speed up future requests by caching the data, so next time Redis GET returns the data directly, shown in step 4 storing and step 8 returning.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Redis GET result at step 7?
AError
Bnull
CUser data JSON
DUndefined
💡 Hint
Check the 'Redis GET Result' column at step 7 in the execution_table.
At which step does the code fetch data from the database?
AStep 2
BStep 3
CStep 6
DStep 8
💡 Hint
Look for the 'DB Fetch' column showing 'User data from DB' in the execution_table.
If Redis always returns data, which steps would be skipped?
ASteps 3 and 4
BSteps 1 and 2
CSteps 5 and 6
DSteps 7 and 8
💡 Hint
Refer to execution_table steps where DB fetch and Redis set happen.
Concept Snapshot
Redis caching flow:
1. App requests data.
2. Check Redis cache with GET.
3. If cache miss, fetch from DB.
4. Store DB data in Redis with SET.
5. Return data to app.
This speeds up repeated data access.
Full Transcript
This visual execution shows how Redis is used for distributed caching in a Node.js app. When the app requests data, it first checks Redis cache using GET. If Redis returns null, it means the data is not cached, so the app fetches it from the database. Then, it stores the fetched data in Redis using SET for future fast access. If Redis returns data, the app parses and returns it immediately, avoiding a database call. This process improves performance by reducing database load and speeding up data retrieval.