0
0
Redisquery~10 mins

Write-through pattern in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Write-through pattern
Client writes data
Write to Cache
Write to Database
Confirm write success
Return success to Client
The write-through pattern writes data to the cache and database at the same time, ensuring cache and database are always synchronized.
Execution Sample
Redis
SET user:1 "Alice"
// Write to cache
SETDB user:1 "Alice"
// Write to database
RETURN success
This example shows writing a user name to both Redis cache and the database before confirming success.
Execution Table
StepActionCache StateDatabase StateResult
1Client sends write request for key 'user:1' with value 'Alice'{}{}Request received
2Write 'Alice' to cache key 'user:1'{"user:1": "Alice"}{}Cache updated
3Write 'Alice' to database key 'user:1'{"user:1": "Alice"}{"user:1": "Alice"}Database updated
4Confirm write success to client{"user:1": "Alice"}{"user:1": "Alice"}Success returned
5End of write-through operation{"user:1": "Alice"}{"user:1": "Alice"}Operation complete
💡 Write-through completes after both cache and database are updated successfully.
Variable Tracker
VariableStartAfter Step 2After Step 3Final
Cache{}{"user:1": "Alice"}{"user:1": "Alice"}{"user:1": "Alice"}
Database{}{}{"user:1": "Alice"}{"user:1": "Alice"}
Key Moments - 2 Insights
Why do we write to the cache before the database?
Writing to the cache first ensures the cache always has the latest data when the database write succeeds, as shown in steps 2 and 3 of the execution_table.
What happens if the database write fails after cache is updated?
In the write-through pattern, failure handling is needed to keep cache and database consistent. The example assumes success, but in real use, rollback or retry logic is required after step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the cache state after step 2?
A{"user:1": "Bob"}
B{"user:1": "Alice"}
C{}
Dnull
💡 Hint
Check the 'Cache State' column in row for step 2.
At which step does the database get updated?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column describing database write.
If the database write fails, what should happen to keep cache consistent?
ARollback cache update or retry database write
BDo nothing, cache stays updated
CClear the database
DReturn success anyway
💡 Hint
Refer to key_moments explanation about failure after step 3.
Concept Snapshot
Write-through pattern:
- Write data to cache and database simultaneously
- Cache always has latest data
- Ensures strong consistency
- Write fails if either cache or database fails
- Used when cache must reflect database immediately
Full Transcript
The write-through pattern means when a client writes data, the system writes it first to the cache, then to the database. This keeps both cache and database synchronized. The example shows writing a key 'user:1' with value 'Alice'. Step 1 receives the request. Step 2 writes to cache. Step 3 writes to database. Step 4 confirms success to client. Variables track cache and database states changing from empty to holding the new data. Key moments include why cache is written first and what happens if database write fails. The visual quiz checks understanding of cache state after step 2, when database updates, and failure handling. The snapshot summarizes the pattern as writing to cache and database together to keep data consistent.