Denormalization for speed in Redis - Time & Space Complexity
Denormalization means storing data in a way that avoids extra lookups. This helps speed up reading data in Redis.
We want to see how the time to get data changes when we use denormalization.
Analyze the time complexity of the following Redis commands for fetching user info.
# Normalized approach
HGETALL user:1000
HGETALL profile:1000
# Denormalized approach
HGETALL user_profile:1000
This code shows fetching user data from two hashes separately versus one combined hash.
Look at what repeats when fetching data.
- Primary operation: Reading hash fields with HGETALL.
- How many times: Normalized reads two hashes separately; denormalized reads one combined hash once.
As the number of fields grows, the time to read grows too.
| Input Size (fields) | Normalized Reads (2 hashes) | Denormalized Read (1 hash) |
|---|---|---|
| 10 | ~20 field reads | ~10 field reads |
| 100 | ~200 field reads | ~100 field reads |
| 1000 | ~2000 field reads | ~1000 field reads |
Pattern observation: Normalized approach doubles the work because it reads two hashes; denormalized reads once, so work grows linearly with fields.
Time Complexity: O(n)
This means the time to read data grows directly with the number of fields you fetch.
[X] Wrong: "Denormalization always makes data fetching instant regardless of size."
[OK] Correct: Even with denormalization, reading more fields takes more time because Redis reads each field once.
Understanding how denormalization affects read speed helps you design fast Redis queries and shows you think about real-world data access patterns.
"What if we split the denormalized hash into three smaller hashes instead of one big one? How would the time complexity change?"
Practice
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 AQuick Check:
Denormalization = Faster access [OK]
- Confusing denormalization with normalization
- Thinking it reduces memory usage
- Assuming it automatically backs up data
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 AQuick Check:
Hash = field-value pairs for related data [OK]
- Choosing List which is just ordered values
- Using Set which stores unique values without fields
- Picking Sorted Set which is for ordered unique elements
HSET user:1000 name "Alice" age 30 city "NY"
What does
HGETALL user:1000 return?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 BQuick Check:
HGETALL returns all fields and values as strings [OK]
- Expecting numeric types instead of strings
- Confusing key name with fields
- Partial field-value pairs returned
HSET user:2000 name "Bob" age 25 city "LA"
But later
HGET user:2000 age returns nil. What is the likely cause?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 CQuick Check:
Missing field means deleted or overwritten [OK]
- Assuming key does not exist when other fields exist
- Misspelling field name in HGET
- Thinking HSET failed without error
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 DQuick Check:
Denormalize related data together for speed [OK]
- Keeping data normalized causing slow joins
- Using sorted sets/lists without grouping fields
- Duplicating data without grouping properly
Ready to prove it?
Pass the test and this pattern moves unknown
