Bird
Raised Fist0
Redisquery~20 mins

Hash vs string for objects in Redis - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Hash vs String Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why choose a Redis hash over a string for storing user profiles?

Consider you want to store user profile data in Redis. Which reason best explains why using a Redis hash is better than a single string for this?

AStrings automatically compress data, saving memory compared to hashes.
BHashes allow updating individual fields without rewriting the entire object.
CHashes can only store numeric values, making them faster than strings.
DStrings support nested data structures, while hashes do not.
Attempts:
2 left
💡 Hint

Think about how you update parts of a user profile frequently.

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

Given these commands executed in order, what is the output of the final command?

HSET user:1 name "Alice" age "30"
HSET user:1 city "Paris"
HGETALL user:1
A["name", "Alice", "age", "30", "city", "Paris"]
B["user:1", "name", "Alice", "age", "30", "city", "Paris"]
CError: key user:1 does not exist
D["name", "Alice", "city", "Paris"]
Attempts:
2 left
💡 Hint

HGETALL returns all fields and values of a hash as a flat list.

📝 Syntax
advanced
2:00remaining
Which Redis command syntax correctly sets multiple fields in a hash?

Select the correct Redis command to set the fields 'name' to 'Bob' and 'age' to '25' in the hash 'user:2'.

AHMSET user:2 name Bob age 25
BSET user:2 name Bob age 25
CHSET user:2 {name: 'Bob', age: 25}
DHSET user:2 name Bob age 25
Attempts:
2 left
💡 Hint

Recent Redis versions recommend a specific command for setting multiple hash fields.

❓ optimization
advanced
2:00remaining
Which approach is more memory efficient for storing 1000 small user objects in Redis?

You have 1000 user objects each with 5 small fields. Which Redis data structure is generally more memory efficient?

AStore each user as a separate Redis string with JSON data.
BStore all users in a single Redis list as JSON strings.
CStore each user as a Redis hash with 5 fields.
DStore each user as a Redis set with field values as members.
Attempts:
2 left
💡 Hint

Consider Redis internal encoding and overhead for small objects.

🔧 Debug
expert
2:00remaining
Why does this Redis command fail with a WRONGTYPE error?

Given the commands below, why does the last command produce a WRONGTYPE error?

SET user:3 "{\"name\":\"Eve\", \"age\": 28}"
HGET user:3 name
ABecause 'user:3' is a string key, not a hash, so HGET cannot be used.
BBecause the JSON string is invalid and cannot be parsed by HGET.
CBecause HGET requires the key to be a string, not a hash.
DBecause the field 'name' does not exist in the string value.
Attempts:
2 left
💡 Hint

Check the data type of the key before using hash commands.

Practice

(1/5)
1. In Redis, when should you prefer using a hash over a string to store data?
easy
A. When you want to store multiple related fields under one key, like an object.
B. When you only need to store a single simple value per key.
C. When you want to store large binary data efficiently.
D. When you want to store data that changes very rarely.

Solution

  1. Step 1: Understand Redis data types

    Strings store single values per key, while hashes can store multiple fields under one key.
  2. Step 2: Match use case to data type

    If you want to store an object with multiple fields (like name, age, email), hashes are better suited.
  3. Final Answer:

    When you want to store multiple related fields under one key, like an object. -> Option A
  4. Quick Check:

    Hash = multiple fields per key [OK]
Hint: Use hashes for objects with many fields, strings for single values [OK]
Common Mistakes:
  • Thinking strings can store multiple fields easily
  • Using hashes for simple single values
  • Confusing hashes with lists or sets
2. Which of the following Redis commands correctly sets a field age to 30 in a hash named user:1?
easy
A. SET user:1 age 30
B. HSET user:1 age 30
C. HMSET user:1 age: 30
D. SETFIELD user:1 age 30

Solution

  1. Step 1: Identify command for setting a single field in a hash

    The HSET command sets a field in a hash key.
  2. Step 2: Check other options

    SET is for strings, HMSET is deprecated but similar to HSET, SETFIELD is invalid.
  3. Final Answer:

    HSET user:1 age 30 -> Option B
  4. Quick Check:

    HSET sets hash fields [OK]
Hint: Use HSET to set one field in a hash [OK]
Common Mistakes:
  • Using SET for hash fields
  • Using deprecated HMSET instead of HSET
  • Inventing commands like SETFIELD
3. Given the following Redis commands executed in order:
HSET user:2 name "Alice" age 25
HSET user:2 city "Paris"
GET user:2

What will be the output of the GET user:2 command?
medium
A. null (no value)
B. "Alice"
C. A list of all fields and values in user:2.
D. An error because user:2 is a hash, not a string.

Solution

  1. Step 1: Understand data types and commands

    HSET creates a hash key user:2. GET works only on string keys.
  2. Step 2: Predict command behavior

    Using GET on a hash key causes a type error in Redis.
  3. Final Answer:

    An error because user:2 is a hash, not a string. -> Option D
  4. Quick Check:

    GET on hash key = error [OK]
Hint: GET only works on strings, not hashes [OK]
Common Mistakes:
  • Expecting GET to return hash fields
  • Confusing GET with HGETALL
  • Assuming null means no error
4. You have this Redis command sequence:
HSET user:3 name "Bob" age 40
SET user:3 "simple string"

What problem will occur and how to fix it?
medium
A. Error because user:3 key type conflicts; delete key before SET.
B. No problem; Redis allows both hash and string on same key.
C. The SET command will overwrite the hash silently.
D. The HSET command will fail because key exists as string.

Solution

  1. Step 1: Understand Redis key type rules

    A key can only hold one data type at a time. Setting a string on a key that is a hash causes an error.
  2. Step 2: Identify fix

    To set a string on the same key, delete the existing hash key first using DEL.
  3. Final Answer:

    Error because user:3 key type conflicts; delete key before SET. -> Option A
  4. Quick Check:

    Key type conflict = error, DEL key first [OK]
Hint: Delete key before changing type to avoid errors [OK]
Common Mistakes:
  • Assuming Redis allows multiple types on one key
  • Expecting SET to overwrite hash without error
  • Not deleting key before changing type
5. You want to store user profiles with fields name, age, and email in Redis. You also want to efficiently update individual fields without rewriting the whole object. Which approach is best and why?
hard
A. Store each user as multiple string keys, one per field, like user:1:name, user:1:age.
B. Store each user as a string with JSON and rewrite entire string on update.
C. Store each user as a hash with fields for name, age, and email for easy updates.
D. Store all users in one big hash with user IDs as fields and JSON strings as values.

Solution

  1. Step 1: Consider update efficiency

    Updating individual fields is easiest with hashes because you can update one field without rewriting others.
  2. Step 2: Compare options

    Strings with JSON require rewriting whole string. Multiple string keys increase complexity. One big hash mixes users and complicates access.
  3. Final Answer:

    Store each user as a hash with fields for name, age, and email for easy updates. -> Option C
  4. Quick Check:

    Hashes = efficient field updates [OK]
Hint: Use hashes for objects needing partial field updates [OK]
Common Mistakes:
  • Using strings with JSON and rewriting whole data
  • Splitting fields into many string keys unnecessarily
  • Storing all users in one hash causing complexity
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