📖 Scenario: You are building a simple online store database using Redis. To make product information retrieval faster, you want to denormalize data by storing product details and their category names together.
🎯 Goal: Create Redis data structures to store products and categories separately, then denormalize by adding category names directly into product hashes for faster access.
📋 What You'll Learn
Create Redis hashes for categories with exact IDs and names
Create Redis hashes for products with exact IDs and names
Add a configuration variable for the category ID to denormalize
Update the product hash to include the category name for the given category ID
💡 Why This Matters
🌍 Real World
Denormalization in Redis is used in real online stores and apps to speed up reading product data by storing related info together.
💼 Career
Understanding denormalization helps database engineers and backend developers optimize performance in fast data systems.
Progress0 / 4 steps
1
Create category hashes
Create two Redis hashes with keys category:1 and category:2. Set the field name to "Electronics" for category:1 and "Books" for category:2.
Redis
Hint
Use the HSET command to create hashes with the field name.
2
Create product hashes
Create two Redis hashes with keys product:101 and product:102. Set the field name to "Smartphone" for product:101 and "Novel" for product:102. Also set the field category_id to 1 for product:101 and 2 for product:102.
Redis
Hint
Use HSET with multiple fields to add product name and category ID.
3
Set category ID to denormalize
Create a Redis string key called denormalize_category_id and set its value to 1 to indicate which category ID to denormalize.
Redis
Hint
Use the SET command to store the category ID to denormalize.
4
Denormalize category name into product hash
For the product with key product:101, add a new field category_name and set its value to the category name from category:1 (which is "Electronics").
Redis
Hint
Use HSET to add the category_name field to the product hash.
Practice
(1/5)
1. What is the main benefit of denormalization in Redis?
easy
A. Faster data retrieval by storing related data together
B. Reducing memory usage by splitting data into many keys
C. Ensuring data is always normalized for consistency
D. Automatically backing up data to disk
Solution
Step 1: Understand denormalization purpose
Denormalization means storing related data together to avoid multiple lookups.
Step 2: Identify main benefit in Redis context
This reduces the number of Redis calls, making data retrieval faster.
Final Answer:
Faster data retrieval by storing related data together -> Option A
Quick Check:
Denormalization = Faster access [OK]
Hint: Denormalization means fewer lookups, so speed improves [OK]
Common Mistakes:
Confusing denormalization with normalization
Thinking it reduces memory usage
Assuming it automatically backs up data
2. Which Redis data structure is best suited for denormalization to store related user info together?
easy
A. Hash
B. List
C. Set
D. Sorted Set
Solution
Step 1: Review Redis data structures
Lists store ordered values, Sets store unique unordered values, Sorted Sets store ordered unique values, Hashes store field-value pairs.
Step 2: Choose structure for related fields
Hashes allow storing multiple fields (like name, email) under one key, ideal for denormalized user info.
Final Answer:
Hash -> Option A
Quick Check:
Hash = field-value pairs for related data [OK]
Hint: Use Hash for grouped related fields in one key [OK]
Common Mistakes:
Choosing List which is just ordered values
Using Set which stores unique values without fields
Picking Sorted Set which is for ordered unique elements
3. Given these Redis commands for denormalized user data:
HSET user:1000 name "Alice" age 30 city "NY"
What does HGETALL user:1000 return?
medium
A. ["name", "Alice", "age", "30"]
B. ["name", "Alice", "age", "30", "city", "NY"]
C. ["name", "Alice", "age", 30, "city", "NY"]
D. ["user:1000", "Alice", "30", "NY"]
Solution
Step 1: Understand HSET command
HSET stores fields and values under key 'user:1000' as strings: name=Alice, age=30, city=NY.
Step 2: Understand HGETALL output format
HGETALL returns an array of field-value pairs as strings in order stored.
Final Answer:
["name", "Alice", "age", "30", "city", "NY"] -> Option B
Quick Check:
HGETALL returns all fields and values as strings [OK]
Hint: HGETALL returns flat list of field-value strings [OK]
Common Mistakes:
Expecting numeric types instead of strings
Confusing key name with fields
Partial field-value pairs returned
4. You try to denormalize user data with:
HSET user:2000 name "Bob" age 25 city "LA"
But later HGET user:2000 age returns nil. What is the likely cause?
medium
A. HSET command was not executed properly
B. The field name is misspelled in HGET
C. The age field was overwritten or deleted
D. The key user:2000 does not exist
Solution
Step 1: Check if key exists
If key did not exist, all fields would be missing, but only age is missing.
Step 2: Consider field deletion or overwrite
Age field might have been deleted or overwritten after initial HSET, causing nil return.
Final Answer:
The age field was overwritten or deleted -> Option C
Quick Check:
Missing field means deleted or overwritten [OK]
Hint: Missing field usually means it was deleted or overwritten [OK]
Common Mistakes:
Assuming key does not exist when other fields exist
Misspelling field name in HGET
Thinking HSET failed without error
5. You want to speed up a Redis app by denormalizing product and category data. Which approach is best?
hard
A. Use a sorted set for categories and a list for products
B. Store product and category in separate keys and join at runtime
C. Normalize data fully to avoid duplication
D. Store product info and category name together in one hash key per product
Solution
Step 1: Understand denormalization goal
Denormalization stores related data together to reduce lookups and speed access.
Step 2: Evaluate options for product-category data
Storing product info and category name together in one hash avoids extra lookups, speeding queries.
Step 3: Compare other options
Separate keys require joins (slower), sorted sets/lists don't group related fields, full normalization slows access.
Final Answer:
Store product info and category name together in one hash key per product -> Option D
Quick Check:
Denormalize related data together for speed [OK]
Hint: Put related data in one hash to avoid joins [OK]
Common Mistakes:
Keeping data normalized causing slow joins
Using sorted sets/lists without grouping fields
Duplicating data without grouping properly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.