Embedding vs referencing in Redis - Performance Comparison
When storing a list of related items (like post IDs) in Redis, we can either embed the list as a string in a hash field or store it in a separate set.
We want to see how the time to get data changes as the data grows.
Analyze the time complexity of these Redis commands for embedding and referencing.
# Embedding: post IDs as string in hash field
HSET user:1 name "Alice" posts "post1,post2,post3"
# Fetch: O(1)
HGET user:1 posts
# Referencing: post IDs in separate set
HSET user:1 name "Alice"
SADD user:1:posts post1 post2 post3
# Fetch: O(N)
SMEMBERS user:1:posts
This code shows two ways to store and fetch a user's post list: embedded string vs set.
Look at what repeats when fetching data.
- Primary operation: HGET for the posts field (embedding) vs SMEMBERS for the posts set (referencing).
- Time growth: Embedding is O(1). Referencing is O(N) where N is the number of posts.
As the number of posts grows, the work changes differently for each method.
| Input Size (posts) | Embedding | Referencing |
|---|---|---|
| 10 | O(1) | O(10) |
| 100 | O(1) | O(100) |
| 1000 | O(1) | O(1000) |
Embedding stays constant because all data is fetched at once. Referencing grows linearly with the number of posts.
Time Complexity: O(1) for embedding, O(N) for referencing
Embedding fetches all data in one step, while referencing requires more steps as data grows.
[X] Wrong: "All single Redis commands are O(1)."
[OK] Correct: SMEMBERS requires time proportional to the set size O(N), unlike HGET which is O(1).
Understanding how data structure choices affect speed helps you design better Redis solutions and explain your reasoning clearly.
"What if we used a Redis pipeline to fetch all referenced posts at once? How would that affect time complexity?"
Practice
Solution
Step 1: Understand embedding concept
Embedding means putting related data inside one Redis key, so all data is together.Step 2: Compare with referencing
Referencing stores data separately and links them by IDs, which is different from embedding.Final Answer:
Storing related data together in one key. -> Option AQuick Check:
Embedding = data together [OK]
- Confusing embedding with referencing
- Thinking embedding uses multiple keys
- Assuming embedding stores only IDs
Solution
Step 1: Identify data structure for embedding
Hashes store multiple fields and values inside one key, ideal for embedding related small data.Step 2: Compare other structures
Strings hold single values, Lists and Sorted Sets are for ordered collections, less suited for embedding small related fields.Final Answer:
Hash -> Option DQuick Check:
Embedding small data = Hash [OK]
- Choosing String for multiple fields
- Confusing List with Hash
- Using Sorted Set without score relevance
HSET user:100 name "Alice" age 30 HSET user:100:address city "NY" zip 10001Which describes the data design?
Solution
Step 1: Analyze keys and data
user:100 holds name and age, user:100:address holds address fields separately.Step 2: Identify design pattern
Address is stored in a separate key linked by user ID, which is referencing.Final Answer:
Referencing address data in a separate key -> Option AQuick Check:
Separate keys linked by ID = referencing [OK]
- Thinking all data is embedded in one key
- Confusing List with Hash usage
- Assuming address is embedded inside user key
Solution
Step 1: Check embedding address in user hash
Embedding means all fields in one key, so address fields city and zip should be in user:200 hash.Step 2: Identify error in command C
Command C uses a separate key user:200:address, storing address separately linked by ID. This is referencing, not embedding.Final Answer:
HSET user:200:address city "LA" zip 90001 -> Option BQuick Check:
Separate keys = referencing, not embedding [OK]
- Missing value for a field
- Using separate keys when embedding
- Mixing field-value pairs incorrectly
Solution
Step 1: Consider data size and access pattern
Reviews can be many and large, embedding all in one key can cause large data and slow updates.Step 2: Choose referencing for scalability
Storing product details separately and reviews in linked keys allows efficient updates and reads without large keys.Step 3: Evaluate other options
Embedding all reviews in one hash risks large keys; storing all reviews in a single list key for all products mixes products; storing product details as strings and reviews as sorted sets don't fit review structure well.Final Answer:
Store product details in one hash and each review in separate keys linked by product ID. -> Option CQuick Check:
Large related data = referencing for performance [OK]
- Embedding large lists causing slow reads
- Mixing all reviews in one key
- Ignoring update frequency of reviews
Ready to prove it?
Pass the test and this pattern moves unknown
