Bird
Raised Fist0
Redisquery~20 mins

Key design patterns in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Key Design Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this Redis command sequence?
Given the following Redis commands executed in order:

1. SET user:1000:name "Alice"
2. SET user:1000:age 30
3. HGETALL user:1000

What will be the output of the last command?
Redis
SET user:1000:name "Alice"
SET user:1000:age 30
HGETALL user:1000
AAn empty list ([]) because user:1000 is not a hash key
B["user:1000:name", "Alice", "user:1000:age", "30"]
C["name", "Alice", "age", "30"]
DError: WRONGTYPE Operation against a key holding the wrong kind of value
Attempts:
2 left
💡 Hint
Remember that SET creates string keys, not hashes.
📝 Syntax
intermediate
2:00remaining
Which Redis command correctly implements a simple rate limiter using a sorted set?
You want to limit a user to 5 actions per minute using Redis sorted sets. Which command correctly adds a timestamp to the sorted set for user:123 and removes old entries older than 60 seconds?
A
ZADD user:123 action1 1620000000
ZREMRANGEBYSCORE user:123 -inf 1619999940
B
ZADD user:123 action1 1620000000
ZREMRANGEBYSCORE user:123 1619999940 +inf
C
ZADD user:123 1620000000 action1
ZREMRANGEBYSCORE user:123 1619999940 +inf
D
ZADD user:123 1620000000 action1
ZREMRANGEBYSCORE user:123 -inf 1619999940
Attempts:
2 left
💡 Hint
ZADD syntax is ZADD key score member. ZREMRANGEBYSCORE removes by score range.
🧠 Conceptual
advanced
2:00remaining
Why is the Redis key naming pattern with colons (:) recommended?
Redis keys often use colons to separate parts, like user:1000:profile. What is the main advantage of this pattern?
AIt helps organize keys logically and supports efficient use of commands like SCAN and KEYS with patterns.
BIt allows Redis to automatically create nested data structures.
CIt compresses keys to save memory in Redis.
DIt enables Redis to enforce access control on key namespaces.
Attempts:
2 left
💡 Hint
Think about how you find keys by pattern.
🔧 Debug
advanced
2:00remaining
What error occurs when running this Redis Lua script?
Consider this Lua script run in Redis:

local val = redis.call('GET', 'counter')
return val + 1

What error will this script produce if 'counter' key does not exist?
Redis
local val = redis.call('GET', 'counter')
return val + 1
ARedis command error because GET is invalid in Lua
Bnil value error because val is nil and cannot be added to 1
CReturns 1 because nil is treated as 0
DSyntax error in Lua script
Attempts:
2 left
💡 Hint
What does GET return if key does not exist?
❓ optimization
expert
2:00remaining
Which Redis data structure and pattern is best for implementing a leaderboard with frequent score updates and top 10 queries?
You want to build a leaderboard that updates user scores frequently and retrieves the top 10 users efficiently. Which Redis data structure and pattern is best suited?
AUse a Redis hash with user IDs as fields and scores as values
BUse a Redis list to push scores and sort on retrieval
CUse a Redis sorted set with user IDs as members and scores as scores
DUse multiple string keys per user to store scores and sort externally
Attempts:
2 left
💡 Hint
Think about data structures that keep elements sorted by score.

Practice

(1/5)
1. What is the main purpose of using key design patterns in Redis?
easy
A. To delete all keys automatically
B. To organize data for easy access and management
C. To slow down data retrieval
D. To increase the size of the database

Solution

  1. Step 1: Understand Redis key design patterns

    Key design patterns help organize Redis keys logically.
  2. Step 2: Identify the main benefit

    Organizing keys makes data easier to access and manage efficiently.
  3. Final Answer:

    To organize data for easy access and management -> Option B
  4. Quick Check:

    Key design patterns = organize data [OK]
Hint: Think about why organization helps in real life [OK]
Common Mistakes:
  • Confusing organization with database size
  • Assuming patterns slow down Redis
  • Thinking patterns delete keys automatically
2. Which of the following is a correct Redis key pattern to group user data by user ID?
easy
A. user:1234:name
B. user-1234-name
C. user1234name
D. user/1234/name

Solution

  1. Step 1: Identify common Redis key pattern syntax

    Redis keys often use colons ':' as separators for readability and grouping.
  2. Step 2: Compare options

    user:1234:name uses colons to separate user, ID, and attribute clearly.
  3. Final Answer:

    user:1234:name -> Option A
  4. Quick Check:

    Colon separators = correct pattern [OK]
Hint: Use colons to separate parts of keys [OK]
Common Mistakes:
  • Using dashes or slashes which are less common
  • Not separating parts clearly
  • Combining all parts without separators
3. Given these Redis keys: order:1001:status, order:1002:status, and order:1001:items, which command retrieves the status of order 1002?
medium
A. GET order:1001:items
B. GET order:1002:items
C. GET order:1002:status
D. GET order:1001:status

Solution

  1. Step 1: Identify the key for order 1002 status

    The key format is order:<order_id>:status, so for 1002 it is order:1002:status.
  2. Step 2: Match the command to the key

    GET order:1002:status retrieves the status of order 1002.
  3. Final Answer:

    GET order:1002:status -> Option C
  4. Quick Check:

    Correct key = order:1002:status [OK]
Hint: Match key parts exactly to get correct data [OK]
Common Mistakes:
  • Mixing order IDs in keys
  • Using wrong attribute like items instead of status
  • Confusing similar keys
4. You have keys like session-123 and session:124. Why might this inconsistency cause problems?
medium
A. Redis will merge both keys automatically
B. Redis will throw a syntax error
C. Keys with dashes are not allowed in Redis
D. It breaks pattern consistency, making data harder to manage

Solution

  1. Step 1: Understand key pattern consistency

    Consistent key patterns help group and find related data easily.
  2. Step 2: Analyze the impact of mixed separators

    Using both dashes and colons breaks this consistency, causing confusion.
  3. Final Answer:

    It breaks pattern consistency, making data harder to manage -> Option D
  4. Quick Check:

    Inconsistent keys = management issues [OK]
Hint: Always use one separator style consistently [OK]
Common Mistakes:
  • Thinking Redis forbids dashes
  • Expecting syntax errors from mixed separators
  • Assuming Redis merges keys automatically
5. You want to store product reviews in Redis for multiple products and users. Which key design pattern is best to efficiently group reviews by product and user?
hard
A. review:<product_id>:<user_id>
B. <product_id>-review-<user_id>
C. review<product_id><user_id>
D. review/<product_id>/<user_id>

Solution

  1. Step 1: Identify clear, scalable key pattern

    Using colons to separate product and user IDs groups reviews logically.
  2. Step 2: Evaluate options for clarity and consistency

    review:<product_id>:<user_id> uses colons and a prefix 'review' for easy filtering and management.
  3. Final Answer:

    review:<product_id>:<user_id> -> Option A
  4. Quick Check:

    Colon-separated keys = best practice [OK]
Hint: Use prefixes and colons to group related data [OK]
Common Mistakes:
  • Using ambiguous separators like dashes or slashes
  • Combining parts without separators
  • Not using a clear prefix for grouping
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