Redis helps store data temporarily so many computers can share it fast. It makes apps quicker by avoiding repeated work.
When you want to speed up a website by saving data that many users need.
When multiple servers need to share the same data quickly.
When you want to reduce load on a slow database by caching results.
When you want to store session info for users in a web app.
When you want to keep temporary data that expires automatically.
Syntax
Node.js
import { createClient } from'redis';
const client = createClient({ url: 'redis://localhost:6379' });
await client.connect();
// Set a value
await client.set('key', 'value');
// Get a value
const value = await client.get('key');
// Disconnect
await client.disconnect();
Use createClient to connect to Redis server.
Use set and get to store and retrieve data.
Examples
Store and retrieve a JavaScript object by converting it to a string.
A. Redis client does not support set and get methods.
B. Missing await before async calls causing unresolved promises.
C. Calling createClient without parentheses.
D. Using require instead of import for Redis client.
Solution
Step 1: Identify async Redis methods
client.connect(), client.set(), and client.get() return promises and must be awaited.
Step 2: Recognize missing await usage
Without await, val is a promise, so console.log(val) logs a Promise object, not the value.
Final Answer:
Missing await before async calls causing unresolved promises. -> Option B
Quick Check:
Async Redis calls need await [OK]
Hint: Always await async Redis calls to get actual results [OK]
Common Mistakes:
Not awaiting client.get causing Promise logged
Assuming require is invalid in Node.js
Thinking Redis client lacks set/get methods
5. You want to cache user profile data in Redis to reduce database calls. Which approach correctly handles cache misses and updates the cache in Node.js?
hard
A. Try to get data from Redis; if null, fetch from DB, then set Redis cache with the data.
B. Always fetch from DB and update Redis cache without checking Redis first.
C. Set Redis cache with empty string before fetching DB to avoid cache misses.
D. Fetch from Redis and DB simultaneously and use whichever returns first.
Solution
Step 1: Check Redis cache first
Attempt to get user profile from Redis to avoid unnecessary DB calls.
Step 2: On cache miss, fetch from DB and update Redis
If Redis returns null, fetch from database, then store result in Redis for future fast access.
Final Answer:
Try to get data from Redis; if null, fetch from DB, then set Redis cache with the data. -> Option A
Quick Check:
Cache miss handled by DB fetch then cache update [OK]
Hint: Check cache first; on miss, fetch DB then update cache [OK]
Common Mistakes:
Skipping cache check and always querying DB
Setting empty cache values causing wrong data
Fetching DB and cache simultaneously causing race conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.