Cache-aside (lazy loading) deep dive in Redis - Time & Space Complexity
We want to understand how the time to get data changes when using cache-aside in Redis.
Specifically, how the number of operations grows when fetching data that may or may not be cached.
Analyze the time complexity of the following Redis cache-aside pattern.
# Try to get data from cache
GET user:123
# If cache miss, fetch from database
# Then store in cache
SET user:123 <data>
This code tries to get user data from Redis cache first. If not found, it fetches from the database and updates the cache.
Look for repeated actions that affect time.
- Primary operation: Single GET command to Redis, possibly followed by a database fetch and SET.
- How many times: Once per data request; no loops or recursion inside this snippet.
Each request involves one or two main operations: a cache GET and possibly a database fetch plus cache SET.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 GETs, some SETs if misses |
| 100 | About 100 GETs, some SETs if misses |
| 1000 | About 1000 GETs, some SETs if misses |
Pattern observation: The number of operations grows linearly with the number of requests.
Time Complexity: O(n)
This means the time grows directly in proportion to the number of data requests.
[X] Wrong: "Cache-aside always makes data fetching faster regardless of input size."
[OK] Correct: If many requests miss the cache, each miss causes a database fetch, which can slow things down as requests grow.
Understanding how cache-aside scales helps you explain real-world data fetching strategies clearly and confidently.
"What if we added a batch GET for multiple keys instead of single GETs? How would the time complexity change?"
Practice
Cache-aside (lazy loading) pattern in Redis?Solution
Step 1: Understand cache-aside purpose
Cache-aside means data is loaded into cache only when requested and not found in cache.Step 2: Compare options with definition
Load data into cache only when it is requested and missing matches this lazy loading idea; others describe different caching strategies.Final Answer:
Load data into cache only when it is requested and missing -> Option BQuick Check:
Cache-aside = lazy loading on miss [OK]
- Thinking cache preloads all data
- Confusing cache-aside with write-through caching
- Assuming cache and DB never sync
Solution
Step 1: Identify command to check key presence
Redis commandEXISTSreturns if a key is present in cache.Step 2: Eliminate other commands
GETretrieves value,SETwrites value,DELdeletes key; onlyEXISTSchecks presence.Final Answer:
EXISTS -> Option AQuick Check:
Check key presence = EXISTS [OK]
- Using GET to check existence (returns nil if missing)
- Confusing SET as check command
- Using DEL which deletes keys
if not redis.exists(key): data = db.query(key) redis.set(key, data) return redis.get(key)
What will be returned if the key is missing in cache but present in database?
Solution
Step 1: Follow code logic on cache miss
If key missing in Redis, code queries database and sets cache with fresh data.Step 2: Return cached data after setting
After setting, code returnsredis.get(key)which now holds fresh data.Final Answer:
The data fetched from database and cached -> Option DQuick Check:
Cache miss triggers DB fetch and cache set [OK]
- Assuming cache returns empty if initially missing
- Expecting error on missing cache key
- Thinking cache returns stale data without update
if redis.get(key) is None: data = db.query(key) redis.set(key, data) return redis.get(key)
But sometimes stale data is returned after DB update. What is the likely bug?
Solution
Step 1: Identify stale data cause
Stale data means cache was not refreshed after DB update.Step 2: Check cache update strategy
Code only sets cache on miss, but does not invalidate or update cache after DB changes.Final Answer:
Not deleting or updating cache after DB changes -> Option CQuick Check:
Cache stale if not refreshed after DB update [OK]
- Thinking redis.get check causes stale data
- Believing cache sets before DB query fixes stale data
- Ignoring cache invalidation on DB writes
Solution
Step 1: Analyze cache consistency needs
Frequent DB updates require cache invalidation to avoid stale data.Step 2: Evaluate options for cache-aside pattern
On DB update, delete cache key; on read, load from DB if cache miss deletes cache on DB update, so next read reloads fresh data, matching cache-aside best practice.Final Answer:
On DB update, delete cache key; on read, load from DB if cache miss -> Option AQuick Check:
Invalidate cache on update, lazy load on read [OK]
- Writing cache before DB causes inconsistency
- Preloading all data wastes memory and may be stale
- Ignoring cache leads to poor performance
Ready to prove it?
Pass the test and this pattern moves unknown
