0
0
Node.jsframework~20 mins

Redis for distributed caching in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Redis Caching Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Redis caching code snippet?

Consider this Node.js code using Redis to cache a value and then retrieve it:

import { createClient } from 'redis';

async function cacheExample() {
  const client = createClient();
  await client.connect();

  await client.set('key1', 'value1', { EX: 10 });
  const result = await client.get('key1');

  await client.disconnect();
  return result;
}

cacheExample().then(console.log);

What will be printed to the console?

Node.js
import { createClient } from 'redis';

async function cacheExample() {
  const client = createClient();
  await client.connect();

  await client.set('key1', 'value1', { EX: 10 });
  const result = await client.get('key1');

  await client.disconnect();
  return result;
}

cacheExample().then(console.log);
A"value1"
Bnull
Cundefined
DThrows a connection error
Attempts:
2 left
💡 Hint

Think about what happens immediately after setting a key with expiration.

📝 Syntax
intermediate
1:30remaining
Which option correctly sets a Redis key with a 5-minute expiration in Node.js?

Given the Redis client in Node.js, which code snippet correctly sets a key 'session' with value 'abc123' that expires after 5 minutes?

Aawait client.set('session', 'abc123', { EX: 300 });
Bawait client.set('session', 'abc123', { PX: 300000 });
Cawait client.set('session', 'abc123', 'EX', 300);
Dawait client.set('session', 'abc123', { EX: '300' });
Attempts:
2 left
💡 Hint

Check the official Redis client method signature for expiration options.

🔧 Debug
advanced
2:00remaining
Why does this Redis cache retrieval code throw an error?

Examine this code snippet:

import { createClient } from 'redis';

const client = createClient();

async function getCache() {
  const value = await client.get('missingKey');
  console.log(value.length);
}

getCache();

What error will this code throw and why?

Node.js
import { createClient } from 'redis';

const client = createClient();

async function getCache() {
  const value = await client.get('missingKey');
  console.log(value.length);
}

getCache();
ANo error, prints 0
BReferenceError: client is not defined
CSyntaxError: Unexpected token 'await'
DTypeError: Cannot read property 'length' of null
Attempts:
2 left
💡 Hint

What does Redis return when a key does not exist?

state_output
advanced
2:00remaining
What is the value of 'count' after running this Redis increment code?

Consider this code snippet:

import { createClient } from 'redis';

async function incrementCounter() {
  const client = createClient();
  await client.connect();

  await client.set('count', '5');
  const count = await client.incr('count');

  await client.disconnect();
  return count;
}

incrementCounter().then(console.log);

What will be printed to the console?

Node.js
import { createClient } from 'redis';

async function incrementCounter() {
  const client = createClient();
  await client.connect();

  await client.set('count', '5');
  const count = await client.incr('count');

  await client.disconnect();
  return count;
}

incrementCounter().then(console.log);
Anull
B5
C6
DThrows a TypeError
Attempts:
2 left
💡 Hint

What does the Redis INCR command do to the string value?

🧠 Conceptual
expert
2:30remaining
Which statement best explains Redis distributed caching behavior in a multi-instance Node.js app?

You have a Node.js app running on multiple servers. Each server connects to the same Redis instance for caching. Which statement is true about the cache behavior?

AEach server has its own isolated cache in Redis, so cache data is not shared across servers.
BAll servers share the same cache data, so a cache set by one server is immediately visible to others.
CRedis automatically replicates cache data to each server's local memory for faster access.
DCache data is only visible to the server that set it until Redis is restarted.
Attempts:
2 left
💡 Hint

Think about how Redis works as a centralized cache.