0
0
Expressframework~20 mins

Redis integration for distributed cache in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redis Cache Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when fetching a cached value?
Given this Express route using Redis as a cache, what will be the response if the key 'user:123' exists with value 'Alice' in Redis?
Express
import express from 'express';
import { createClient } from 'redis';

const app = express();
const redisClient = createClient();
await redisClient.connect();

app.get('/user/:id', async (req, res) => {
  const userId = req.params.id;
  const cachedUser = await redisClient.get(`user:${userId}`);
  if (cachedUser) {
    res.send(`Cached User: ${cachedUser}`);
  } else {
    res.send('User not found in cache');
  }
});
AError: redisClient.get is not a function
BUser not found in cache
CCached User: Alice
DCached User: undefined
Attempts:
2 left
💡 Hint
Think about what happens when the key exists in Redis and is retrieved.
📝 Syntax
intermediate
2:00remaining
Which option correctly initializes Redis client in Express?
Choose the correct way to create and connect a Redis client using the modern redis package in an Express app.
A
import { createClient } from 'redis';
const client = createClient;
client.connect();
B
import { createClient } from 'redis';
const client = createClient();
await client.connect();
C
import redis from 'redis';
const client = redis.createClient();
client.connect();
D
const redis = require('redis');
const client = redis.createClient();
client.connect();
Attempts:
2 left
💡 Hint
The modern redis package uses createClient function and connect returns a promise.
🔧 Debug
advanced
2:00remaining
Why does this Redis cache middleware cause a runtime error?
Examine the middleware below. Why does it cause a runtime error when used in an Express app?
Express
async function cache(req, res, next) {
  const key = req.originalUrl;
  const cachedData = redisClient.get(key);
  if (cachedData) {
    res.send(cachedData);
  } else {
    next();
  }
}
AredisClient.get returns a promise but code treats it as synchronous, causing res.send to get a promise object.
BredisClient.get is undefined because redisClient was not imported.
CThe middleware does not call next() in any case, causing request hang.
DThe key variable is not a string, causing redisClient.get to throw a TypeError.
Attempts:
2 left
💡 Hint
Check if redisClient.get is asynchronous and how its result is used.
state_output
advanced
2:00remaining
What is the value of 'cacheHit' after this Express route runs?
Consider this Express route using Redis cache. What is the value of 'cacheHit' after a request to '/data' if the cache was empty initially?
Express
let cacheHit = false;

app.get('/data', async (req, res) => {
  const cached = await redisClient.get('data');
  if (cached) {
    cacheHit = true;
    res.send(cached);
  } else {
    cacheHit = false;
    const freshData = 'fresh data';
    await redisClient.set('data', freshData);
    res.send(freshData);
  }
});
Aundefined
Bnull
Ctrue
Dfalse
Attempts:
2 left
💡 Hint
Think about the cache state before the request and what the code does when cache is empty.
🧠 Conceptual
expert
3:00remaining
Which Redis feature best supports distributed cache invalidation across multiple Express instances?
You have multiple Express servers using Redis as a distributed cache. Which Redis feature helps notify all servers to invalidate a cache key when data changes?
ARedis Pub/Sub to publish invalidation messages to all servers
BRedis persistence (RDB snapshots) to save cache state
CRedis transactions to atomically update cache keys
DRedis Lua scripting to run cache logic on server
Attempts:
2 left
💡 Hint
Think about how servers can communicate cache changes in real time.