Challenge - 5 Problems
Redis with Node.js Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this Redis command sequence using ioredis?
Consider the following Node.js code using ioredis:
What will be printed to the console?
const Redis = require('ioredis');
const redis = new Redis();
await redis.set('count', 5);
const result = await redis.incr('count');
console.log(result);What will be printed to the console?
Redis
const Redis = require('ioredis'); const redis = new Redis(); await redis.set('count', 5); const result = await redis.incr('count'); console.log(result);
Attempts:
2 left
💡 Hint
The INCR command increases the integer value stored at key by one.
✗ Incorrect
The key 'count' is set to 5, then INCR increases it by 1, so the result is 6.
📝 Syntax
intermediate2:00remaining
Which option correctly sets a hash field using ioredis?
You want to set the field 'name' to 'Alice' in the hash stored at key 'user:1'. Which of the following code snippets is correct?
Attempts:
2 left
💡 Hint
hset takes key, field, and value as separate arguments.
✗ Incorrect
Option C uses the correct syntax: key, field, value. Option C passes an object which is invalid for hset in ioredis. Option C passes an array which is invalid. Option C passes a single string combining field and value which is invalid.
❓ optimization
advanced2:00remaining
Which approach is best to retrieve multiple keys efficiently with ioredis?
You need to get the values of keys 'key1', 'key2', and 'key3'. Which option is the most efficient?
Attempts:
2 left
💡 Hint
Pipelining batches commands to reduce round-trip time.
✗ Incorrect
Option B uses mget which is the most efficient for retrieving multiple keys in one command. Option B uses pipelining which batches commands but mget is more efficient for multiple key retrieval. Option B sends commands sequentially, slowest. Option B sends commands in parallel but still multiple round-trips.
🔧 Debug
advanced2:00remaining
Why does this ioredis code throw an error?
Given this code:
It throws: SyntaxError: await is only valid in async function. Why?
const Redis = require('ioredis');
const redis = new Redis();
redis.set('foo', 'bar');
const value = await redis.get('foo');
console.log(value);It throws: SyntaxError: await is only valid in async function. Why?
Redis
const Redis = require('ioredis'); const redis = new Redis(); redis.set('foo', 'bar'); const value = await redis.get('foo'); console.log(value);
Attempts:
2 left
💡 Hint
await can only be used inside async functions.
✗ Incorrect
The error is due to using await at the top level outside an async function. Wrapping code in async function or using .then() fixes it.
🧠 Conceptual
expert2:00remaining
What happens if you call redis.del('key') on a non-existing key?
Using ioredis, you run:
What will be printed?
const result = await redis.del('nonexistent');
console.log(result);What will be printed?
Redis
const result = await redis.del('nonexistent');
console.log(result);Attempts:
2 left
💡 Hint
DEL returns the number of keys removed.
✗ Incorrect
If the key does not exist, DEL returns 0 indicating no keys were deleted.