What if your data could organize itself so you never lose track again?
Embedding vs referencing in Redis - When to Use Which
Imagine you have a big box of photos and notes scattered all over your room. To find a specific photo or note, you have to dig through piles manually every time.
Searching through scattered papers is slow and frustrating. You might lose important notes or mix things up. It's easy to make mistakes and waste time.
Embedding and referencing organize your data smartly. Embedding puts related info together, like keeping photos and notes in one album. Referencing links separate items with clear pointers, like a catalog telling you where each photo is.
store photo1 store note1 search photo1 search note1
embed photo1 with note1 or reference photo1 -> note1
It lets you quickly find and manage related data without confusion or lost pieces.
In a social app, embedding user comments inside posts keeps everything together, while referencing user profiles separately avoids repeating info and keeps data clean.
Manual data handling is slow and error-prone.
Embedding groups related data for fast access.
Referencing links data to avoid duplication and keep it organized.
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
