Write-through pattern in Redis - Time & Space Complexity
We want to understand how the time it takes to save data changes as the amount of data grows when using the write-through pattern in Redis.
Specifically, how does writing to both cache and database affect performance as data size increases?
Analyze the time complexity of the following Redis write-through code snippet.
-- Write-through cache update
redis.call('SET', key, value) -- write to cache
database_write(key, value) -- write to database
return 'OK'
This code writes data to the Redis cache first, then writes the same data to the database, ensuring both are updated immediately.
In this snippet, there are no loops or recursion. The operations happen once per write.
- Primary operation: Two write commands: one to Redis cache, one to the database.
- How many times: Once per data write request.
Each write involves two steps regardless of data size. So, if you write 10 items, you do 20 writes total; for 100 items, 200 writes; for 1000 items, 2000 writes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 20 |
| 100 | 200 |
| 1000 | 2000 |
Pattern observation: The number of operations grows directly with the number of writes; doubling the writes doubles the operations.
Time Complexity: O(n)
This means the time to complete all writes grows linearly with the number of items you write.
[X] Wrong: "Writing to cache and database at the same time means the time stays the same no matter how many writes happen."
[OK] Correct: Each write still happens one after another for each item, so more items mean more total work and longer time.
Understanding how write-through caching affects time helps you explain trade-offs between speed and data consistency in real systems.
"What if we changed the write-through pattern to write-back caching? How would the time complexity change?"
Practice
write-through pattern in Redis caching?Solution
Step 1: Understand write-through pattern basics
The write-through pattern means writing data to the cache and database simultaneously to keep them in sync.Step 2: Identify the correct behavior in Redis context
In Redis, this pattern ensures cache is updated immediately when the database changes, avoiding stale data.Final Answer:
To update both the cache and the database at the same time -> Option DQuick Check:
Write-through = update cache + database [OK]
- Thinking write-through updates only cache
- Confusing write-through with write-back pattern
- Assuming cache is updated after database asynchronously
user:123 and value {"name":"Alice"}?Solution
Step 1: Identify write-through steps
Write-through requires writing to cache (Redis) and database together.Step 2: Match Redis commands to write-through
Using SET updates Redis cache; followed by DB update ensures both are in sync.Final Answer:
SET user:123 '{"name":"Alice"}' followed by DB update -> Option CQuick Check:
SET + DB update = write-through [OK]
- Only updating cache without DB
- Deleting cache before setting without DB update
- Getting cache before setting without DB write
cache.SET('item:1', 'value1')
database.UPDATE('item:1', 'value1')
result = cache.GET('item:1')
What will result contain after execution?Solution
Step 1: Analyze cache write
The cache is set to 'value1' for key 'item:1' before database update.Step 2: Analyze cache read
Reading from cache immediately after setting returns the new value 'value1'.Final Answer:
'value1' -> Option BQuick Check:
Cache GET after SET returns new value [OK]
- Assuming cache GET returns null if DB updated
- Thinking old cached value remains after SET
- Expecting error without explicit DB read
Solution
Step 1: Identify cause of stale cache
If database updates but cache is not updated, cache holds old data causing staleness.Step 2: Match mistake to write-through pattern
Write-through requires cache update on every DB write; missing cache update breaks this.Final Answer:
Updating database but forgetting to update Redis cache -> Option AQuick Check:
Missing cache update causes stale data [OK]
- Updating cache only without DB
- Using GET to update cache instead of SET
- Not clearing cache on DB deletes
Solution
Step 1: Understand write-through consistency needs
Write-through requires cache and database to be updated together to avoid stale reads.Step 2: Evaluate options for consistency
Updating both synchronously ensures cache always reflects latest DB state, minimizing stale data.Final Answer:
On product update, update both Redis cache and database synchronously in the same operation -> Option AQuick Check:
Synchronous cache + DB update = consistent write-through [OK]
- Updating cache before DB asynchronously causing race conditions
- Deleting cache instead of updating it
- Relying only on cache expiration for freshness
Ready to prove it?
Pass the test and this pattern moves unknown
