Bird
Raised Fist0
Redisquery~20 mins

Why data modeling differs in Redis - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Data Modeling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why does Redis data modeling differ from traditional relational databases?

Redis is a key-value store with different data structures than relational databases. Which reason best explains why data modeling in Redis differs?

ARedis supports various data structures like lists, sets, and hashes, requiring different modeling approaches.
BRedis uses tables and rows like relational databases, so modeling is the same.
CRedis stores data as simple strings only, so complex models are impossible.
DRedis automatically converts all data into JSON format, so modeling is standardized.
Attempts:
2 left
💡 Hint

Think about the types of data Redis can store beyond simple strings.

❓ query_result
intermediate
2:00remaining
What is the output of this Redis command sequence?

Given the following Redis commands, what is the output of LRANGE mylist 0 -1?

LPUSH mylist "apple"
LPUSH mylist "banana"
LPUSH mylist "cherry"
A["banana", "apple", "cherry"]
B["apple", "banana", "cherry"]
C["cherry", "banana", "apple"]
D[]
Attempts:
2 left
💡 Hint

Remember that LPUSH adds elements to the head (left) of the list.

📝 Syntax
advanced
2:00remaining
Which Redis command syntax correctly adds a member to a set?

Choose the correct Redis command syntax to add the member "orange" to the set "fruits".

ASET fruits orange
BLPUSH fruits orange
CADD fruits orange
DSADD fruits orange
Attempts:
2 left
💡 Hint

Sets in Redis use a specific command to add members.

❓ optimization
advanced
2:00remaining
How to optimize Redis data modeling for fast retrieval of user sessions?

You want to store user session data in Redis for quick access. Which data modeling approach is best for fast retrieval by session ID?

AStore each session as a separate key with a hash containing session fields.
BStore all sessions in a single large list and scan it to find sessions.
CStore sessions as JSON strings in a sorted set with timestamps as scores.
DStore sessions in a Redis stream and read all entries sequentially.
Attempts:
2 left
💡 Hint

Consider how to quickly access a session by its unique ID.

🔧 Debug
expert
3:00remaining
Why does this Redis Lua script fail to update a hash field?

Given this Lua script run in Redis, why does the field "count" not update as expected?

local key = KEYS[1]
local field = ARGV[1]
local increment = tonumber(ARGV[2])
local current = redis.call('HGET', key, field)
if not current then
  current = 0
else
  current = tonumber(current)
end
redis.call('HSET', key, field, current + increment)
AThe script does not return any value, so the update is not saved.
BThe script does not handle the case when the existing field value is non-numeric correctly.
CThe script uses HSET instead of HINCRBY, causing the update to fail.
DThe variable 'current' is not converted to a number before addition.
Attempts:
2 left
💡 Hint

Check what happens if the field exists but contains a non-numeric value.

Practice

(1/5)
1. Why does data modeling in Redis differ from traditional relational databases?
easy
A. Because Redis uses SQL queries for data access
B. Because Redis stores data in memory using simple data types, not tables
C. Because Redis requires complex joins for data retrieval
D. Because Redis stores data only on disk

Solution

  1. Step 1: Understand Redis data storage

    Redis stores data in memory using simple data types like strings, lists, sets, hashes, and sorted sets instead of tables.
  2. Step 2: Compare with relational databases

    Relational databases store data in tables and use SQL for complex queries, which is different from Redis's in-memory key-value approach.
  3. Final Answer:

    Because Redis stores data in memory using simple data types, not tables -> Option B
  4. Quick Check:

    Redis data model = simple in-memory types [OK]
Hint: Remember Redis uses keys and simple types, not tables [OK]
Common Mistakes:
  • Thinking Redis uses SQL queries
  • Assuming Redis stores data on disk only
  • Believing Redis requires complex joins
2. Which of the following is the correct way to set a string value in Redis?
easy
A. SET key value
B. INSERT INTO key VALUES value
C. ADD key value
D. CREATE key value

Solution

  1. Step 1: Recall Redis command for setting string

    The Redis command to set a string value is SET key value.
  2. Step 2: Eliminate incorrect SQL-like commands

    Commands like INSERT INTO, ADD, and CREATE are not valid Redis commands.
  3. Final Answer:

    SET key value -> Option A
  4. Quick Check:

    Redis string set = SET key value [OK]
Hint: Use SET command to store strings in Redis [OK]
Common Mistakes:
  • Using SQL syntax like INSERT INTO
  • Confusing ADD with SET
  • Trying to CREATE keys like tables
3. Given the Redis commands:
LPUSH fruits apple
LPUSH fruits banana
LRANGE fruits 0 -1
What is the output of the LRANGE command?
medium
A. ["apple", "banana"]
B. ["apple"]
C. ["banana", "apple"]
D. Syntax error

Solution

  1. Step 1: Understand LPUSH behavior

    LPUSH adds elements to the head (left) of the list. First 'apple' is pushed, then 'banana' is pushed before it.
  2. Step 2: Check LRANGE output order

    LRANGE 0 -1 returns all elements from start to end, so the list is ["banana", "apple"].
  3. Final Answer:

    ["banana", "apple"] -> Option C
  4. Quick Check:

    LPUSH adds left, so newest first [OK]
Hint: LPUSH adds to front; LRANGE shows list from front [OK]
Common Mistakes:
  • Assuming LPUSH adds to the end
  • Confusing order of elements in LRANGE
  • Expecting syntax error from correct commands
4. You run the command HSET user:1 name "Alice" age 30 in Redis but get an error. What is the likely cause?
medium
A. The command syntax is correct; error is elsewhere
B. HSET does not accept multiple field-value pairs in one command
C. The age value must be a string, not a number
D. HSET requires field-value pairs, but age is not quoted

Solution

  1. Step 1: Verify HSET syntax

    HSET accepts a key followed by field-value pairs (multiple allowed). Unquoted integers like 30 are valid and stored as strings "30". No syntax error here.
  2. Step 2: Identify likely error sources

    Errors could stem from connection issues, insufficient permissions, Redis server not running, or client-specific parsing problems.
  3. Final Answer:

    The command syntax is correct; error is elsewhere -> Option A
  4. Quick Check:

    HSET user:1 name "Alice" age 30 [OK]
Hint: HSET supports multiple pairs; unquoted numbers OK [OK]
Common Mistakes:
  • Thinking numbers must be quoted in HSET
  • Believing HSET rejects multiple field-value pairs
  • Overlooking connection or permission issues
5. You want to model a user profile in Redis with fast access to username, email, and a list of friends. Which data modeling approach is best?
hard
A. Store all data as a single JSON string in a Redis string key
B. Create a relational table in Redis for user profiles
C. Use multiple Redis lists for each field
D. Use a Redis hash for username and email, and a Redis set for friends

Solution

  1. Step 1: Identify data types for each field

    Username and email are simple key-value pairs, best stored in a Redis hash for grouped access. Friends are a collection, best stored in a Redis set for uniqueness and fast membership checks.
  2. Step 2: Evaluate other options

    Storing all data as JSON string loses Redis data type benefits. Multiple lists for each field is inefficient. Redis does not support relational tables.
  3. Final Answer:

    Use a Redis hash for username and email, and a Redis set for friends -> Option D
  4. Quick Check:

    Hashes for objects, sets for unique collections [OK]
Hint: Use hashes for objects and sets for unique lists [OK]
Common Mistakes:
  • Storing everything as JSON string
  • Using lists for unique friend sets
  • Expecting relational tables in Redis
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard